首页 > 解决方案 > Google Maps API 在 Chrome 和 RestTemplate 之间具有不同的结果

问题描述

该服务正在使用 google maps api ( geocode )。

当我使用 spring resttemplate 的默认 bean 配置执行 GET 时,我的值与在 Web 浏览器 (Chrome) 上执行此 GET 时不同。

调用 Chrome 并使用 resttemplate:

https://maps.googleapis.com/maps/api/geocode/json?key=mykeymykeymykeymykey&address=Rua%20Marques%20de%20Valenca,%20100,%20Alto%20da%20Mooca,%20S%C3%A3o%20Paulo%20 -%20SP,%20巴西&语言=pt-BR

当我执行反向地理编码时,chrome 执行更加精确。

结果: 铬:

location: {
  lat: -23.5577251,
  lng: -46.5948733
},

休息模板:

location: {
  lat: -23.5574375,
  lng: -46.5948733
},

我试过使用 Double、Float 和 BigDecimal。我尝试在序列化之前创建一个反序列化器来获取这个值,但是值是一样的。

我正在使用 Java 8 和 Spring Boot 2.0.3。

有人知道如何准确吗?

标签: springgoogle-mapsjava-8google-apiresttemplate

解决方案


我正在使用 UriComponentsBuilder ,当我使用 toUriString 时,url 被格式化为浏览器,并且无法正常工作。

UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(googleHost)
                .queryParam("key", apiKey)
                .queryParam(input, address)
                .queryParam("language", language);

它现在正在使用 StringUtils.join(...) 来构建 URI。

String googleurl = StringUtils.join(googleGeocodingHost, 
        "?key=", apiKey, "&", input, "=", address, "&language=", language);

推荐阅读