Why are my overlays on a mapview not shown?

旧时模样 提交于 2019-12-11 02:46:49

问题


I followed the instructions from the google hellomapview tutorial. I get a working mapview etc. But the two items that are added to the map are not shown. It seems they are there somewhere because tapping at the specified location shows the message that was added to the items.

Edit

Here is my source code. It should be very close to the google tutorial source code.

public class MapOverlay extends ItemizedOverlay<OverlayItem> {

private List<OverlayItem> overlays = new ArrayList<OverlayItem>();
private Context context;

public MapOverlay(Drawable defaultMarker, Context context) {
    super(defaultMarker);
    overlays = new ArrayList<OverlayItem>();
    this.context = context;
}

@Override
protected OverlayItem createItem(int i) {
    return overlays.get(i);
}

@Override
public int size() {
     return overlays.size();
}

public void addOverlay(OverlayItem overlay) {
    overlays.add(overlay);
    this.populate();
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = overlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

}

public class MapsActivity extends MapActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    MapOverlay overlay = new MapOverlay(this.getResources().getDrawable(
            R.drawable.androidmarker), this);

    overlay.addOverlay(new OverlayItem(new GeoPoint(19240000,-99120000), "Blubb", "See?"));
    mapView.getOverlays().add(overlay);
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

Is the source code from the google tutorial available somewhere?


回答1:


The problem is that I forgot to set the bounds of the drawable. It seems that if the mapview doesn't know how to align the image it won't show it at all.

I changed the first line in my constructor from:

super(defaultMarker);

to

super(boundCenterBottom(defaultMarker));

and know its working perfect.




回答2:


At the same time, I have no idea how to help you directly.

Here are links to various editions of a project that definitely work with overlays, perhaps they will help.



来源:https://stackoverflow.com/questions/2357623/why-are-my-overlays-on-a-mapview-not-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!