我需要在Android上获取用户的当前位置,我看过一些方法,但他们都似乎矫枉过正。我已经在应用上实施了Google地图,有没有简单的方法来获取用户的当前位置?什么是最简单的方式来获取用户当前位置Android
-4
A
回答
0
请阅读谷歌有关位置策略。我有一个应用程序与用户当前的位置,我也用这样的方式,它的工作非常好:
杉杉我得到的位置,这个类:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
我创建了两个方法用于接收位置:第一个是NETWORK_PROVIDER,第二个是GPS。
private static final int TWO_MINUTES = 1000 * 60 * 2;
/** Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
*/
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
相关问题
- 1. 获取iPhone当前位置的最简单方法是什么?
- 2. 获取当前用户的SID的最佳方式是什么?
- 3. 在android中获取当前精确位置的最佳方式是什么?
- 4. Android的最好的方式来获得当前位置
- 5. 获取当前用户的UserId的最佳方法是什么?
- 6. 获取mysql_fetch_assoc循环的数据指针当前位置的最简单方法是什么?
- 7. 应用开发:最简单的方式获得手机当前位置
- 8. 在.NET中获取当前总CPU负载的最简单方法是什么?
- 9. 获取当前操作系统的最佳方式是什么?
- 10. Android获取当前位置
- 11. android获取当前位置
- 12. 获取当前位置android
- 13. 什么是最简单的方式来获取字符串中字符位置的行号?
- 14. 什么是当前设置第三方cookie的最佳方式?
- 15. Android:使WebView显示位图的最简单方法是什么?
- 16. 获取Android中当前位置方向的位置
- 17. 获得前x个字符的最简单方法是什么?
- 18. osmdroid获取当前用户位置
- 19. 如何获取用户当前位置
- 20. 获取发送邮件的MessageID最简单/最干净的方式是什么?
- 21. 什么是最简单的方式来运行Windows Cron作业
- 22. 获取与GPS /网络的Android MapView的用户当前位置
- 23. 为什么不能在android设备上获取当前位置?
- 24. 如何获取用户在android上的当前位置?
- 25. ASP.NET中获取当前域的最佳方法是什么?
- 26. 获取XmlElement顶级文本的最简单方法是什么?
- 27. 从HSQLDB获取ddl sql-script的最简单方法是什么?
- 28. 从网址获取号码的最简单方法是什么?
- 29. 获取域名IP地址的最简单方法是什么?
- 30. 获取经纬度的最简单方法是什么?
为 – 2014-10-01 09:21:11
使用的LocationManager类,你可以发布一个例子请:之后,我用这个类有什么更好的位置检查?我试过但没有成功 – Signo 2014-10-01 09:22:35