【问题标题】:google-maps-services java library result differs from browser call resultgoogle-maps-services java 库结果与浏览器调用结果不同
【发布时间】:2016-07-11 10:11:40
【问题描述】:

我正在使用这个 java 库

<dependency>
        <groupId>com.google.maps</groupId>
        <artifactId>google-maps-services</artifactId>
        <version>0.1.15</version>
</dependency>

当我执行下面的代码时

public static GeocodingResult getGeocode(String address) {
    GeocodingResult[] results;
    try {
        GeoApiContext context = new GeoApiContext().setApiKey(GOOGLE_KEY);
        results = GeocodingApi.geocode(context, address).await();
        if (results.length > 0) {
            return results[0];
        } else {
            return null;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

public static void main(String[] args) {
        GeocodingResult geocode = getGeocode("Imanova 19, Astana");

        System.out.println(geocode.geometry.location.lat);
}

结果给出

INFO: Request: https://maps.googleapis.com/maps/api/geocode/json?key=MY_GOOGLE_KEY&address=Imanova+19%2C+Astana
51.16052269999999

但是,当我尝试从浏览器调用相同的给定请求时,它会给出另一个结果。

问题是,为什么会这样以及如何解决?

从 cmets 编辑:

https://maps.googleapis.com/maps/api/geocode/json?address=Imanova+19%2C+Astana的结果

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Astaná",
               "short_name" : "Astaná",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Astana",
               "short_name" : "Astana",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Kazajistán",
               "short_name" : "KZ",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "020000",
               "short_name" : "020000",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Astaná 020000, Kazajistán",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 51.2903453,
                  "lng" : 71.7427397
               },
               "southwest" : {
                  "lat" : 51.0055461,
                  "lng" : 70.9179879
               }
            },
            "location" : {
               "lat" : 51.16052269999999,
               "lng" : 71.47035579999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 51.2903453,
                  "lng" : 71.7427397
               },
               "southwest" : {
                  "lat" : 51.0055461,
                  "lng" : 70.9179879
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJCUa1fcSARUIRKJKx3Y0U-Zc",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

【问题讨论】:

  • 使用 Chrome 查询maps.googleapis.com/maps/api/geocode/… 我收到"lat" : 51.16052269999999"location_type" : "APPROXIMATE"。你的location_type是什么?
  • 我的 location_type 是 ROOFTOP。我也在使用 Chrome 进行测试。但是为什么我们会得到不同的类型呢?如何影响到这种类型?
  • 根据developers.google.com/maps/documentation/geocoding/intro ROOFTOP“表示返回的结果是一个精确的地理编码,我们的位置信息精确到街道地址的精确度”。我在没有 API 密钥的情况下查询
  • @antonio,但文档没有说明 API 密钥和 location_type 的关系
  • @antonio,你可以显示或发送纬度为51.16052269999999的json结果@

标签: java google-maps


【解决方案1】:

这个答案https://stackoverflow.com/a/30749036/1862262 完全描述了我的问题的解决方案。

和我设置区域的方法一样

GeocodingResult[] results = GeocodingApi.geocode(context, address).region("KZ").await();

并得到与浏览器中的结果相同的结果。

但是不设置的话不知道要带哪个区域。

这是有关此选项和问题的文档https://developers.google.com/maps/documentation/geocoding/#RegionCodes

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多