Gmaps4rails -如何设置地图语言

Gmaps4rails - how to set map language

本文关键字:设置 地图 语言 何设置 Gmaps4rails      更新时间:2023-09-26

我想对Gmaps4rails生成的地图进行本地化,这样我就可以用用户想要的语言显示地名。谷歌文档如何做到这一点:https://developers.google.com/maps/documentation/javascript/examples/map-language

我正在使用Gmaps4rails的一个相当标准的(目前功能齐全的)实现,您可以在这里看到它。

handler = Gmaps.build('Google');
handler.buildMap({ 
    provider: { styles: mapStyle }, 
    internal: {id: 'map'}
}, function(){
    markers = handler.addMarkers(<%=raw @hash.to_json %>);
    handler.bounds.extendWith(markers);
    handler.fitMapToBounds();
});

渲染到html…

<div class="map-container">
  <div id="map"></div>
</div>

我只需要找出在哪里定义语言代码。我试过将它作为一个选项添加到提供商,没有喜悦(例如provider: { styles: mapStyle, language: 'zh-TW' })。

我已经搜索了文档(和源代码),但似乎找不到任何关于这方面的信息。任何帮助将不胜感激!

您必须在脚本中指明语言。

例如在地图API v3现在说你的语言:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=pt-BR">

你可以在这里找到语言列表。

下面是代码示例:
<!DOCTYPE html>
<html>
  <head>
    <title>Localizing the Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      // This example displays a map with the language and region set
      // to Japan. These settings are specified in the HTML script element
      // when loading the Google Maps JavaScript API.
      // Setting the language shows the map in the language of your choice.
      // Setting the region biases the geocoding results to that region.
      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 8,
          center: {lat: 35.717, lng: 139.731}
        });
      }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&language=ja&region=JP"
    async defer>
    </script>
  </body>
</html>

您可以在这行代码中看到,key=YOUR_API_KEY&callback=initMap& language=ja &region=JP,语言设置为jp =日语

还可以使用动态语言设置检查它们的工作示例。

希望这对你有帮助!