如何在MapQuest中通过触摸添加标记到地图?

How to add markers to map by touching in MapQuest?

现在我可以显示程序中定义的地图和标记,但我需要在触摸的位置添加标记。

我想我知道这样做的方法是:

1 为 MapView 设置类似 onTouchEventListener 的东西;

2 从Listener获取位置信息;

3 根据位置信息设置marker

但是我对此太陌生了,不知道如何编写代码来实现它,尤其是第 1 步。我的意思是我不明白如何使用像 Add marker on touched location using google map in Android .My problem is more fundamental. I dont know how to set eventListener for my MapView and which eventListener should I use. https://developer.mapquest.com/content/mobile/android/documentation/api/com/mapquest/android/maps/Overlay.OverlayTouchEventListener.html 这样的解决方案,这个 OverlayTouchEventListener 似乎是那个,但找不到有用的指南。

有人能详细告诉我怎么做吗?
Eg.How设置监听器和选择哪个监听器?

试试这个...

1.创建接口MyGeoPointListener.java

  public interface MyGeoPointListener {
    public void GetGeoPoint(GeoPoint geopoint);
  }

2。创建叠加层 class:

import com.mapquest.android.maps.ItemizedOverlay;
import com.mapquest.android.maps.OverlayItem;
class DynamicMarkerOverlay extends ItemizedOverlay<OverlayItem> {

    private boolean isPinch = false;
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private MyGeoPointListener geoPointListener;

    public DynamicMarkerOverlay(Drawable defaultMarker,
            MyGeoPointListener geoPointListener) {
        super(boundCenterBottom(defaultMarker));
        this.geoPointListener = geoPointListener;
    }

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

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

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

    @Override
    public boolean onTap(GeoPoint geoPoint, MapView map) {

        if (isPinch) {
            return false;
        } else {

            if (geoPoint != null) {
                if (null != geoPointListener) {
                    geoPointListener.GetGeoPoint(geoPoint);
                }
                return true;
            } else {
                if (null != geoPointListener) {
                    geoPointListener.GetGeoPoint(null);
                }
                return false;
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        int fingers = event.getPointerCount();
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            isPinch = false; // Touch DOWN, don't know if it's a pinch yet
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE && fingers == 2) {
            isPinch = true; // Two fingers, def a pinch
        }
        return super.onTouchEvent(event, mapView);
    }

}

3。在 activity

中实现 MyGeoPointListener

喜欢,

public class MainActivity extends MapActivity implements MyGeoPointListener {

4。覆盖 MyGeoPointListener:

.....

@Override
public void GetGeoPoint(GeoPoint geopoint) {
    if (null != geopoint) {
        String msg = "Lat: " + geopoint.getLatitudeE6() / 1E6 + " - "
                + "Lon: " + geopoint.getLongitudeE6() / 1E6;
        Toast toast = Toast.makeText(MyLocationMap.this, msg,
                Toast.LENGTH_SHORT);
        toast.show();

        List<Overlay> mapOverlays = myMap.getOverlays();
        OverlayItem overlayitem = new OverlayItem(geopoint, "address_name",
                "address");
        Drawable icon = getResources().getDrawable(
                R.drawable.location_marker);
        DynamicMarkerOverlay customoverlay = new DynamicMarkerOverlay(icon,
                this);
        customoverlay.addOverlay(overlayitem);
        mapOverlays.add(customoverlay);
        myMap.getController().animateTo(geopoint);
    }
 }

 .....

5.在 OnCreate()

......
public MapView myMap;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_map_layout);

    .......
    .......

    myMap = (MapView) findViewById(R.id.map);

    Drawable marker = getResources().getDrawable(R.drawable.ic_launcher);

    marker.setBounds(0, 0, marker.getIntrinsicWidth(),
            marker.getIntrinsicHeight());

    myMap.getOverlays().add(new DynamicMarkerOverlay(marker, this));
   
   ......
}

6.结果: