简体   繁体   中英

Google Maps Android API v2 and current location

I am using JellyBean 4.2 and testing on my device.

How to get current location on clicking myLocationButton.

getMYLocation is returning null on using Network Provider.

I am using LocationManager to get current location and referring to Google documentation https://developers.google.com/maps/documentation/android/start .

After this clicking on MyLocatiionButton is not bringing any change

You need to add a LocationSource to your map fragment. Please see this answer for a discrete example.

Do you have all permission set in the Manifest? Also, you have to enable the location service (is the gps icon in the status bar showing?). It'll take some time for the device to get the location, so see if onLocationChanged is called.

Use this code Id it does work.

private LocationManager locationManager;
private Location location;
private boolean hasGpsProvider, hasNetwrokProvider;

    public Location getLocation(Context mContext) {

        if (locationManager == null) {
            locationManager = (LocationManager) mContext
                    .getSystemService(Context.LOCATION_SERVICE);
        }

        hasGpsProvider = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        hasNetwrokProvider = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (hasGpsProvider) {
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 100, locationListenerGps);
            location = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            return location;
        }

        if (hasNetwrokProvider) {
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 100,
                    locationListenerNetwork);
            location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            return location;
        }

        return location;
    }

And call

if (location != null) {
    lat = location.getLatitude();
    lng = location.getLongitude();
}

basically what you should do is make a call to Google Directions API receive the road direction coordinates (many Latlng points), and then draw a polyline between each one of them.

U can user either overview_polyline or legs and steps.

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