2015-03-31 81 views

回答

1

嘗試網絡提供商

LocationManager lManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 

     boolean netEnabled = lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
       if (netEnabled) { 
        lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
        location = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    if (location != null) 
       { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 

         } 
       } 
+0

你好+ Dilavar,是否需要安裝谷歌Play服務才能使用此功能。請回答。 – 2015-04-02 05:03:02

+0

它不使用Google Play服務。 – 2015-04-02 06:34:07

+0

謝謝+ Dilavar:D – 2015-04-04 12:09:26

0

你應該做這樣的事情,而不是LocationManager.GPS_PROVIDER,使用LocationManager.NETWORK_PROVIDER

LocationManager lManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(
           LocationManager.NETWORK_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("Network", "Network"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
           System.out.println("latitude network" + latitude+"longi "+location.getLongitude()); 
           if (latitude == 0.0) { 
            showSettingsAlertData(); 
           } 
          } 
1

老答 嘗試使用此代碼。它會給你的地理位置,距離網絡不來回GPS

Location location =myManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
latPoint=location.getLatitude(); 
lngPoint=location.getLongitude(); 

更新ANS

private Location getLastBestLocation() { 
Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

long GPSLocationTime = 0; 
if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); } 

long NetLocationTime = 0; 

if (null != locationNet) { 
    NetLocationTime = locationNet.getTime(); 
} 

if (0 < GPSLocationTime - NetLocationTime) { 
    return locationGPS; 
} 
else { 
    return locationNet; 
} 
} 
+0

你好烏梅爾哈立德。感謝您的回答。我能做些什麼來配置: 如果GPS可用,那麼從GPS獲取位置,否則使用網絡。再次感謝。 – 2015-04-04 12:11:58

+0

支票更新ans – 2015-04-04 13:45:05

+0

感謝Umair Khalid – 2015-04-04 13:52:13

相關問題