Show my current location in Google API 10 for Andr

2019-08-30 01:40发布

问题:

I'm having problem with showing my current location in this code. Can you please tell me how to add code that will zoom and show my current location.

public class MapaActivity extends MapActivity
{

 @Override
 public void onCreate(Bundle savedInstanceState)
 {

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

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

 List<Overlay> mapOverlays = mapView.getOverlays();
 Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
 HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);

 GeoPoint point = new GeoPoint(44900101,14839100);
 OverlayItem overlayitem = new OverlayItem(point, "Pin","Test pin");

 itemizedoverlay.addOverlay(overlayitem);



 mapOverlays.add(itemizedoverlay);

回答1:

For your GeoPoint you can use:

LocationManager locationManager;
        locationManager=(LocationManager) getSystemService(LOCATION_SERVICE);
        Location location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        GeoPoint point=new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));

And at the end add:

 mapView.getController().animateTo(point);

In the Manifest file add permission:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>