Android获得当前的GEO坐标

时间:2010-07-16 13:53:14

标签: android

如何在Android中不使用GPS获得GEO坐标(纬度和经度)? 有什么建议吗?..

1 个答案:

答案 0 :(得分:7)

/**
 * 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;
}