【问题标题】:Geocoding Address name from Database using leaflet esri plugin使用传单 esri 插件从数据库中地理编码地址名称
【发布时间】:2019-05-22 11:37:22
【问题描述】:

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

【问题讨论】:

    标签: javascript leaflet geocoding esri


    【解决方案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

    【讨论】:

    • 谢谢kboul,但地理编码方法是什么?不是 L.esri.Geocoding.geocode() 吗?
    • geocode 方法初始化一个新的地理编码任务,类似于L.esri.Geocoding.geocode(&lt;Object&gt; options),但以 ES6 方式。当您使用import 导入文件时,您会创建一个namespace,然后您可以访问所有可用的方法,就像使用 vanilla js 一样。另请查看answer,了解如何使用 ES6 导入添加外部库。
    • @NigelNg 如果对您有帮助,请接受答案,以便其他用户在遇到相同问题时可以找到您的问题。
    • 感谢提醒,这里的栈溢出新手。
    猜你喜欢
    • 2021-01-04
    • 1970-01-01
    • 2014-02-25
    • 2012-11-06
    • 1970-01-01
    • 2016-10-08
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多