2011-11-21 43 views
2

我遵循人們給我的例子之一。我在當前位置有一個標記針,並顯示一張地圖。現在我想讓用戶在他/她喜歡的任何位置放置標記(圖釘),並且初始標記(圖釘)將在用戶別針之後消失。誰能幫忙?用戶在任何地方放置標記,初始標記將消失

mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     mc = mapView.getController(); 

     // obtain gps location 
     lm = (LocationManager) 
      getSystemService(Context.LOCATION_SERVICE); 

     locationListener = new MyLocationListener(); 
    lm.requestLocationUpdates(
     // LocationManager.GPS_PROVIDER, 
      LocationManager.NETWORK_PROVIDER, 
      0, 
      0, 
      locationListener); 
} 

private class MyLocationListener implements LocationListener 
{ 

    public void onLocationChanged(Location loc) { 
     if (loc != null) { 
      Toast.makeText(getBaseContext(), 
        "Location changed: Lat: " + loc.getLatitude() + 
        " Lng: " + loc.getLongitude(), 
        Toast.LENGTH_SHORT).show(); 
     } 
     p = new GeoPoint(
       (int) (loc.getLatitude() * 1E6), 
       (int) (loc.getLongitude() * 1E6)); 
     mc.animateTo(p); 
     mc.setZoom(18); 

     // Add a location marker 
     MapOverlay mapOverlay = new MapOverlay(); 
     List<Overlay> listofOverlays = mapView.getOverlays(); 
     listofOverlays.clear(); 
     listofOverlays.add(mapOverlay); 

     // invalidate() method forces the MapView to be redrawn 
     mapView.invalidate(); 
    } 

    public void onProviderDisabled(String provider){ 
    } 

    public void onProviderEnabled(String provider){ 
    } 

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

    @Override 
    protected boolean isRouteDisplayed(){ 
     return false; 
    } 

class MapOverlay extends com.google.android.maps.Overlay { 
    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
      long when) { 
     super.draw(canvas, mapView, shadow); 
     // ---translate the GeoPoint to screen pixels--- 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(p, screenPts); 

     // ---add the marker--- 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), 
       R.drawable.marker); 
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 48, null); 

     return true; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event, MapView mapView) { 
     // ---when user lifts his finger--- 
     if (event.getAction() == 1) { 
      GeoPoint p = mapView.getProjection().fromPixels(
        (int) event.getX(), (int) event.getY()); 

      Toast.makeText(
        getBaseContext(), 
        p.getLatitudeE6()/1E6 + "," + p.getLongitudeE6() 
          /1E6, Toast.LENGTH_SHORT).show(); 

     } 
     return false; 
    } 
} 

}

+0

ontouchevent會激怒用戶,因爲即使在拖動功能後標記也會被放置 –

回答

0

這爲我工作:

Geopoint k; 

的OnCreate代碼在這裏:

mapview = (MapView) findViewById(R.id.mapView); 
     mapview.setBuiltInZoomControls(true); 
     List<Overlay> mapOverlays = mapview.getOverlays(); 
     MapOverlay mapOverlay = new MapOverlay(); 

     mapOverlays.add(mapOverlay); 

覆蓋內部類在這裏:

class MapOverlay extends com.google.android.maps.Overlay 
    { 

    @Override 
    public boolean onTap(GeoPoint p, MapView mapView) { 
     // TODO Auto-generated method stub 

     k = p; 
     mc= mapView.getController(); 
     mc.animateTo(p); 

     mapView.invalidate(); 
     latitude=p.getLatitudeE6()/1E6; 
     longitude=p.getLongitudeE6() /1E6 ; 
       Toast.makeText(Activity.this, 
        p.getLatitudeE6()/1E6 + "," + 
        p.getLongitudeE6() /1E6 , 
        Toast.LENGTH_SHORT).show(); 

       new AlertDialog.Builder(Activity.this) 
       .setTitle("Change city") 
       .setMessage("go to the new location?") 
       .setNegativeButton("NO", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
         dialog.dismiss(); 
        } 
       }) 
       .setPositiveButton("YES", new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 

        } 
       }).show(); 
     return true; 
    } 




    @Override 
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    { 
     super.draw(canvas, mapView, shadow);     
     if(k!=null) 
     { 
     //---translate the GeoPoint to screen pixels--- 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(k, screenPts); 

     //---add the marker--- 
     Bitmap bmp = BitmapFactory.decodeResource(
      getResources(), R.drawable.marker);    
     canvas.drawBitmap(bmp, screenPts.x-10, screenPts.y-34, null); 
     } 
     return true; 


    } 
+0

我意識到當我運行此地圖時,標記不會自動指向我當前的位置? – sowhat

+0

如果你想在當前位置標記,你將不得不使用itemizedoverlay類,這將只標記你的當前位置,...將很快發佈代碼 –

+0

你有msn嗎?我們在那邊談論呢? – sowhat