Roger W
Roger W

Reputation: 107

Android Google Maps setMyLocationEnabled(true)

I'm working through a tutorial and my app is basically working except for one small detail. I have an Options menu to demonstrate things like showing the map as a Satellite view, zooming to specific places etc.

The tutorial has two menu options as shown below which effectively need to be used in the order shown in order to zoom the map to where I'm currently located. I wanted to make the first item redundant (getcurrentlocation) by adding in the same functionality into the second menu item (show current loaction) but this causes the app to crash.

Does anyone have any ideas why and how to overcome it. My thanks.

Code snippet follows

    case R.id.menu_getcurrentlocation:
        // ---get your current location and display a blue dot---
        map.setMyLocationEnabled(true);

        break;

    case R.id.menu_showcurrentlocation:
            // Adding the next line breaks the app.
            // map.setMyLocationEnabled(true);
        Location myLocation = map.getMyLocation();
        LatLng myLatLng = new LatLng(myLocation.getLatitude(),myLocation.getLongitude());


        CameraPosition myPosition = new CameraPosition.Builder().target(myLatLng).zoom(17).bearing(90).tilt(30).build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));

        break;

Upvotes: 1

Views: 10085

Answers (2)

sjain
sjain

Reputation: 23354

From developers doc,

A GoogleMap can only be acquired using getMap() when the underlying maps system is loaded and the underlying view in the fragment exists. This class automatically initializes the maps system and the view; however you cannot be guaranteed when it will be ready because this depends on the availability of the Google Play services APK. If a GoogleMap is not available, getMap() will return null.

  • This can happen if the fragment lifecyle have not gone through onCreateView(LayoutInflater, ViewGroup, Bundle) yet.
  • This can also happen if Google Play services is not available.

Further pointing out,

If Google Play services becomes available afterwards and the fragment have gone through onCreateView(LayoutInflater, ViewGroup, Bundle), calling this method again will initialize and return the GoogleMap.

As there is no indication that from where did you got your map and at exactly which method call so I assume that your getmap() is returning null. That means the map is not ready. Either because the fragment is not ready or your are running on a device without Google Play services available.

See getMap for more info.

Upvotes: 1

Piyush
Piyush

Reputation: 18923

Initially when first time your GoogleMap loads takes some time and there fore Location will be null.

So for your both menu options check this way.

if(map!=null){
 map.setMyLocationEnabled(true);
}


if(map!=null){
    Location myLocation = map.getMyLocation();

    if(myLocation !=null){
    LatLng myLatLng = new LatLng(myLocation.getLatitude(),
            myLocation.getLongitude());


    CameraPosition myPosition = new CameraPosition.Builder()
            .target(myLatLng).zoom(17).bearing(90).tilt(30).build();
    map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));
   }
}

Upvotes: 3

Related Questions