ItemizedOverlay的初级使用

实现一个ItemizedOverlay其实很简单,首先一个装载item的list,用于读取并绘制每一个item,创建一个添加方法 add 剩下的事情留给框架处理吧

引用

package map.chaos.com;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MyMap extends MapActivity {
MapView mapview;
MapController controller;
GeoPoint[] g=new GeoPoint[5];
LocationManager locationManager;
Drawable[] drawables;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
drawables=new Drawable[]{getResources().getDrawable(R.drawable.home),
getResources().getDrawable(R.drawable.tg001),getResources().getDrawable(R.drawable.tg002),
getResources().getDrawable(R.drawable.tg003),getResources().getDrawable(R.drawable.tg004)};
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//设置mapview
mapview = (MapView) findViewById(R.id.mapview);
mapview.setTraffic(false);
mapview.setSatellite(false);
mapview.setStreetView(false);
mapview.setEnabled(true);
mapview.setClickable(true);
mapview.setBuiltInZoomControls(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,
60000, 0, new LocationListener() {

public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
// controller.animateTo(locationToGeopoint(location));
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub

}
});
Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
List<Overlay> list = mapview.getOverlays();
//初始化GeoPoint
g[0]=locationToGeopoint(location);
g[1]=new GeoPoint(30523109,114389224);
g[2]=new GeoPoint(30623059,115389344);
//添加了3个MarkMyMap对象 显示3个不同的图标
MarkMyMap home = new MarkMyMap(drawables[0], this);
OverlayItem item_home = new OverlayItem(locationToGeopoint(location),"IAMHOME", "Home");
home.addOverlay(item_home);
MarkMyMap tg1=new MarkMyMap(drawables[1],this);
OverlayItem item_tg1=new OverlayItem(g[1],"tg1","tg1");
tg1.addOverlay(item_tg1);
MarkMyMap tg2=new MarkMyMap(drawables[2],this);
OverlayItem item_tg2=new OverlayItem(g[2],"tg2","tg2");
tg2.addOverlay(item_tg2);
// 添加到Overlay的list当中
list.add(home);
list.add(tg1);
list.add(tg2);
controller = mapview.getController();
controller.setZoom(17);
controller.animateTo(locationToGeopoint(location));
}

// location转GeoPoint
public GeoPoint locationToGeopoint(Location location) {
// TODO Auto-generated method stub
int gX = (int) (location.getLatitude() * 1E6);
int gY = (int) (location.getLongitude() * 1E6);
GeoPoint gp = new GeoPoint(gX, gY);
return gp;
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

// 计算亮点距离
public String getDistance(GeoPoint gp){
float latitude_A=(float) (g[0].getLatitudeE6()/1E6);
float longitude_A=(float) (g[0].getLongitudeE6()/1E6);
float latitude_B=(float) (gp.getLatitudeE6()/1E6);
float longitude_B=(float) (gp.getLongitudeE6()/1E6);
float a=latitude_A-latitude_B;
float b=longitude_A-longitude_B;
float distance=(float) (2*6378.137*Math.asin(
Math.sin(a/2)*Math.sin(a/2)+Math.cos(latitude_A)*Math.cos(latitude_B)*Math.sin(b/2)*Math.sin(b/2)
));
return "与我相距 "+Math.round(distance*1000)+" 米";
}

// 处理地图标记
class MarkMyMap extends ItemizedOverlay<OverlayItem> {
ArrayList<OverlayItem> itemsOverlay = new ArrayList<OverlayItem>();
Context context;
GeoPoint tg;

public MarkMyMap(Drawable defaultMarker) {
//这里一定要加上boundCenterBottom 否则图片根本显示不出来 而不是显示效果不佳
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}

public MarkMyMap(Drawable defaultMaker, Context context) {
this(defaultMaker);//调用上一个构造器
this.context = context;
}

@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
//GeoPoint和location转换使用1E6 精度不会丢失很严重
System.out.println("itemsOverlay.get(i).getMarker(i)====>"+ itemsOverlay.get(i).getPoint());
return itemsOverlay.get(i);
}

@Override
public int size() {
// TODO Auto-generated method stub
return itemsOverlay.size();
}
//mapview里使用
@Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
//如果同一个对象 添加了很多item 这里可以显示被点击的item ID
System.out.println("index is "+index);
Toast.makeText(context, getDistance(tg), Toast.LENGTH_SHORT).show();
return true;
}

public void addOverlay(OverlayItem item) {
itemsOverlay.add(item);
System.out.println("itemsOverlay.size()=====" + itemsOverlay.size());
tg=item.getPoint();
this.populate();//通知overlay添加数据
}

}

}

你可能感兴趣的:(android,框架,Google,OS)