【问题标题】:Gmaps4rails - how to set map languageGmaps4rails - 如何设置地图语言
【发布时间】:2016-09-15 00:12:03
【问题描述】:

我想本地化由 Gmaps4rails 生成的地图,这样我就可以用用户想要的语言显示地名。 Google 在此处记录了如何执行此操作: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' })。

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

【问题讨论】:

    标签: javascript ruby-on-rails google-maps google-maps-api-3 gmaps4rails


    【解决方案1】:

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

    例如在Maps API v3 Now Speaks Your Language

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

    您可以在此处找到languages 的列表。

    这是代码示例:

    <!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&amp;callback=initMap&amp;language=ja&amp;region=JP,语言设置为 jp = Japanese

    还可以使用动态语言设置检查他们的工作 sample

    希望这会有所帮助!

    【讨论】:

    • 感谢@Mr.Rebot,我认为可能有一种方法可以通过 gem 来处理这个问题,但是因为我可以在 rails 中动态编辑 src,所以我不妨这样做。谢谢你的帖子。