Abt检索设备的纬度和经度

时间:2010-07-16 09:29:53

标签: android

我想检索用户当前位置的纬度和经度,我已经尝试使用基于给定输入距离和时间触发事件的位置观察器,因此它触发事件以根据给定的持续时间识别位置和距离。

我想要的是方法,只需调用该方法即可获取位置。我什么时候打电话给那个方法,它应该通知我纬度和经度。

android中有没有方法?

1 个答案:

答案 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;
}