0

Hey I'm trying to get location services to work on Android Studio and .getMapAsync(this) keeps crashing the app. If anyone can show me where I'm messing up I'd greatly appreciate it.

GoogleMap memberMap;
GoogleApiClient memberAPI;
LocationRequest memberLocationRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (googleServicesAvailable()) {

        setContentView(R.layout.activity_main_page);

        onMapReady(memberMap);
    } else {
        Toast.makeText(this, "Google services not available", Toast.LENGTH_LONG);
        //to be done
    }
}
@Override
public void onMapReady(GoogleMap googleMap) {
    memberMap = googleMap;

    //memberMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    SupportMapFragment mapFragment =(SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapfragment);
    mapFragment.getMapAsync(this);





    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if(!locationManager.isProviderEnabled((LocationManager.GPS_PROVIDER))){
        buildAlertMessageNoGps();
    }

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            buildGoogleApiClient();
            memberMap.setMyLocationEnabled(true);
        }
    }

}
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare May 27 '17 at 20:38

1 Answers1

0

Call getMapAsync(this) from onCreate() method.

Update your code as below:

GoogleMap memberMap;
GoogleApiClient memberAPI;
LocationRequest memberLocationRequest;

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

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment);
        mapFragment.getMapAsync(this);
    } else {
        Toast.makeText(this, "Google services not available", Toast.LENGTH_LONG);
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    memberMap = googleMap;

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if(!locationManager.isProviderEnabled((LocationManager.GPS_PROVIDER))){
        buildAlertMessageNoGps();
    }

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            buildGoogleApiClient();
            memberMap.setMyLocationEnabled(true);
        } else {
            buildGoogleApiClient();
            memberMap.setMyLocationEnabled(true);
        }
    }

    // Add markers
    LatLng sydney = new LatLng(-34, 151);
    memberMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    memberMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

...................
...............

Here are some good tutorial about GoogleMap and Location API:

  1. Developing an Android app having Google Maps
  2. Google Map Tutorial in Android Studio: How to get current location in Android Google Map
  3. Android Developer’s Guide to the Google Location Services API
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61