2014-02-28 37 views
0

我正在使用Google Map V2,並試圖獲取當前位置。但它總是返回null。LocationManager發送null

代碼

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
Criteria criteria = new Criteria(); 
provider = locationManager.getBestProvider(criteria, false); 
Location location = locationManager.getLastKnownLocation(provider); 

每次我得到location空。我的GPS工作正常,在我的設備什麼都的問題,請幫助我。

+0

使用GPS找到位置需要更多的時間,所以你收到空,如果你使用網絡提供商,你得到它在第二 – appukrb

+0

打開本地地圖的應用程序,然後打開你的應用程序,它給出的位置,這是GPS提供商的問題,更好,你可以嘗試熔合位置提供商,以避免這個問題 –

回答

0

getLastKnownLocation documentation

如果提供當前被禁用,則返回null。

與您使用getBestProvider(criteria, false)你的意思是你允許未啓用的供應商(這是false手段) - 切換到true如果你只是想看看啓用提供商(這將確保getLastKnownLocation不返回空值)。

請注意,getLastKnownLocation可能已過時,如果您需要獲取最近的位置,您可能仍然希望查找位置更新。

0

試試這個:

將這個全球:

private LocationManager location_manager; 
    // flag for GPS status 
    boolean isGPSEnabled = false; 

    // flag for network status 
    boolean isNetworkEnabled = false; 

    // flag for GPS status 
    boolean canGetLocation = false; 

    Location location = null; // location 
    double latitude; // latitude 
    double longitude; // longitude 

    // The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

從電話,無論你想這個方法:

public Location getCurrentLatLong(Context conext,LocationListener con) 
    { 
     try 
     { 
      location_manager = (LocationManager) ((Activity) con) 
        .getSystemService(Context.LOCATION_SERVICE); 

      // getting GPS status 
      isGPSEnabled = location_manager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = location_manager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) 
      { 
       // no network provider is enabled 
       Toast.makeText(conext, "No provider is enabled", Toast.LENGTH_SHORT).show(); 
      } 
      else 
      { 
       this.canGetLocation = true; 
       if (isNetworkEnabled) 
       { 
        location_manager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, con); 
        Log.d("Network", "Network Enabled"); 
        if (location_manager != null) 
        { 
         location = location_manager 
           .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) 
        { 
         location_manager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, con); 
         Log.d("GPS", "GPS Enabled"); 
         if (location_manager != null) 
         { 
          location = location_manager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) 
          { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     return location; 
    } 
+0

你會清楚getCurrentLatLong() – Developer

+0

只要通過您的上下文 –

+0

那麼這個LocationListener con參數 – Developer

0

有不同的方法。首先,請確保您已獲得所有必要許可,您的清單:

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

如果你只想要一個大概的位置,你可以得到lastlocation(GPS或網絡)

if(_locationManager != null) 
{ 
    if(_locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) 
     return new Location(_locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER),true); 

    else if(_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) != null) 
     return new Location(_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER),true);  
} 
相關問題