在查看了许多不同的指南/教程之后,当用户点击地图上的叠加层(标记)时,我发现自己迷失了如何实现简单的警告框/工具提示。
在我已经拥有的代码中实现它时,我看到的所有指南都令人困惑。标记来自外部JSON,我在其中获得纬度/经度来创建并将标记放置在地图上。到目前为止一切顺利,我有所有的标记,但是当用户点击它时,我似乎无法找到实现警报框/工具提示的最佳方法。
我的代码发布在下面...我知道它有很多东西,但任何帮助都表示赞赏。非常感谢你!
package ca.transcontinental.android.vanilla.demo;
import (...)
public class GoogleMapActivity extends MapActivity implements LocationListener {
MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_map_layout);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
this.mapView = (MapView) findViewById(R.id.mapview);
this.mapView.setBuiltInZoomControls(true);
this.mapView.getController().setZoom(10);
}
private BaseApplication getBaseApplication() {
return (BaseApplication)getApplication();
}
public void onLocationChanged(Location location) {
if (location != null) {
String sLat = String.valueOf(location.getLatitude());
String sLng = String.valueOf(location.getLongitude());
try {
getBaseApplication().debug("GPS: lat="+sLat + ", lng="+sLng);
RestQueryEngine.getInstance().setup("http://example.com/JSONPublicationService.svc", "key", getBaseApplication().getOrgCode(), getBaseApplication().getBannerCode());
StoreList list = StoreList.getStoreListByGeopos(sLat, sLng);
for(Store store: list){
System.out.println(store);
MapOverlay overlay = new MapOverlay(Double.parseDouble(store.getLatitude()), Double.parseDouble(store.getLongitude()), "");
mapView.getOverlays().add(overlay);
}
mapView.invalidate();
}
catch (Throwable e) {
e.printStackTrace();
getBaseApplication().showExceptionMessage(e);
}
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.removeUpdates(this);
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
class MapOverlay extends Overlay {
private GeoPoint locpoint;
private String label;
public MapOverlay(GeoPoint geoPoint, String name) {
this.locpoint = geoPoint;
this.label = name;
}
public MapOverlay(double lat, double lon, String name) {
this(new GeoPoint((int)(lat*1E6), (int)(lon*1E6)), name);
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
Point screenPoint = new Point();
mapView.getProjection().toPixels(this.locpoint, screenPoint);
Bitmap markerImage = BitmapFactory.decodeResource(getResources(), R.drawable.androidmarker);
canvas.drawBitmap(markerImage, screenPoint.x - markerImage.getWidth() / 2, screenPoint.y - markerImage.getHeight() / 2, null);
return true;
}
}
}
答案 0 :(得分:0)
我这样做的方法是使用OverlayItem而不是Overlay,并创建一个扩展ItemizedOverlay的类
class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<MapOverlay> mOverlays = new ArrayList<MapOverlay>();
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addItem(MapOverlay item) {
mOverlays.add(item);
}
@Override
protected MapOverlay createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
// Handle tap for the given overlay item based on this index
return super.onTap(index);
}
}
这样做有好处,你将使用R.drawable.androidmarker
作为默认标记,而不必在Overlay中实现draw
方法,这是为你完成的。