在osmdroid中获取地图上点击位置的纬度和经度

6

我已经成功编写了一个Android应用程��,使用它来跟踪用户位置并在地图上展示标记。以下是相关代码:

public class MainActivity extends Activity implements LocationListener {

public MapView mapView;
private LocationManager locationManager;
MyItemizedOverlay myItemizedOverlay = null;
Drawable marker;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mapView = (MapView) this.findViewById(R.id.mapview);
    mapView.setUseDataConnection(false);
    mapView.setClickable(true);
    mapView.setBuiltInZoomControls(true);
    mapView.setMultiTouchControls(true);
    mapView.setUseDataConnection(false);
    mapView.setFocusable(true);
    mapView.setFocusableInTouchMode(true);

    mapView.getController().setZoom(16); // set initial zoom-level, depends
                                            // on your need
    marker = getResources().getDrawable(android.R.drawable.star_on);
    int markerWidth = 1;
    int markerHeight = 1;
    marker.setBounds(0, markerHeight, markerWidth, 0);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, this); // You can also use LocationManager.GPS_PROVIDER and
                        // LocationManager.PASSIVE_PROVIDER

}

@Override
public void onLocationChanged(Location location) {

    int lat = (int) (location.getLatitude() * 1E6);
    int lng = (int) (location.getLongitude() * 1E6);
    GeoPoint point = new GeoPoint(lat, lng);
    mapView.getController().setCenter(point);

    mapView.getController().animateTo(point);
    mapView.invalidate();

    ResourceProxy resourceProxy = new DefaultResourceProxyImpl(
            getApplicationContext());
    myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);

    mapView.getOverlays().clear();
    mapView.getOverlays().add(myItemizedOverlay);

    myItemizedOverlay.addItem(point, "myPoint1", "myPoint1");

    TextView tv1 = (TextView) findViewById(R.id.myLat);
    tv1.setText("Lat is " + lat);

    TextView tv2 = (TextView) findViewById(R.id.myLong);
    tv2.setText("Long is " + lng);

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {
}

现在我需要一种方法来获取地图上所点击位置的纬度和经度信息。 我的意思是,我需要在osmdroid中像下面这段Google Map API代码一样实现相似功能。

google.maps.event.addListener(map, 'click', function(event) {
YourHandler(event.latLng);});

看看这个,它和你的问题类似。 - viduka
https://dev59.com/p3jZa4cB1Zd3GeqPZAhO - Rajal
2个回答

4

您需要创建一个覆盖层(Overlay),并重写onSingleTapConfirmed方法。

请尝试以下代码:

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) {

        final Drawable marker = getApplicationContext().getResources().getDrawable(R.drawable.markericon);
        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);
        System.out.println("- Latitude = " + latitude + ", Longitude = " + longitude );
        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);
        }
  //      dlgThread();
        return true;
    }
};
mapView.getOverlays().add(touchOverlay);

1
如果您实现了MapEventsReceiver,则可以使用singleTapConfirmedHelper()函数,该函数直接为您提供所单击位置的GeoPoint对象。以下是一个示例:
public class MapActivity extends AppCompatActivity implements MapEventsReceiver {

    ...

    @Override
    public boolean singleTapConfirmedHelper(GeoPoint p) {
        mapController.animateTo(p);
        return true;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接