簡體   English   中英

Gmaps4rails-如何設置地圖語言

[英]Gmaps4rails - how to set map language

我想對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' } )。

我已經搜索了文檔(和源代碼),但是似乎找不到任何信息。 任何幫助,將不勝感激!

您必須在腳本中指出語言。

例如,在Maps 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 = Japanese

還要使用動態語言設置檢查其工作示例

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM