在使用 Maps API 时,我是初学者,所以请多多包涵,我知道还有很多其他帖子处理相同的问题,但我仍然卡住了。
我已经能够在我的地图上放置一些叠加图像。我现在唯一遇到的问题是,当我缩小足够的级别时,我不知道如何使它们消失,因为这会导致叠加图像挤在一起并重叠 - 基本上使它们在那个级别无用。因此,对于如何使它们出现(在缩放级别 18 之后)的任何帮助将不胜感激。
我曾尝试使用缩放侦听器和 if 语句,但它没有任何变化 - 很可能是因为我不知道我需要在哪里实现它和/或启用它需要哪些其他方法。另外,我不确定如何实现 draw() 方法,因为许多其他人已经使用它来使其缩放和消失。
编辑:
这些是我迄今为止成功执行的两个类(在应用答案之后):
Map.java文件:
public class Map extends com.google.android.maps.MapActivity implements
OnOverlayGestureListener {
private boolean mShowOverlays = true;
private MapView mMapView;
MapView mapView;
MapController mapController;
private void setOverlayVisibility() {
boolean showOverlays = mMapView.getZoomLevel() > 18;
if (showOverlays != mShowOverlays) {
mShowOverlays = showOverlays;
for (Overlay overlay : mMapView.getOverlays()) {
if (overlay instanceof ItemOverlay) {
((ItemOverlay) overlay).setVisible(showOverlays);
}
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(17);
boolean showOverlays = mMapView.getZoomLevel() > 18;
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable lot = this.getResources().getDrawable(R.drawable.lot);
ItemOverlay parking_lot = new ItemOverlay(lot, this);
GeoPoint point1 = new GeoPoint(43806622, -79219797);
OverlayItem parking = new OverlayItem(point1, "Shopping Center","Parking Lot");
parking_lot.addOverlayItems(parking);
mapOverlays.add(parking_lot);
Drawable logo = this.getResources().getDrawable(R.drawable.entrance);
ItemOverlay ent = new ItemOverlay(logo, this);
GeoPoint start = new GeoPoint(43805697, -79221031);
mapController.setCenter(start);
OverlayItem welcome = new OverlayItem(start, "Welcome", " ");
ent.addOverlayItems(welcome);
mapOverlays.add(ent);
public <ZoomEvent> boolean onZoom(ZoomEvent ze, ManagedOverlay mo) {
setOverlayVisibility();
return true;
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
ItemOverlay.java文件:
public class ItemOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
private boolean visible = true;
private boolean mVisible = true;
public void setVisible(boolean value) {
mVisible = value;
}
public boolean isVisible() {
return mVisible ;
}
@Override
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
if (mVisible) {
super.draw(canvas, mapView, shadow);
}
}
public ItemOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
mContext = context;
}
public void addOverlayItems(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();
}
@Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}