Mapbox geocoder.reverseQuery如何工作?

时间:2018-06-27 19:07:52

标签: mapbox geocoding reverse-geocoding geocoder

有一个有关如何使用Mapbox反向查询的示例。我在geocoder.query上找到了一个示例,但似乎找不到反向查询的有效示例。

以下示例将地址转换为lat long,我想学习如何使用反向查询将lat long转换为地址。反向查询需要输入[lon,lat],但是我不确定如何获取结果并将其显示为地址。

https://www.mapbox.com/mapbox.js/api/v3.1.1/l-mapbox-geocoder/

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Use geocoder to set map position</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v3.1.1/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v3.1.1/mapbox.css' rel='stylesheet' />
<style>
  body { margin:0; padding:0; }
  #map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
L.mapbox.accessToken = '<your access token here>';
var geocoder = L.mapbox.geocoder('mapbox.places'),
    map = L.mapbox.map('map', 'examples.map-h67hf2ic');

geocoder.query('Chester, NJ', showMap);

function showMap(err, data) {
    // The geocoder can return an area, like a city, or a
    // point, like an address. Here we handle both cases,
    // by fitting the map bounds to an area or zooming to a point.
    if (data.lbounds) {
        map.fitBounds(data.lbounds);
    } else if (data.latlng) {
        map.setView([data.latlng[0], data.latlng[1]], 13);
    }
}
</script>
</body>
</html>

0 个答案:

没有答案