2012-05-21 50 views
1

我寫了一個地圖查看器,我想從用戶處獲取緯度和經度,然後向他顯示位置。 我使用這個工作的菜單。但我不知道怎麼給他看點什麼。這是我的代碼。在地圖上設置經度和緯度

public class main extends MapActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    MapView view = (MapView) findViewById(R.id.themap); 
    view.setBuiltInZoomControls(true); 

    final MapController control = view.getController(); 

    LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    LocationListener listener = new LocationListener() { 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

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

     } 

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

     } 

     @Override 
     public void onLocationChanged(Location location) { 
      control.setCenter(new GeoPoint((int)location.getLatitude(), (int)location.getLongitude())); 
     } 
    }; 
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    return super.onOptionsItemSelected(item); 
} 

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

}

,所以我有2個問題。首先,我如何爲我的位置創建菜單。我必須使用上下文菜單或其他東西。 秒,如果我獲得地點,我可以直接輸入他們到我的經度和緯度? 在我的代碼中我使用用戶的默認位置。

回答

0

如果妳只是想顯示你當前的位置喲可以使用MyLoicationOverlay類:

miPunto = new MyLocationOverlay(this, eMapa); 
      eMapa.getOverlays().add(miPunto); 
      miPunto.enableCompass(); 
      miPunto.enableMyLocation(); 

如果你願意使用這個,你必須得到你的位置,然後使用itemizedoverlay

0

這是我的代碼,它工作正常,我

Drawable marker=getResources().getDrawable(R.drawable.mapmarker); 
    marker.setBounds((int) (-marker.getIntrinsicWidth()/2), 
          -marker.getIntrinsicHeight(), 
          (int) (marker.getIntrinsicWidth()/2), 
          0); 

    InterestingLocations funPlaces = 
        new InterestingLocations(marker); 

添加您覆蓋項目在這裏。

mapView.getOverlays().add(funPlaces); 

class InterestingLocations extends ItemizedOverlay { 
    private ArrayList<OverlayItem> locations = 
      new ArrayList<OverlayItem>(); 
    private GeoPoint center = null; 

    public InterestingLocations(Drawable marker) 
    { 
     super(marker); 

     // Put your custom Lat, Long in here , 
     GeoPoint disneySevenLagoon = 
      new GeoPoint((int)(28.410067*1000000), 
         (int)(-81.583699*1000000)); 

     locations.add(new OverlayItem(disneySevenLagoon , 
         "Seven Seas Lagoon", "Seven Seas Lagoon")); 
     populate(); 
    } 
相關問題