Kristijan Drača
Kristijan Drača

Reputation: 614

Show my current location in Google API 10 for Android

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);

Upvotes: 0

Views: 320

Answers (1)

Mohit Verma
Mohit Verma

Reputation: 3025

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"/>

Upvotes: 1

Related Questions