【问题标题】:Getting LatLong using NetWork Provider使用 NetWork Provider 获取 LatLong
【发布时间】:2016-02-09 09:07:52
【问题描述】:

我正在使用下面的代码来获取 LatLong,我也在使用 NetWorkProvider 来获取 LatLong:

下面的代码工作正常:

        // if GPS Enabled get lat/long using GPS Services
        if (isGPSEnabled) {
            if (mLocation == null) {
                mLocationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        5000,
                        5000, this);
                if (mLocationManager != null) {
                    mLocation = mLocationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (mLocation != null) {

                        //Called here...
                        onLocationChanged(mLocation);

                    }
                }
            }
        }

但是,当我使用下面的代码时:

if (isNetworkEnabled) {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER,
                    5000,
                    5000, MainActivity.this);

            if (mLocationManager != null) {
                mLocation = mLocationManager
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (mLocation != null) {
                    onLocationChanged(mLocation);
                }
            }
        }

我正在获取 mLocation null。可能是什么原因?

【问题讨论】:

    标签: android google-maps location latitude-longitude


    【解决方案1】:

    请尝试下面的代码。

    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 (Exception e) {
        e.printStackTrace();
    }
    
    return location;
    }
    

    【讨论】:

    • 同样的问题,获取位置为空,当我为 isGPSEnableb 提交 if 循环时,但它可以使用 GPS 取消注释和检查。
    【解决方案2】:

    检查AndroidManifest.xml中是否存在权限:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    【讨论】:

      猜你喜欢
      • 2017-05-03
      • 2011-06-03
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      相关资源
      最近更新 更多