使用leaflet esri插件从数据库对地址进行地理编码

问题描述 投票:0回答:1

我正在尝试构建一个在数据库中使用地址名称并在传单地图中显示标记的地图应用程序。我遇到过传单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

leaflet geocoding esri
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.