Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I display users current location in my app, using google maps?

How do I display users current location in my app, using google maps? I would like to show users current location using mobile network or, if available, GPS. I didn't find any tutorials that could help me or they were too hard to understand.

Here's the code of my Map.java activity, could you just give me the code I need? Thanks!

import java.util.List;

public class Map extends MapActivity
{

 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.map);

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

 MapController mc = mapView.getController();
 String coordinates[] = {"54.6738310", "025.2740480"};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 GeoPoint p = new GeoPoint(
     (int) (lat * 1E6), 
     (int) (lng * 1E6));

 mc.animateTo(p);
 mc.setZoom(13); 
 mapView.invalidate();

 List<Overlay> mapOverlays = mapView.getOverlays();
 Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
 HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
 GeoPoint point = new GeoPoint(30443769,-91158458);
 OverlayItem overlayitem = new OverlayItem(point, "Laissez les bon temps rouler!", "I'm in Louisiana!");

 GeoPoint point2 = new GeoPoint(17385812,78480667);
 OverlayItem overlayitem2 = new OverlayItem(point2, "Namashkaar!", "I'm in Hyderabad, India!");

 itemizedoverlay.addOverlay(overlayitem);
 itemizedoverlay.addOverlay(overlayitem2);

 mapOverlays.add(itemizedoverlay);
 }


 @Override
 protected boolean isRouteDisplayed()
 {
 return false;
 }
}
like image 572
Simonas Avatar asked Oct 18 '25 13:10

Simonas


1 Answers

you need to combine your mapview with a location manager. Thanks to that, you can get the user position and paint it in the map (as another overlay). Take a look into this link which is a tutorial for getting the position.

like image 63
Finuka Avatar answered Oct 20 '25 03:10

Finuka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!