启用 GPS 后获取用户当前的纬度 lng - 安卓



我按照本教程 http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/在未启用GPS时显示弹出窗口并获取用户当前位置。

这是广播接收器,它侦听GPS启用禁用条件

private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            gps = new GPSTracker(getActivity());
            if(gps.canGetLocation()){
                Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
                Toast.makeText(getActivity(), "GPS Switch", Toast.LENGTH_SHORT).show();
                if(gps.getLatitude() != 0 && gps.getLongitude() != 0 ){
                    final CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(gps.getLatitude(),gps.getLongitude()))      // Sets the center of the map to selected place
                            .zoom(15)                   // Sets the zoom
                            .build();                   //
                    gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                    Toast.makeText(getActivity(), "GPS Switch Lat: "+gps.getLatitude()+" Long: "+gps.getLongitude(), Toast.LENGTH_SHORT).show();
                }
            } else {
                if(!gps.isSettingDialogShown())
                    gps.showSettingsAlert();
            }
        }
    }
};

当我通过设置对话框或下拉菜单启用GPS时。GPSTracker 类获取位置服务并在此代码片段中检查用户的当前位置

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    return location;
}

问题

首先,我不知道为什么广播接收器会接收两次操作,其次,当在正常模式下,接收器中的 lat lng 值保持零,但在调试中有时它会返回用户当前位置的值。

你能帮助我做错什么吗?

如果您不需要单独使用 GPS 和网络位置。我建议使用FusedLocationProvider。BroadcastReceiver 可能工作两次,因为它适用于 GPS 和网络定位服务

最新更新