android - 在openstreetmap android中单击时绘制标记

标签 android location openstreetmap marker

我是 Open Street Map map 的新手。我想将标记放在我点击的 map 上。我也想删除以前的标记。请帮我。提前致谢。这是我的代码。我在这个问题上挣扎了两天。

Overlay touchOverlay = new Overlay(this) {
 ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = null;

 @Override
 protected void draw(Canvas arg0, MapView arg1, boolean arg2) {

 }

 @Override
 public boolean onSingleTapConfirmed(final MotionEvent e,
 final MapView mapView) {
 Projection proj = mapView.getProjection();
 GeoPoint loc = (GeoPoint) proj.fromPixels((int) e.getX(),
 (int) e.getY());
 String longitude = Double
 .toString(((double) loc.getLongitudeE6()) / 1000000);
 String latitude = Double
 .toString(((double) loc.getLatitudeE6()) / 1000000);
 Toast toast = Toast.makeText(getApplicationContext(), "Longitude:"
 + longitude + " Latitude: " + latitude, Toast.LENGTH_SHORT);
 toast.show();
 ArrayList<OverlayItem> overlayArray = new ArrayList<OverlayItem>();
 OverlayItem mapItem = new OverlayItem("", "", new GeoPoint(
 (((double) loc.getLatitudeE6()) / 1000000),
 (((double) loc.getLongitudeE6()) / 1000000)));

 mapItem.setMarker(marker);
 overlayArray.add(mapItem);
 if (anotherItemizedIconOverlay == null) {
 anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(
 getApplicationContext(), overlayArray, null);
 mapView.getOverlays().add(anotherItemizedIconOverlay);
 mapView.invalidate();
 } else {
 mapView.getOverlays().remove(anotherItemizedIconOverlay);
 mapView.invalidate();
 anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(
 getApplicationContext(), overlayArray, null);
 mapView.getOverlays().add(anotherItemizedIconOverlay);
 }
 return true;
 }

 };

最佳答案

最后我找到了这个问题的解决方案。这是我的解决方案。

@Override
    public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView) {

        Projection proj = mapView.getProjection();
        p = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
         proj = mapView.getProjection();
         loc = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
         String longitude = Double
         .toString(((double) loc.getLongitudeE6()) / 1000000);
         String latitude = Double
         .toString(((double) loc.getLatitudeE6()) / 1000000);
         Toast toast = Toast.makeText(getApplicationContext(),
         "Longitude: "
         + longitude + " Latitude: " + latitude, Toast.LENGTH_SHORT);
         toast.show();
        return true;
    }

private void addLocation(double lat, double lng) {
    // ---Add a location marker---

    p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

    Drawable marker = getResources().getDrawable(
            android.R.drawable.star_big_on);

    int markerWidth = marker.getIntrinsicWidth();
    int markerHeight = marker.getIntrinsicHeight();

    marker.setBounds(0, markerHeight, markerWidth, 0);

    ResourceProxy resourceProxy = new DefaultResourceProxyImpl(
            getApplicationContext());

    myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);

    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(myItemizedOverlay);

    mapView.invalidate();
}

关于android - 在openstreetmap android中单击时绘制标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19444868/

相关文章:

android - 无法解析符号 ReceiveUpdatesIntentService

安卓GPS路由系统

android - TextView.setText (Android) 导致崩溃。知道为什么吗?

java - 更改表查询不更新我的表

java - 在android中按下按钮时从文本框中读取

android - TextView 中显示的最后一个词

Android - 根据特定的时间量和特定的行进距离获取 GPS 更新

ios - 如何在 iphone 本地存储 openstreetmap 数据

react-native - React Native map UrlTile

html5-canvas - 是否可以将 Openstreets map (传单)保存为图像