2012-08-28 33 views
0

這是我的主要活動類:如何在銷釘添加到google maps android後出現彈出式菜單?

public class MyActivity extends MapActivity implements LocationListener, OnClickListener{ 

private MapView mapView; 
private MyItemizedOverlay itemizedOverlay; 
Button route; 
Drawable drawableDot; 
static GeoPoint src; 
static GeoPoint des; 

Canvas canvas; 
Paint paint; 
boolean shadow; 

private LocationManager locManager; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    route = (Button) findViewById(R.id.cmd_submit); 
    route.setOnClickListener(this); 



    //fetch the map view from the layout 
    mapView = (MapView) findViewById(R.id.myMapView); 

    //make available zoom controls 
    mapView.setBuiltInZoomControls(true); 

    //latitude and longitude of Rome 
    double lat = 41.889882; 
    double lon = 12.479267; 

    //create geo point 
    GeoPoint point = new GeoPoint((int)(lat * 1E6), (int)(lon *1E6)); 

    //get the MapController object 
    MapController controller = mapView.getController(); 

    //animate to the desired point 
    controller.animateTo(point); 

    //set the map zoom to 13 
    // zoom 1 is top world view 
    controller.setZoom(13); 

    //invalidate the map in order to show changes 
    mapView.invalidate(); 

    // Use the location manager through GPS 
    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
      0, this); 

    //get the current location (last known location) from the location manager 
    Location location = locManager 
      .getLastKnownLocation(LocationManager.GPS_PROVIDER); 


     //if location found display as a toast the current latitude and longitude 
    if (location != null) { 

     Toast.makeText(
       this, 
       "Current location:\nLatitude: " + location.getLatitude() 
         + "\n" + "Longitude: " + location.getLongitude(), 
       Toast.LENGTH_LONG).show(); 

     point = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude() *1E6)); 

     controller.animateTo(point); 

    } else { 

     Toast.makeText(this, "Cannot fetch current location!", 
       Toast.LENGTH_LONG).show(); 
    } 

    //when the current location is found – stop listening for updates (preserves battery) 
    locManager.removeUpdates(this); 

    // fetch the drawable - the pin that will be displayed on the map 
    Drawable drawable = this.getResources().getDrawable(R.drawable.marker); 

    // create and add an OverlayItem to the MyItemizedOverlay list 
    OverlayItem overlayItem = new OverlayItem(point, "", ""); 

    itemizedOverlay = new MyItemizedOverlay(drawable,this); 

    itemizedOverlay.setGestureDetector(new GestureDetector(new MyGestureDetector())); 

    itemizedOverlay.addOverlay(overlayItem); 


    // add the overlays to the map 
    mapView.getOverlays().add(itemizedOverlay); 
    mapView.invalidate(); 

    //when the current location is found – stop listening for updates (preserves battery) 
    locManager.removeUpdates(this); 
} 

@Override 
protected boolean isRouteDisplayed() { 
    return false; 

} 


class MyGestureDetector extends SimpleOnGestureListener implements OnClickListener{ 

ArrayList<GeoPoint> points =new ArrayList<GeoPoint>(); 


@Override 
public boolean onSingleTapConfirmed(MotionEvent event) { 

    // fetch the correspondent point from the map 

    Log.d("A condition", "d5al l methodaaya"); 
    GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY()); 
    points.add(p); 

    // create an overlay item and clear all others 
    OverlayItem o = new OverlayItem(p, null, null); 
    // itemizedOverlay.clear(); 
    itemizedOverlay.addOverlay(o); 


    // add the overlay item 
    //mapView.getOverlays().clear(); 
    mapView.getOverlays().add(itemizedOverlay); 
    mapView.invalidate(); 

    Geocoder geoCoder = new Geocoder(getBaseContext(), 
      Locale.getDefault()); 

    // get the address based on the coordinates 
    try { 
     List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6, 1); 

     String addressString = ""; 
     if (addresses.size() > 0) { 
      for (int i = 0; i < addresses.get(0) 
        .getMaxAddressLineIndex(); i++) 
       addressString += addresses.get(0).getAddressLine(i) 
         + " - "; 
     } 

