2011-07-30 59 views
0

我有顯示在地圖的地圖的活動,我想添加一個標記,當我觸摸屏幕上..如何在Android中觸摸地圖時添加標記?

這是我的活動......

package com.adhamenaya.android; 

import java.util.List; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.widget.Toast; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 

public class MapApp extends MapActivity { 

    private MapView mapView; 
    private MapController mapController; 
    private LocationManager locationManager; 
    private Context context; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     context=this; 
     initLayout(); 
     initMap(); 
    } 

    private void initLayout(){ 
     mapView = (MapView) findViewById(R.id.mapview); 
     //blueIcon = (Drawable)this.getResources().getDrawable(R.drawable.blueicon); 

    } 
    private void initMap(){ 
     mapView.setBuiltInZoomControls(true); 
     mapView.setStreetView(true); 
     mapController=mapView.getController(); 
     mapController.setZoom(10);// 1 is world view 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, new GeoUpdateHandler()); 

    } 


    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    class GeoUpdateHandler implements LocationListener { 

     @Override 
     public void onLocationChanged(Location location) { 
      int lat=(int)(location.getLatitude()*1E6); 
      int lng=(int)(location.getLongitude()*1E6); 

      GeoPoint point=new GeoPoint(lat,lng); 
      //GeoPoint point = new GeoPoint(19240000,-99120000); 

      mapController.animateTo(point); 
      mapController.setCenter(point); 
      Drawable redIcon = context.getResources().getDrawable(R.drawable.redicon); 
      List<Overlay> mapOverlays = mapView.getOverlays(); 
      MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon); 
      OverlayItem overlayitem = new OverlayItem(point, "Hello !", "I'm here"); 
      itemizedoverlay.addOverlay(overlayitem); 
      mapOverlays.add(itemizedoverlay); 
     } 

     @Override 
     public void onProviderDisabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
      // TODO Auto-generated method stub 

     } 
     public boolean onTouchEvent(MotionEvent event){ 
      int x=(int)event.getX(); 
      int y=(int)event.getY(); 
      Toast.makeText(context, x, Toast.LENGTH_LONG).show(); 
      GeoPoint point = mapView.getProjection().fromPixels(x, y); 

      Drawable redIcon = context.getResources().getDrawable(R.drawable.blueicon); 
      List<Overlay> mapOverlays = mapView.getOverlays(); 
      MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon); 
      OverlayItem overlayitem = new OverlayItem(point, "New Place", "I clicked here !"); 
      itemizedoverlay.addOverlay(overlayitem); 
      mapOverlays.add(itemizedoverlay); 
      return false; 
     } 

} 
} 

編輯:這是階級爲ItemizedOverlay,在那裏我已經實現中的onTap()方法,但沒有任何反應......

package com.adhamenaya.android; 

import java.util.ArrayList; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.view.MotionEvent; 
import android.widget.Toast; 

import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.MapView; 
import com.google.android.maps.OverlayItem; 

public class MyItemizedOverlay extends ItemizedOverlay{ 

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
    private Context mContext; 
    public MyItemizedOverlay(Drawable defaultMarker) { 
      super(boundCenterBottom(defaultMarker)); 
    } 

    public MyItemizedOverlay(Drawable defaultMarker, Context context) { 
      super(defaultMarker); 
      mContext = context; 
    } 
    public void addOverlay(OverlayItem overlay) { 
     mOverlays.add(overlay); 
     populate(); 
    } 
    @Override 
    protected OverlayItem createItem(int i) { 
     return mOverlays.get(i); 
    } 
    @Override 
    public int size() { 
     return mOverlays.size(); 
    } 
    @Override 
    protected boolean onTap(int index) { 
     OverlayItem item = mOverlays.get(index); 
     AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
     dialog.setTitle(item.getTitle()); 
     dialog.setMessage(item.getSnippet()); 
     dialog.show(); 
     return true; 
    } 
    @Override 
     public boolean onTouchEvent(MotionEvent event, MapView mapView) { 
     int x=(int)event.getX(); 
      int y=(int)event.getY(); 
      Toast.makeText(mContext, x, Toast.LENGTH_LONG).show(); 
      return super.onTouchEvent(event, mapView); 
     } 


} 

回答

2

Hello, MapView!教程包括顯示在MapView標記。在教程中,標記會在活動開始時立即添加,但您也可以在稍後添加標記,例如用戶觸摸屏幕時。如果您嘗試這種方法,您可能會想要在ItemizedOverlay的子類中覆蓋onTap

更新:

如果按照教程,你有你自己的ItemizedOverlay的子類。 onTap是它的一種方法。現在我看文檔,不幸的是onTap(GeoPoint p, MapView mapView)沒有公開或保護,所以你不能覆蓋它。

你可以做什麼,有另一個覆蓋層,僅用於檢測水龍頭。而不是繼承ItemizedOverlay,子類Overlay

private class TapOverlay extends Overlay { 
    public boolean onTap(GeoPoint p, MapView mapView) { 
     // code that adds item in your ItemizedOverlay 
     // goes here 

     return true; 
    } 
} 
+0

我該如何實現onTap?以及如何在模擬器中測試它? – Adham

+0

更新了我的答案。 –

相關問題