谷歌位置服务getBestProvider使应用程序崩溃即使所有位置服务被禁用或可更改(从GPS到无线)

时间:2014-07-07 14:48:54

标签: android gps google-maps-api-2

我希望当位置服务禁用或从GPS更改为无线或无线到GPS时,将显示地图。我尝试了更多,但应用程序崩溃了。

非常感谢任何帮助。提前谢谢。

java代码

if (servicesOK()) {
        setContentView(R.layout.map);
        if (initMap()) {

            Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show();

            gotoLocation();
        } else {
            Toast.makeText(this, "Map not available", Toast.LENGTH_SHORT)
                    .show();

        }
    } else {
        setContentView(R.layout.activity_error);
    }

}



public boolean servicesOK() {
    int isAvailable = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    if (isAvailable == ConnectionResult.SUCCESS) {
        return true;
    } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
                this, GPS_ERRORDIALOG_REQUEST);
        dialog.show();
    } else {
        Toast.makeText(this, "can't connect to Google Play Services",
                Toast.LENGTH_SHORT).show();
    }
    return false;
}

private boolean initMap() {
    if (Gmap == null) {
        SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        Gmap = mapFrag.getMap();
    }
    return (Gmap != null);
}

private void gotoLocation() {
    // Enable my location layer of Google map

    Gmap.setMyLocationEnabled(true);
    // get LocationManger object from system service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // create criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Get the name of best provider
    String provider = locationManager.getBestProvider(criteria, true);
    // Get Current Location 

    Location myLocation = locationManager.getLastKnownLocation(provider);

    // set map type
    Gmap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    // Get latitude of the current location

    double latitude = myLocation.getLatitude();

    // Get longitude of the current location

    double longitude = myLocation.getLongitude();

    // create latlng object for the current location

    LatLng latlng = new LatLng(latitude, longitude);

    }

1 个答案:

答案 0 :(得分:0)

使用此方法,您可以检查启用了哪种位置:

 public static String checkGPS(Context context) {

        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        String locationProviders = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            return "Not enable";
        }
        // if gps is enabled and network location is disabled
        // note, GPS can be slow to obtain location data but is more accurate
        else if (locationProviders.contains("gps") && !locationProviders.contains("network")) {
            return "Enable";
        } else if (locationProviders.contains("gps") && locationProviders.contains("network")) {
            return "Enable + Network";
        } else if (!locationProviders.contains("gps") && locationProviders.contains("network")) {
            return "Only Network";
        } else {
            return "Not enable";
        }

    }

希望它对你有所帮助。