2013-10-16 56 views
3

我是新的打開街道地圖地圖。我想把標記放在我點擊的地圖上。我也想刪除以前的標記。請幫幫我。 在此先感謝。這裏是我的代碼位置改變,並把標記點擊openstreetmap

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; 
    } 
}; 

回答

3

最後我得到了這個問題的解決方案。這是我的答案。

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