简体   繁体   中英

Find My Location On Google Maps

i am trying to make the application display my location on google maps with a geapoint, but it always opens the same place when opening the application on my phone, which is the value i added in the code as an initial value for the testing on the compiler. how can i make it display my location(make it change the Latitude and longitude according to my place)?

here is the code:

public class AndroidGoogleMapsActivity extends MapActivity {

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

    // Displaying Zooming controls
    MapView mapView = (MapView) findViewById(R.id.mapView);
    mapView.setBuiltInZoomControls(true);

    /**
     * Changing Map Type
     * */
     mapView.setSatellite(true); // Satellite View
    // mapView.setStreetView(true); // Street View
     mapView.setTraffic(true); // Traffic view

    /**
     * showing location by Latitude and Longitude
     * */        
    MapController mc = mapView.getController();
    double lat = Double.parseDouble("31.894178");
    double lon = Double.parseDouble("35.872694");
    GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
    mc.animateTo(geoPoint);
    mc.setZoom(15);
    mapView.invalidate(); 



    /**
     * Placing Marker
     * */
    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
    AddItemizedOverlay itemizedOverlay = 
         new AddItemizedOverlay(drawable, this);


    OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");

    itemizedOverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedOverlay);

}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

}

您永远不会调用实际的GPS,这就是为什么它将恢复为编程时使用的GPS的原因。这在其他地方也有很好的记录,但这是一个不错的起点: http : //about-android.blogspot.com/2010 /04/find-current-location-in-android-gps.html

See Location detection ; that's the easy way to do it afaik, and it worked for me. Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM