我想在android中獲得我的精確經度和緯度。我的android設備中沒有GPS。所以,我需要在互聯網的幫助下獲得我的位置。如何獲取我的當前位置在緯度和經度? (不帶GPS,但帶有互聯網服務)
&
如果獲得緯度和經度失敗,顯示舉杯「無法獲取位置」提前
感謝。
我想在android中獲得我的精確經度和緯度。我的android設備中沒有GPS。所以,我需要在互聯網的幫助下獲得我的位置。如何獲取我的當前位置在緯度和經度? (不帶GPS,但帶有互聯網服務)
&
如果獲得緯度和經度失敗,顯示舉杯「無法獲取位置」提前
感謝。
嘗試網絡提供商
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();
}
}
你應該做這樣的事情,而不是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();
}
}
老答 嘗試使用此代碼。它會給你的地理位置,距離網絡不來回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;
}
}
你好烏梅爾哈立德。感謝您的回答。我能做些什麼來配置: 如果GPS可用,那麼從GPS獲取位置,否則使用網絡。再次感謝。 – 2015-04-04 12:11:58
支票更新ans – 2015-04-04 13:45:05
感謝Umair Khalid – 2015-04-04 13:52:13
你好+ Dilavar,是否需要安裝谷歌Play服務才能使用此功能。請回答。 – 2015-04-02 05:03:02
它不使用Google Play服務。 – 2015-04-02 06:34:07
謝謝+ Dilavar:D – 2015-04-04 12:09:26