2016-02-09 21 views
-2

我使用下面的代码来获得的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。可能是什么原因?

回答

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; 
} 
+0

同样的问题,获取位置空,当我commiting如果回路isGPSEnableb,但亚其取消注释,并检查使用GPS的工作。 – user5716019

0

检查权限存在在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" /> 
+0

已经assinged @塔帕 – user5716019