如何通过触摸MapQuest来添加地图标记?

时间:2015-06-10 04:20:13

标签: android mapquest

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

我想我知道这样做的方法,即:

1将onTouchEventListener设置为MapView;

2从Listener获取位置信息;

3根据位置信息设置标记。

但是我太新了,不知道如何编写代码来实现它,尤其是第1步。我的意思是我不明白如何使用像Add marker on touched location using google map in android这样的解决方案。我的问题是更基本的。我不知道如何为我的MapView设置eventListener以及我应该使用哪个eventListener。 https://developer.mapquest.com/content/mobile/android/documentation/api/com/mapquest/android/maps/Overlay.OverlayTouchEventListener.html这个OverlayTouchEventListener似乎是一个但却无法找到它的有用指南。

那么有人可以告诉我如何详细解释这个问题吗? 例如,如何设置监听器和选择哪个监听器? 提前谢谢。

PS我使用MapQuest。

1 个答案:

答案 0 :(得分:0)

试试这个......

<强> 1。创建接口MyGeoPointListener.java

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

<强> 2。创建叠加课程:

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。在活动中实施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。结果

Add markers to map by touching in MapQuest

  1. 快乐的编码......