【问题标题】:How to change language in mapbox如何在地图框中更改语言
【发布时间】:2019-10-29 10:06:50
【问题描述】:

我需要在javascript中更改mapbox的语言,我只在文档中看到以下代码

map.setLayoutProperty('country-label', 'text-field', ['get', 'name_de'])

但这行代码只会更改国家/地区名称,但我需要一切(城市、城镇……等)

【问题讨论】:

    标签: javascript reactjs mapbox mapbox-gl-js


    【解决方案1】:

    根据您的 mapbox 样式,会有不同的文本层。对于dark-v9,这些是可用的文本层。

    country-label
    state-label
    settlement-label
    settlement-subdivision-label
    airport-label
    poi-label
    water-point-label
    water-line-label
    natural-point-label
    natural-line-label
    waterway-label
    road-label 
    

    使用您在上述层的问题中提到的代码 sn-p,您应该能够更改语言。

    map.setLayoutProperty('country-label', 'text-field', ['get', 'name_de'])
    

    或者您可以使用mapbox-language-plugin 更改所有可能层的语言。这是插件的工作示例。

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset='utf-8' />
        <title>Change a map's language</title>
        <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
        <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.4.1/mapbox-gl.js'></script>
        <script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-language/v0.10.1/mapbox-gl-language.js'></script>
        <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.4.1/mapbox-gl.css' rel='stylesheet' />
        <style>
            body { margin:0; padding:0; }
            #map { position:absolute; top:0; bottom:0; width:100%; }
        </style>
    </head>
    <body>
    
    <style>
        #buttons {
            width: 90%;
            margin: 0 auto;
        }
        .button {
            display: inline-block;
            position: relative;
            cursor: pointer;
            width: 20%;
            padding: 8px;
            border-radius: 3px;
            margin-top: 10px;
            font-size: 12px;
            text-align: center;
            color: #fff;
            background: #ee8a65;
            font-family: sans-serif;
            font-weight: bold;
        }
    </style>
    <div id='map'></div>
    <ul id="buttons">
        <li id='button-fr' class='button'>French</li>
        <li id='button-ru' class='button'>Russian</li>
        <li id='button-de' class='button'>German</li>
        <li id='button-es' class='button'>Spanish</li>
    </ul>
    <script>
    mapboxgl.accessToken = 'pk.eyJ1IjoibXVyYWxpcHJhamFwYXRpIiwiYSI6ImNrMHA1d3VjYzBna3gzbG50ZjR5b2Zkb20ifQ.guBaIUcqkTdYHX1R6CM6FQ';
    var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/dark-v9',
        center: [16.05, 48],
        zoom: 2.9
    });
      mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.1.0/mapbox-gl-rtl-text.js');
      var mapboxLanguage = new MapboxLanguage({
      defaultLanguage: 'en'
    });
     
    map.addControl(mapboxLanguage);
      
    
    
    document.getElementById('buttons').addEventListener('click', function(event) {
        var language = event.target.id.substr('button-'.length);
        map.setStyle(mapboxLanguage.setLanguage(map.getStyle(), language));
      
    });
    
    </script>
    
    </body>
    </html>

    参考:https://blog.mapbox.com/how-to-localize-your-maps-in-mapbox-gl-js-da4cc6749f47

    【讨论】:

    • 感谢您的解释,我想获取所有层的列表,map.getStyle().layers 这给了我正确的数据。
    【解决方案2】:

    这是我发现对我有帮助的代码from github mapbox here

    map.getStyle().layers.forEach(function(thisLayer){
      if(thisLayer.type == 'symbol'){
        map.setLayoutProperty(thisLayer.id, 'text-field', ['get','name_ja'])
      }
    })
    

    我已经修改为

    map.getStyle().layers.forEach(function(thisLayer){
            console.log(thisLayer);
                if(thisLayer.id.indexOf('-label')>0){
                    console.log('change '+thisLayer.id);
                    map.setLayoutProperty(thisLayer.id, 'text-field', ['get','name_fr'])
                }
            });
    

    但否则你必须自己更改不同的标签,正如 murli-prajapati 提到的那样

    当然,用您自己的语言更改“name_xx”

    如:

    map.setLayoutProperty('country-label', 'text-field', ['get','name_fr']);
    map.setLayoutProperty('state-label', 'text-field', ['get','name_fr']);
    map.setLayoutProperty('settlement-label', 'text-field', ['get','name_fr']);
    map.setLayoutProperty('settlement-subdivision-label', 'text-field', ['get','name_fr']);
    

    【讨论】:

      【解决方案3】:

      唯一对我有帮助的是在 load 事件中放置了一个循环:

      map.on('load', function() {
                 let labels = [ 'country-label', 'state-label', 
                 'settlement-label', 'settlement-subdivision-label', 
                 'airport-label', 'poi-label', 'water-point-label', 
                 'water-line-label', 'natural-point-label', 
                 'natural-line-label', 'waterway-label', 'road-label' ];
                  
                  labels.forEach(label => {
                          map.setLayoutProperty(label, 'text-field', 'get','name_ru']);
                  });
       })
      

      语言插件和其他东西不起作用。

      【讨论】:

        【解决方案4】:

        后来,您可以使用此插件根据用户区域设置更改显示语言。

        https://github.com/mapbox/mapbox-gl-language

        例如:

        mapboxgl.accessToken = 'YOUR_ACCESS_TOKEN';
        const map = new mapboxgl.Map({
            container: 'map',
            style: 'mapbox://styles/mapbox/streets-v10',
            center: [-77.0259, 38.9010],
            zoom: 9
        });
        
        const language = new MapboxLanguage();
        map.addControl(language);
        

        【讨论】:

          猜你喜欢
          • 2018-07-29
          • 1970-01-01
          • 1970-01-01
          • 2021-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多