Zoom on current user location displayed
Asked Answered
P

2

2

I'm using the Google Maps for Android v2. I would like to display the current user location and zoom on it. Here is the code in the FragmentActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_activity);

    mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapFragment_mapActivity)).getMap();
    if (mMap == null) {
        Toast.makeText(this, R.string.mapCreationProblem, Toast.LENGTH_LONG)
                .show();
        finish();
        return;
    }
    mMap.setMyLocationEnabled(true);
    Location currentLocation = mMap.getMyLocation();
    if(currentLocation!=null){
       LatLng currentCoordinates = new LatLng(
                            currentLocation.getLatitude(),
                            currentLocation.getLongitude());
       mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentCoordinates, 10));
    }
}

The map works fine and also the blue dot on the current location works. But it seems currentLocation is always null. So I cannot zoom on the current location.

Anyone who knows why, please?

Paulitapaulk answered 24/1, 2013 at 13:8 Comment(0)
B
8

Here is an implementation of getMyLocation() with added protection for finding the persons location. I just have this in the activity that manages the map fragment.

private Location getMyLocation() {
    // Get location from GPS if it's available
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    // Location wasn't found, check the next most accurate place for the current location
    if (myLocation == null) {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        // Finds a provider that matches the criteria
        String provider = lm.getBestProvider(criteria, true);
        // Use the provider to get the last known location
        myLocation = lm.getLastKnownLocation(provider);
    }

    return myLocation;
}

Thanks, DMan

Bodiless answered 24/1, 2013 at 21:21 Comment(1)
still getting null. However my current location is shown on the mapMenorrhagia
I
1

Try using the LocationManager:

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location currentLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Invertebrate answered 24/1, 2013 at 13:19 Comment(2)
Yes I thought about it. But I would like to know why getMyLocation() returns null.Paulitapaulk
@DanieleVitali by the looks of this https://mcmap.net/q/261188/-how-to-get-the-current-location-in-google-maps-android-api-v2 there are already issues reported against this constantly returning null. The location manager seems like your best bet. I have posted my implementation of getMyLocation() which is basically the same as above with a slight fail safe.Bodiless

© 2022 - 2024 — McMap. All rights reserved.