0

我想本地化由Gmaps4rails生成的地圖,這樣我就可以在用戶所需的語言中顯示地名。 Google文檔如何在此處執行此操作:https://developers.google.com/maps/documentation/javascript/examples/map-languageGmaps4rails - 如何設置地圖語言

我正在使用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' })。

我已經搜遍了文檔(和源代碼),但似乎無法找到關於此的任何信息。任何幫助,將不勝感激!

回答

0

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

例如在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&callback=initMap&language=ja&region=JP,語言設置爲JP =日本

還檢查他們的工作sample與動態語言設置。

希望這會有所幫助!

+1

Thanks @ Mr.Rebot,我認爲可能有辦法通過gem來處理這個問題,但是因爲我可以在rails中動態編輯src,所以我可能會這樣做。感謝您的發佈。 – vaughanos