     Toast.makeText(getBaseContext(), addressString, 
       Toast.LENGTH_SHORT).show(); 


     //Add action item 
     ActionItem addAction = new ActionItem();  
     addAction.setTitle("Add"); 
     addAction.setIcon(getResources().getDrawable(R.drawable.marker)); 

     //Accept action item 
     ActionItem accAction = new ActionItem(); 
     accAction.setTitle("Accept"); 
     accAction.setIcon(getResources().getDrawable(R.drawable.marker)); 

     //Upload action item 
     ActionItem upAction = new ActionItem();   
     upAction.setTitle("Upload"); 
     upAction.setIcon(getResources().getDrawable(R.drawable.marker)); 

     final QuickAction mQuickAction = new QuickAction(getBaseContext()); 

     mQuickAction.addActionItem(addAction); 
     mQuickAction.addActionItem(accAction); 
     mQuickAction.addActionItem(upAction); 

現在,這裏的問題,如果我嘗試下面的OnClickListener與路徑(這是一個按鈕),它工作正常,但如果我嘗試對整個地圖它絕對沒有。 (如果有,它實現了那簡直太好了引腳setOnClickListener另一種完全不同的方式!)

//   mapView.setOnClickListener(new View.OnClickListener() { 
//    @Override 
//    public void onClick(View v) { 
//     mQuickAction.show(v); 
//    } 
//    }); 


     route.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mQuickAction.show(v); 
      } 
      }); 

     //setup the action item click listener 
     mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() { 

       public void onItemClick(int pos) { 
       if (pos == 0) { //Add item selected 
        Toast.makeText(MyActivity.this, "Add item selected", Toast.LENGTH_SHORT).show(); 
       } else if (pos == 1) { //Accept item selected 
        Toast.makeText(MyActivity.this, "Accept item selected", Toast.LENGTH_SHORT).show(); 
       } else if (pos == 2) { //Upload item selected 
        Toast.makeText(MyActivity.this, "Upload items selected", Toast.LENGTH_SHORT).show(); 
       } 
      } 

       @Override 
       public void onItemClick(QuickAction source, int pos, 
         int actionId) { 
        if (pos == 0) { //Add item selected 
          Toast.makeText(MyActivity.this, "Add item selected",  Toast.LENGTH_SHORT).show(); 
         } else if (pos == 1) { //Accept item selected 
          Toast.makeText(MyActivity.this, "Accept item selected", Toast.LENGTH_SHORT).show(); 
         } else if (pos == 2) { //Upload item selected 
          Toast.makeText(MyActivity.this, "Upload items selected", Toast.LENGTH_SHORT).show(); 
         } 

       } 
     }); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return true; 
} 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 
    return super.onFling(e1, e2, velocityX, velocityY); 
} 

@Override 
public boolean onDown(MotionEvent e) { 
    return false; 
} 

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 

} 
} 


} 

而且,我想怎麼在這裏建議,但沒有奏效。 Android set Listener for marker on google map 任何幫助將不勝感激,在此先感謝!

+0

Wait,s o您是否希望點擊標記時打開彈出式菜單,或者是否希望在將圖釘添加到地圖後自動顯示彈出式菜單? –

+0

理想情況下,我希望它打開時,我點擊標記,但如果它自動出現,它也很好! – Salma

回答

0

如果你想在彈出菜單中,當用戶點擊該標記打開,然後首先,刪除此代碼:

itemizedOverlay.setGestureDetector(new GestureDetector(new MyGestureDetector())); 

我覺得你不知道和Ontap()方法的ItemizedOverlay如此使用它而不是使用GestureDetector,因爲我不認爲你可以使用不是視圖的Android類的GestureDetector(我已經提供了下面的代碼),你將不得不在下面的代碼中使用你的MyItemizedOverlay類的

@Override 
public boolean onTap(int index) 
{ 
//Put the code for the creation of a pop-up menu, I'd suggest using a custom Dialog 
//box and putting options in it. However, you seem to be using ActionItems, I'm 
//not sure what they are. 

}