本文主要是介绍Android学习笔记之百度地图(分条目覆盖物:ItemizedOverlay),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
看看多了那三个小图片就是我们的效果结果。点击一下也会有相应的。试一下吧!
某个类型的覆盖物,包含多个类型相同、显示方式相同、处理方式相同的项时,使用此类:
- 自定义类继承ItemizedOverlay<OverlayItem>,并Override其draw()方法,如果需要点击、按键、触摸等交互操作,还需Override onTap()等方法。
主要用到类 OverlayItem,百度地图上的API
ItemizedOverlay是Overlay的一个基类,包含了一个OverlayItem列表。 从南到北的处理item,用于绘制、创建平移边界、为每个点绘制标记点,和维护一个焦点选中的item,同时也负责把一个屏幕点击匹配到item上去,分发焦点改变事件给备选的监听器。
类型 | 名称 |
---|---|
static int | ITEM_STATE_FOCUSED_MASK
|
static int | ITEM_STATE_NORMAL_MASK
|
static int | ITEM_STATE_PRESSED_MASK
|
static int | ITEM_STATE_SELECTED_MASK
|
protected Drawable | mMarker
|
protected GeoPoint | mPoint
|
protected java.lang.String | mSnippet
|
protected java.lang.String | mTitle
|
返回类型 | 方法 |
---|---|
Drawable | getMarker(int stateBitset)
|
GeoPoint | getPoint()
|
java.lang.String | getSnippet()
|
java.lang.String | getTitle()
|
java.lang.String | routableAddress()
|
void | setMarker(Drawable marker)
|
static void | setState(Drawable drawable, int stateBitset)
|
具体实现:
package xiaosi.baiduMap;import java.util.ArrayList;
import java.util.List;import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;public class BaiduMapActivity extends MapActivity
{/** Called when the activity is first created. */private BMapManager mapManager = null;private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";private MapView mapView = null;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);mapManager = new BMapManager(getApplication());mapManager.init(key, null);super.initMapActivity(mapManager);mapView = (MapView) findViewById(R.id.mapsView);mapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件MapController mapController = mapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放mapController.setZoom(12); // 设置地图zoom级别Drawable marker = getResources().getDrawable(R.drawable.d); //得到需要标在地图上的资源mapView.getOverlays().add(new OverItemT(marker, this)); //添加ItemizedOverlay实例到mMapView}class OverItemT extends ItemizedOverlay<OverlayItem>{private List<OverlayItem> GeoPointList = new ArrayList<OverlayItem>();private Context mContext;//三个坐标private double coordinateX1= 39.90923;private double coordinateY1 = 116.397428;private double coordinateX2 = 39.9022;private double coordinateY2 = 116.3922;private double coordinateX3 = 39.917723;private double coordinateY3 = 116.3722;private double coordinateX4 = 39.90923;private double coordinateY4 = 116.3922;public OverItemT(Drawable marker, Context context){//调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素。super(boundCenterBottom(marker));this.mContext = context;// 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)GeoPoint point1 = new GeoPoint((int) (coordinateX1 * 1E6), (int) (coordinateY1 * 1E6));GeoPoint point2 = new GeoPoint((int) (coordinateX2 * 1E6), (int) (coordinateY2 * 1E6));GeoPoint point3 = new GeoPoint((int) (coordinateX3 * 1E6), (int) (coordinateY3 * 1E6));GeoPoint point4 = new GeoPoint((int) (coordinateX4 * 1E6), (int) (coordinateY4 * 1E6));/*OverlayItem(GeoPoint point, String title, String snippet)* point 该item的位置* title 该item的标题文本* snippet 该item的文字片段*/GeoPointList.add(new OverlayItem(point1, "P1", "写字楼"));GeoPointList.add(new OverlayItem(point2, "P2", "西城区政府"));GeoPointList.add(new OverlayItem(point3, "P3", "招待所"));GeoPointList.add(new OverlayItem(point4, "P4", "小学"));// createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法populate(); }@Overrideprotected OverlayItem createItem(int i){return GeoPointList.get(i);}@Overridepublic int size(){System.out.println("Size;" + GeoPointList.size());return GeoPointList.size();}@Override// 处理当点击事件protected boolean onTap(int i){//getSnippet() 该item的文字片段Toast.makeText(this.mContext, GeoPointList.get(i).getSnippet(),Toast.LENGTH_SHORT).show();return true;}}@Overrideprotected boolean isRouteDisplayed(){return false;}@Overrideprotected void onDestroy(){if (mapManager != null){mapManager.destroy();mapManager = null;}super.onDestroy();}@Overrideprotected void onPause(){if (mapManager != null){mapManager.stop();}super.onPause();}@Overrideprotected void onResume(){if (mapManager != null){mapManager.start();}super.onResume();}
}
源代码下载: 点击打开链接
这篇关于Android学习笔记之百度地图(分条目覆盖物:ItemizedOverlay)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!