我想检索用户当前位置的纬度和经度,我已经尝试使用基于给定输入距离和时间触发事件的位置观察器,因此它触发事件以根据给定的持续时间识别位置和距离。
我想要的是方法,只需调用该方法即可获取位置。我什么时候打电话给那个方法,它应该通知我纬度和经度。
android中有没有方法?
答案 0 :(得分:2)
/**
* This is a fast code to get the last known location of the phone. If there
* is no exact gps-information it falls back to the network-based location
* info. This code is using LocationManager. Code from:
* http://www.androidsnippets.org/snippets/21/
*
* @param ctx
* @return
*/
public static Location getLocation(Context ctx) {
LocationManager lm = (LocationManager) ctx
.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/*
* Loop over the array backwards, and if you get an accurate location,
* then break out the loop
*/
Location l = null;
for (int i = providers.size() - 1; i >= 0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null)
break;
}
return l;
}