Google Maps API结果与地点结果会返回不同的名称

时间:2018-04-23 07:00:13

标签: google-maps google-maps-urls

我正在使用谷歌地图提供网站内多个位置的路线。用户在日本,但非日语,因此结果应为英语。

在某些示例中,即使名称位于查询参数中,类似此location的链接也会返回替代日语地名(主教座圣堂牧师馆),而不是“圣安德鲁东京”。

此链接是动态生成的,因此我可以根据需要更改参数,但我无法弄清楚如何在不对整个链接进行硬编码的情况下强制显示更像this的结果。以下是构建URL的内容:

//handle directions links; send to Apple Maps (iOS), or Google Maps (everything else)
var iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
$body.on('click', 'a.tsml-directions', function(e){
    e.preventDefault();
    var directions = (iOS ? 'maps://?' : 'https://maps.google.com/?') + $.param({
        daddr: $(this).attr('data-latitude') + ',' + $(this).attr('data-longitude'),
        saddr: 'Current Location',
        q: $(this).attr('data-location')
    });
    window.open(directions);
});

1 个答案:

答案 0 :(得分:1)

我查看了您的示例网址https://www.google.com/maps?daddr=35.6603676,139.7444553&saddr=Yotsuya,%20Shinjuku,%20Tokyo%20160-0004&q=St.%20Andrew%27s%20Tokyo

我了解您的意图是在Google地图上获取路线。在上述网址中,您指定了原始saddr和目标daddr的参数,q参数不应影响此情况下的路线。因此,目标地址只是坐标35.6603676,139.7444553。当我反向地理编码这个坐标时,我得到了'日本,〒105-0011Tōkyō-to,Minato-ku,Shibakōen,3 Chome-6-18主教座圣堂牧师馆'地址,如Geocoder工具所示:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D35.660368%252C139.744455

主教座聖堂牧師館对应于前提地址组件,我怀疑它在Google数据库中没有翻译成英文,因为语言设置为英语的Web服务调用也会以原始语言返回此组件

https://maps.googleapis.com/maps/api/geocode/json?latlng=35.6603676%2C139.7444553&language=en&key=YOUR_API_KEY

如果您的目的地应为圣安德鲁,请将其用作目的地参数。

最重要的部分:Google将 Google Maps URLs 作为构建网址的官方,推荐和文档化方法。我建议您使用此API来创建路线网址。这些URL是跨平台的,因此无需为iOS,Android或Web浏览器创建不同的URL。

您的示例将转换为

https://www.google.com/maps/dir/?api=1&origin=Yotsuya,%20Shinjuku,%20Tokyo%20160-0004&destination=St.%20Andrew%27s%20Tokyo&travelmode=driving

结果显示在屏幕截图

enter image description here

您的代码可能类似于

$body.on('click', 'a.tsml-directions', function(e){
    e.preventDefault();
    var directions = 'https://www.google.com/maps/dir/?' + $.param({
        api: "1",
        destination: $(this).attr('data-location'),
        travelmode: "driving"
    });
    window.open(directions);
});

注意我没有指定origin参数,因为它是可选的,在这种情况下它应该被解释为我当前的位置。

我希望这有帮助!