【问题标题】:Current Location with google map api V2当前位置与谷歌地图 api V2
【发布时间】:2013-05-18 21:33:13
【问题描述】:

问候,

我只是想问一下,Google Map Api 密钥第 2 版是否有任何方法或类,我可以使用它通过派生的纬度和经度获取当前位置(在地址中)。在 Google Map Api 版本 1 中有类 Geocoder,我们可以使用它来将纬度和经度更改为地址。但我认为我们不能在 Api v2 中使用 Geocoder 类,因为它使用 Fragment 类。有人有解决方案吗?谢谢。

【问题讨论】:

标签: android google-maps


【解决方案1】:

Geocoder 不属于 Maps API v1,您可以将其与 API v2 一起使用。

【讨论】:

  • 你有什么例子吗?或者如果你可以给我看一些代码。实际上我做到了,但是我得到了纬度和经度,但我无法获得该特定位置的地址。
  • 在顶部的搜索框中输入“地理编码器示例”会给出:stackoverflow.com/questions/11430726/…
  • 好的,谢谢。我会试试这个。
【解决方案2】:

是的,GeoCoder 真的很可怕。在某些设备上,这无法正常工作。你可以这样做,这是独立的......做一个json-request:

如果您需要其他方式,该代码将为您提供帮助。地址 -> 位置

public static JSONObject getLocationInfo(String address) {

    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +address+"&ka&sensor=false");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonObject;
}

public static GeoPoint getGeoPoint(JSONObject jsonObject) {

    Double lon = new Double(0);
    Double lat = new Double(0);

    try {

        lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lng");

        lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2015-02-10
    • 1970-01-01
    • 2016-04-29
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多