2016-08-22 143 views
0

我使用mapbox-gl的API來創建一個彈出式地圖框視圖。我想知道我該怎麼讓我的座標(birdLocation變量)作爲地址,如:紐約,紐約11208mapbox-gl:在彈出窗口中顯示反向地理編碼結果?

renderMap() { 
    if (this.props.bird.location) { 
     let birdLocation = this.props.bird.location; 
     let map = new mapboxgl.Map({ 
      container: 'mapbox-container', 
      style: config.mapbox.style, 
      center: birdLocation, 
      zoom: 13, 
      interactive: false, 
      preserveDrawingBuffer: true 
     }); 
     let popup = new mapboxgl.Popup() 
      .setLngLat(birdLocation) 
      .setHTML(
       '<div class="location-popup location-title">BIRD LOCATION</div><div class="location-popup location-address">' + {birdLocation} + '</div><a href="/trips" class="location-popup location-plan">PLAN A TRIP</a>' 
      ) 
      .addTo(map); 

     map.on('load', function() { 
      map.addSource("points", { 
       "type": "geojson", 
       "data": { 
        "type": "FeatureCollection", 
        "features": [{ 
         "type": "Feature", 
         "geometry": { 
          "type": "Point", 
          "coordinates": birdLocation 
         }, 
         "properties": { 
          "icon": "dark-map-pin" 
         } 
        }] 
       } 
      }); 

      map.addLayer({ 
       "id": "points", 
       "type": "symbol", 
       "source": "points", 
       "layout": { 
        "icon-image": "{icon}" 
       } 
      }); 
     }); 
    } 
}, 

回答

1

什麼你要找的是反向地理編碼,這變成一個座標,如birdLocation,轉換爲名稱和/或地址。這裏是Mapbox's API for reverse geocoding,你可以使用任何AJAX庫來獲得結果。

披露:我在Mapbox工作

+0

我查看了API中的反向地理編碼,任何想法如何使用它打印位置? – eclipsis

+0

查看響應格式https://www.mapbox.com/api-documentation/#response-format - 該位置將位於'response.features [0] .place_name'中。你會得到迴應,在那裏找到地名,然後插入它。 – tmcw

+0

你可以幫我創建例子嗎?到目前爲止,我有這個:'let mapboxGeocoder = new Geocoder()' – eclipsis