添加自定义标记到地图 - 安卓(Adding a custom marker to map - An

2019-06-23 10:47发布

我当前使用的MapView这显示谷歌地图给用户的应用程序。

我一直在试图留下一个标记,使用此代码在地图上,

public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        if (event.getAction() == 1) {
            GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),
                    (int) event.getY());
            DemoOverlay demoOverlay = new DemoOverlay(); 
            mapView.getOverlays().add(demoOverlay);

            mapView.invalidate();
        }
        return false;
    }

我有一个演示覆盖成立,它将一个标记在地图上,但是当我触碰从未出现的标志画面。

任何帮助,将不胜感激 !!!

Answer 1:

GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
Drawable srcdrawable = getApplicationContext().getResources().getDrawable(R.drawable.pin_blue);
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable, getApplicationContext());
OverlayItem srcoverlayitem = new OverlayItem(p, "Hello!", "This is your Location.");
srcitemizedOverlay.addOverlay(srcoverlayitem);
mapView.getOverlays().clear();
mapView.getOverlays().add(srcitemizedOverlay);
mapController.animateTo(srcpoint);
mapController.setZoom(16);

使用在ontouch方法上面的代码也是用下面CustomItemizedOverlay.java类

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

    private Context context;

    public CustomItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
    }

    public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
        this(defaultMarker);
        this.context = context;
    }

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

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

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

}


Answer 2:

Android的mapviewballoons真的是最好的地图图书馆为Android之一。 你为什么不拿一试? :)



Answer 3:

这里是完整的例子把多个标记在地图和

还如何绘制两个引脚之间的路径...

可能对你有帮助...



Answer 4:

修改代码,这样---

public boolean onTouchEvent(MotionEvent event, MapView mapView) 
   {   

    if (event.getAction() == 1) { 

          p = mapView.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());
          Toast.makeText(getBaseContext(),p.getLatitudeE6() / 1E6 + "," +p.getLongitudeE6() /1E6 ,Toast.LENGTH_SHORT).show();

              List<Overlay> mapOverlays = mapView.getOverlays();
              Drawable drawable = this.getResources().getDrawable(R.drawable.map_point);
              AddItemizedOverlay itemizedOverlay =
              new AddItemizedOverlay(drawable, this);

              OverlayItem overlayitem = new OverlayItem(p, "", "");
              itemizedOverlay.addOverlay(overlayitem);
              mapOverlays.add(itemizedOverlay);

          return true;
    }
     return false;
}        

做一个覆盖分类 -

AddItemizedOverlay.class--

   public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

       private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

       private Context context;

       public AddItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
       }

       public AddItemizedOverlay(Drawable defaultMarker, Context context) {
            this(defaultMarker);
            this.context = context;
       }

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

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

       @Override
       protected boolean onTap(int index) {
          Log.e("Tap", "Tap Performed");
          return true;
       }

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

    }

它会显示点当u触摸屏幕。



Answer 5:

感谢您的建议,但我已经决定了,因为他们覆盖正是我需要遵循这些教程, http://thenewboston.org/watch.php?cat=6&number=133

这部影片特别是覆盖我的问题之一,

http://thenewboston.org/watch.php?cat=6&number=141



文章来源: Adding a custom marker to map - Android