使用Leaflet ESRI插件对数据库中的地址名称进行地理编码

时间:2018-12-22 08:48:03

标签: leaflet geocoding esri

我正在尝试构建一个在数据库中使用地址名称并在传单地图中显示标记的地图应用。我遇到过传单esri插件,但不确定如何使用代码。有人可以教我如何从地址解析功能中提取结果(经度和纬度)并绘制标记吗?谢谢!

地理编码功能

L.esri.Geocoding.geocode(<Object> options)

结果:

{results: [
    {
      latlng: L.LatLng,
      text: 'Formatted Address',
      score: 100, // certainty ranking of the match
      properties: {
        // additional info like specific address components (Country Code etc.)
      }
    }
  ]
}

http://esri.github.io/esri-leaflet/api-reference/tasks/geocode.html

1 个答案:

答案 0 :(得分:1)

以下是使用ES6的示例:

import L from "leaflet";
// import library as ELG
import * as ELG from "esri-leaflet-geocoder";

// here is an example address in the US - use the one from your DB
const address = "380 New York St, Redlands, California, 92373";

// call geocode method of the library, no need to call L.esri.Geocoding.geocode() as in vanilla js
ELG.geocode()
  // pass the address
  .text(address)
  .run((err, results, response) => {
    console.log(results.results[0].latlng);
    // retrieve latitude, longitude from related response
    const { lat, lng } = results.results[0].latlng;
    // build a marker using the retrieved address
    L.marker([lat, lng])
      .addTo(mymap)
      .bindPopup(address)
      .openPopup();
});

Demo