Android Google Map实例 - 添加Google Map自定义图层(Android mapview)

上篇:Android Google Map应用实例 - 创建一个Google Map例程(Android mapview)

通常情况光有地图还是不够的,如何添加一个自定义的图层,譬如说把一个热点,一个基站地址标注在地图上。如下步骤帮你完成Android Google Map 自定义图层的添加

 

创建一个 ItemizedOverlay 类

  1. 新建一个 Java 类 HelloItemizedOverlay
    在新建列表框中的
    Superclass, 填入"com.google.android.maps.ItemizedOverlay". 选择选项 Constructors from superclass.
  2. 需要 OverlayItem ArrayList, 通过这个ArrayList将所需要的地标OverlayItem 标到地图上. 在 HelloItemizedOverlay 类中添加:
    private ArrayList mOverlays = new ArrayList();
  3. 定义构造函数
    public HelloItemizedOverlay(Drawable defaultMarker) {
      super(boundCenterBottom(defaultMarker));
    }
  4. 为了将 OverlayItems 加到 ArrayList, 需要addOverlay方法:
    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }
    每添加一个OverlayItem 到ArrayList,需要调用 populate(),用来读取所有的OverlayItems 并准备画到图层上。
  5. populate()方法执行完成后会调用createItem(int),重写这个函数获取ArrayList 中OverlayItem 的指示。
    @Override
    protected OverlayItem createItem(int i) {
      return mOverlays.get(i);
    }
  6. 重写size函数:
    @Override
    public int size() {
      return mOverlays.size();
    }
  7. 下面来设置响应屏幕触摸的函数,添加成员变量 private Context mContext;

添加一个新的构造函数:
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
  this(defaultMarker);
 
this.mContext = context;
}
重写 onTap(int) 函数, 用来响应的用户的触摸:
@Override
protected boolean onTap(int index) {
  OverlayItem item =mOverlays.get(index);
  AlertDialog.Builder dialog = newAlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

HelloItemizedOverlay类完成了.

打开HelloGoogleMaps 类.下面将新建 OverlayItem 并加到HelloItemizedOverlay 实例中, 并将HelloItemizedOverlay图层加到MapView.

 

  1. 首先需要一个图标的图片,将文件拖到 res/drawable/ 文件夹下即可.
  2. 在 onCreate() 方法的后面, 加上:
    List mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.
    XXXX);  //XXXX为图标图片的文件名

HelloItemizedOverlay itemizedoverlay = newHelloItemizedOverlay(drawable,this);

  1. 创建GeoPoint , 并存入OverlayItem:
    GeoPoint point = new GeoPoint(29578181,121512974);
    OverlayItem overlayitem = new OverlayItem(point, "Hello!", "I'm in Chunhu!");
  2. 将 OverlayItem 添加入 HelloItemizedOverlay 实例,:
    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);
  3. 添加新标注:

GeoPointpoint3 = new GeoPoint(29578290,121613174);

OverlayItem overlayitem3 = newOverlayItem(point3, "Hello3!","I'm in Chunhu3!");

itemizedoverlay.addOverlay(overlayitem3);

  1. onCreate()方法后添加:

mymapController.setZoom(9);      //设置地图放大层级

  1. 运行程序得到如下左图结果:

 

  1. 点击图标显示如上右图

 

详细代码:

HelloGoogleMaps.java文件:

package com.google.maps.HelloGoogleMaps; import java.util.List; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import com.google.android.maps.*; public class HelloGoogleMaps extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView)findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true); mapView.setEnabled(true); MapController mymapController = mapView.getController(); Double lng = 121.512974 * 1E6; Double lat = 29.578181 * 1E6; GeoPoint point = new GeoPoint( lat.intValue(),lng.intValue()); mymapController.setCenter(point);//显示中心 mymapController.setZoom(9); //放大层级 // mapView.setSatellite(true); //是否为卫星模式 List mapOverlays = mapView.getOverlays(); Drawable drawable = this.getResources().getDrawable(R.drawable.w9mt3knd); HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this); GeoPoint point2 = new GeoPoint(29578181,121512974); OverlayItem overlayitem2 = new OverlayItem(point2, "Hello!", "I'm in Chunhu!"); GeoPoint point3 = new GeoPoint(29578290,121613174); OverlayItem overlayitem3 = new OverlayItem(point3, "Hello2!", "I'm in Chunhu2!"); itemizedoverlay.addOverlay(overlayitem2); itemizedoverlay.addOverlay(overlayitem3); mapOverlays.add(itemizedoverlay); } @Override protected boolean isRouteDisplayed() { return false; } }

H e lloItemizedOverlay.java文件

package com.google.maps.HelloGoogleMaps; import java.util.ArrayList; import android.app.AlertDialog; import android.content.Context; import android.graphics.drawable.Drawable; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.OverlayItem; public class HelloItemizedOverlay extends ItemizedOverlay { private ArrayList mOverlays = new ArrayList(); private Context mContext; public HelloItemizedOverlay(Drawable defaultMarker) { //super(defaultMarker); super(boundCenterBottom(defaultMarker)); // TODO Auto-generated constructor stub } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } @Override protected OverlayItem createItem(int i) { // TODO Auto-generated method stub return mOverlays.get(i); } @Override public int size() { // TODO Auto-generated method stub return mOverlays.size(); } public HelloItemizedOverlay(Drawable defaultMarker, Context context) { //super(defaultMarker); this(defaultMarker); //mContext = context; this.mContext = context; } @Override protected boolean onTap(int index) { OverlayItem item = mOverlays.get(index); AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.show(); return true; } }

下篇: Android Google Map应用实例 - 安装到手机后的效果(Android mapview)

           Android Google Map应用实例 - 在地图和卫星图之间切换(Android mapview)

           Android Google Map应用实例 - 添加GPS位置标注(Android mapview)


转载本站文章请注明,转载自:ADASiteMap的空间[http://www.adasitemap.tk]

本文链接:Android Google Map实例 – 添加Google Map自定义图层(Android mapview) | ADASiteMap的空间


你可能感兴趣的:(/Android/,/google,maps/)