更改位置并在openstreetmap中将“标记"置于“单击"上 [英] Location change and put Marker on Click in openstreetmap

查看:157
本文介绍了更改位置并在openstreetmap中将“标记"置于“单击"上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Open Street Map地图的新手.我想将标记放置在我点击过的地图上.我也想删除以前的标记.请帮我. 提前致谢.这是我的代码

I am new to Open Street Map map. I want to put the marker on map where i Tapped. I want to delete previous marker also. Please help me. Thanks in advance. Here is my code

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);
        ArrayList<OverlayItem> overlayArray = new ArrayList<OverlayItem>();
        OverlayItem mapItem = new OverlayItem("", "", new GeoPoint(
                (((double) loc.getLatitudeE6()) / 1000000),
                (((double) loc.getLongitudeE6()) / 1000000)));
        Drawable marker = null;
        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;
    }
};

推荐答案

最后,我得到了此问题的解决方案.这是我的答案.

Finally i get solution of this problem. here is my answer.

@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();
}

这篇关于更改位置并在openstreetmap中将“标记"置于“单击"上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