【问题标题】:Get user current lat lng after GPS enabled - Android启用 GPS 后获取用户当前的 lat lng - Android
【发布时间】:2017-03-31 12:32:07
【问题描述】:

我已按照本教程 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 值保持为零,但在调试中有时它会返回用户当前位置的值。

你能帮我做错吗?

【问题讨论】:

  • 使用 google Fused Location Api 和位置设置,Check this
  • @CommonsWare 我不明白打开 GPS 后如何在广播接收器中获取 lat lng ?在你的回答中,你说避免使用 getLastKnownLocation() 那么用什么来代替呢?
  • 使用requestLocationUpdates(),您可以在其中获得LocationListener 中的位置。这包含在我链接的答案中,请参阅步骤 #2 和 #3。

标签: android google-maps geolocation gps android-gps


【解决方案1】:

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

【讨论】:

  • 感谢您的帮助,它解决了我面临的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
  • 2016-07-30
相关资源
最近更新 更多