【问题标题】:Google Location Services getBestProvider make the app crashed even though all location services are disabled or changeable from (GPS to Wireless)即使所有位置服务都已禁用或可从(GPS 到无线)更改,Google 位置服务 getBestProvider 也会使应用程序崩溃
【发布时间】:2023-03-31 18:51:01
【问题描述】:

我希望在定位服务禁用或从 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);

    }

【问题讨论】:

  • 我现在也遇到了同样的问题,请问您解决了吗?请更新我们。

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


【解决方案1】:

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

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

    }

希望对你有所帮助。

【讨论】:

  • 我在我的代码中添加了你的方法,当打开无线运行良好但打开 gps(注意 gps 在当前位置不工作)不起作用时,我希望当所有服务都被禁用时,地图是仅显示。
  • 那么,您需要实现一个接收器来在 GPS 状态改变时进行通知。这可以帮助你:stackoverflow.com/questions/12665558/…
  • 这个 public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } }
猜你喜欢
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
相关资源
最近更新 更多