我正在尝试在地图视图上添加多个标记,但我无法使其工作,
当我评论调用 LoadMarkers() 的方法时,它似乎甚至没有加载地图视图。你能告诉我我的代码有什么问题吗?
public class MyATMLocatorActivity extends MapActivity {
/** Called when the activity is first created. */
private MapView mapView;
private MapController myMapController;
private GeoPoint myGeoPoint;
private LocationManager myLocationManager;
private LocationListener myLocationListener;
public static Context context;
private MyItemizedOverlay myItemizedOverlay = null;
private MyLocationOverlay myLocationOverlay = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
myMapController = mapView.getController();
this.LoadMarkers();
}
这是加载标记的方法..
private void LoadMArkers() {
mapView.getOverlays().clear();
mapView.postInvalidate();
OverlayItem overlayItem = null;
Drawable myATMPic = getResources().getDrawable(R.drawable.atmicon);
myATMPic.setBounds(0, 0, myATMPic.getIntrinsicWidth(), myATMPic.getIntrinsicHeight());
// Super España
myItemizedOverlay = new MyItemizedOverlay(myATMPic);
myGeoPoint = new GeoPoint((int) (-25.353043), (int) (-57.444495));
overlayItem = new OverlayItem(myGeoPoint, "Supermercado Espana","Capiatá");
myItemizedOverlay.addOverlay(overlayItem);
mapView.getOverlays().add(myItemizedOverlay);
// Martín Ledesma
myItemizedOverlay = new MyItemizedOverlay(myATMPic);
myGeoPoint = new GeoPoint((int) (-25.353974), (int) (-57.445214));
overlayItem = new OverlayItem(myGeoPoint, "Martín Ledesma", "Capiatá");
myItemizedOverlay.addOverlay(overlayItem);
mapView.getOverlays().add(myItemizedOverlay);
mapView.postInvalidate();
}
这是我的 ItemizedOverlay() 类
public class MyItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {
private Context mContext;
private ArrayList<OverlayItem> myOverlaysArray = new ArrayList<OverlayItem>();;
private GeoPoint geoPoint = null;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
myOverlaysArray.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return myOverlaysArray.get(i);
}
// Removes overlay item i
public void removeItem(int i) {
myOverlaysArray.remove(i);
populate();
}
// Returns present number of items in list
@Override
public int size() {
return myOverlaysArray.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlaysArray.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
geoPoint = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(geoPoint, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected boolean onTap(int index) {
OverlayItem item = myOverlaysArray.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}