如何修复getMapAsync(this)崩溃?

时间:2017-05-27 20:36:45

标签: java android google-maps maps

嘿我试图让位置服务在Android Studio上工作,而.getMapAsync(这个)会让应用程序崩溃。如果有人能告诉我我搞砸了哪里,我会非常感激。

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);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

getMapAsync(this)方法调用onCreate()

更新您的代码,如下所示:

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));
}

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

以下是关于GoogleMapLocation 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