Java 叠加点击时显示工具提示/警报
在阅读了许多不同的指南/教程之后,我发现自己对如何在用户单击地图上的覆盖标记时实现一个简单的警告框/工具提示一无所知 在我已经拥有的代码中实现它时,我看到的所有指南都令人困惑。这些标记来自一个外部JSON,我从中获得创建标记并将其放置在地图上的纬度/经度。到目前为止还不错,我有所有的标记,但我似乎找不到最好的方法来实现一个警告框/工具提示,当用户点击它们 我的代码贴在下面。。。我知道这是很多东西,但任何帮助都是感激的。非常感谢 软件包ca.transcontinetal.android.vanilla.demoJava 叠加点击时显示工具提示/警报,java,android,google-maps,Java,Android,Google Maps,在阅读了许多不同的指南/教程之后,我发现自己对如何在用户单击地图上的覆盖标记时实现一个简单的警告框/工具提示一无所知 在我已经拥有的代码中实现它时,我看到的所有指南都令人困惑。这些标记来自一个外部JSON,我从中获得创建标记并将其放置在地图上的纬度/经度。到目前为止还不错,我有所有的标记,但我似乎找不到最好的方法来实现一个警告框/工具提示,当用户点击它们 我的代码贴在下面。。。我知道这是很多东西,但任何帮助都是感激的。非常感谢 软件包ca.transcontinetal.android.vani
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;
}
}
}
我这样做的方式是使用OverlayItem而不是Overlay,并创建一个扩展ItemizeOverlay的类
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方法,因为这是为您完成的