首页 > 解决方案 > 更改由 google Places API 自动完成方法调用的 formatted_address 的语言

问题描述

我使用 google Places API 收到的数据以我的本地语言为我提供了 formatted_address 字段。我希望它是英文的。

我已启用 google Places Api 并使用自动完成方法来加载建议。创建一个简单的 html 页面来加载接收到的数据。我尝试在脚本标签中将语言选项设置为“en”。

HTML:

<div id="info-table">
  Name: <span id="place_name"></span><br>
  Place id: <span id="place_id"></span><br>
  Address :<span id="place_address"></span><br>
  Phone : <span id="phone_no"></span><br>
  Openhours: <span id="open_time"></span><br>
  Open Now : <span id="open_now"></span><br>
  website : <span id="website"></span><br>
  photo :<br> <img id="photo" src="" style="display:none;" />
</div>

<div>
<p>
<span id="all-data"></span>
</p>
</div>


<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&callback=initMap&language=en&region=US">
</script>

JS:

function initMap() {

  var input = document.getElementById('pac-input');

  var options = {
  language:'en',
    types: ['establishment']
  };

  var autocomplete = new google.maps.places.Autocomplete(input, options);

  autocomplete.setFields(['place_id', 'geometry', 'name', 'formatted_address', 'formatted_phone_number', 'opening_hours', 'website', 'photos']);

  autocomplete.addListener('place_changed', placechange);

  function placechange() {

    var place = autocomplete.getPlace();
    var photos = place.photos;

    $('#all-data').text(JSON.stringify(place));
    $('#place_name').text(place.name);
    $('#place_id').text(place.place_id);
        $('#place_address').text(place.formatted_address);
    $('#phone_no').text(place.formatted_phone_number);
    $('#open_time').text(place.opening_hours.weekday_text[0]);
    $('#open_now').text(place.opening_hours.open_now);
    $('#photo').attr("src",photos[0].getUrl());
    $('#photo').attr("style","width:50%;");
    $('#website').text(place.website);

   }
}

当前 formatted_address:2nd Floor, 320 ටී බී ජයා මාවත, කොළඹ 01000
预期 formatted_address: 2nd Floor , 320 ,TBJaya Avenue,Colombo 01000。

我得到的当前响应:

{"formatted_address":"2nd Floor, 320 ටී බී ජයා මාවත, කොළඹ 01000, Sri Lanka","formatted_phone_number":"0112 683 751","geometry":{"location":{"lat":6.923615999999999,"lng":79.86028599999997},"viewport":{"south":6.922419719708497,"west":79.8594205697085,"north":6.925117680291502,"east":79.86211853029158}},"name":"hSenid Mobile Solutions","opening_hours":{"open_now":true,"periods":[{"close":{"day":1,"time":"1730","hours":17,"minutes":30},"open":{"day":1,"time":"0830","hours":8,"minutes":30}},{"close":{"day":2,"time":"1730","hours":17,"minutes":30},"open":{"day":2,"time":"0830","hours":8,"minutes":30}},{"close":{"day":3,"time":"1730","hours":17,"minutes":30},"open":{"day":3,"time":"0830","hours":8,"minutes":30}},{"close":{"day":4,"time":"1730","hours":17,"minutes":30},"open":{"day":4,"time":"0830","hours":8,"minutes":30}},{"close":{"day":5,"time":"1730","hours":17,"minutes":30},"open":{"day":5,"time":"0830","hours":8,"minutes":30}}],"weekday_text":["Monday: 8:30 AM – 5:30 PM","Tuesday: 8:30 AM – 5:30 PM","Wednesday: 8:30 AM – 5:30 PM","Thursday: 8:30 AM – 5:30 PM","Friday: 8:30 AM – 5:30 PM","Saturday: Closed","Sunday: Closed"]},"photos":[{"height":2176,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/100291523214478364695/photos\">Malinda Prasad</a>"],"width":4608},{"height":4032,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/105380056766383259301/photos\">Sasindu Jayashma</a>"],"width":2268},{"height":2184,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/116851599999613156839/photos\">Dinesha Karunathilake</a>"],"width":4608},{"height":2448,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/100485539031416489634/photos\">Milinda Arambawela</a>"],"width":3264},{"height":4032,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/105380056766383259301/photos\">Sasindu Jayashma</a>"],"width":3024},{"height":960,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/114606659795691325467/photos\">hSenid Mobile Solutions</a>"],"width":960},{"height":4032,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/105380056766383259301/photos\">Sasindu Jayashma</a>"],"width":2268},{"height":300,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/114606659795691325467/photos\">hSenid Mobile Solutions</a>"],"width":300},{"height":720,"html_attributions":["<a href=\"https://maps.google.com/maps/contrib/114606659795691325467/photos\">hSenid Mobile Solutions</a>"],"width":960}],"place_id":"ChIJO814yhNZ4joRzunigaOMo6A","website":"https://www.hsenidmobile.com/","html_attributions":[]}

标签: javascriptgoogle-mapsgoogle-apigoogle-places-api

解决方案


language参数影响控件的名称、版权声明、行车路线和控件标签,以及对服务请求的响应。对服务的影响并不明显。

例如,当对街道级地址进行地理编码时,国家名称将以您请求的语言返回,但地址的其余部分将特定于您正在地理编码的位置。另一方面,邮政和政治结果以请求的语言返回。

这意味着您还需要指定region偏差。例如?language=en-GB&region=GB

当您更新语言参数时,请尝试此演示以试验地图上的更改。

更多信息在这里

更新:

经过调查,似乎这是自动完成中的一个错误。

谷歌地图自动完成


推荐阅读