代码之家  ›  专栏  ›  技术社区  ›  Rakesh

Abt检索设备的纬度和经度

  •  2
  • Rakesh  · 技术社区  · 14 年前

    我想检索用户当前位置的经纬度,我已经尝试使用location observer,它根据给定的输入距离和时间触发事件,因此它会根据给定的持续时间和距离触发事件来标识位置。

    在android中有什么方法吗?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Pentium10    14 年前
    /**
     * 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;
    }