反向地理编码

了解如何使用地理编码服务查找某个位置附近的地址

反向地理编码是将位置转换为地址或地点的过程。要反向地理编码,您可以使用地理编码服务reverseGeocode 操作。此操作需要一个初始位置,返回一个具有地名和位置等属性的地址。为了简化访问地理编码服务,可以使用 locator 模块。

在本教程中,演示使用 locator 进行反向地理编码,并在地图上查找最接近您所单击位置处的地址。

步骤

创建新 Pen

  1. 在此之前,先了解显示地图教程

添加模块

  1. 通过 require 引入locator 模块。

    GeoScene Maps SDK for JavaScript 提供了 AMD 模块ES 模块,本教程中我们是以 AMD 为例。AMD 使用require 函数加载模块 - 例如,您可以指定 "geoscene/Map" 来加载 Map 模块。加载后,它们将作为参数 (例如,Map) 传递给回调函数,以便在应用程序中使用。保持引用模块和回调参数的顺序相同是很重要的。有关不同类型模块的更多信息,请访问工具指南主题。

    展开
    代码块使用深色
                                                                                 
    添加行。更改行
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
          require([
            "geoscene/config",
            "geoscene/Map",
            "geoscene/views/MapView",
    
            "geoscene/rest/locator"
    
          ], function (geosceneConfig, Map, MapView, locator) {
    
    展开

定义服务 url

使用 locator 模块访问地理编码服务reverseGeocode 操作。

  1. 定义变量 serviceUrl ,设置其值为地理编码服务服务地址。

    展开
    代码块使用深色
                                                                                 
    添加行。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-78.50169, -0.21489],
              zoom: 12
            });
    
            const serviceUrl = "https://www.geosceneonline.cn/geocode/rest/GeoScene/GeocodeServer";
    
    展开

反向地理编码

点击地图,应用程序捕获地图上的点击位置,调用地理编码服务。如果找到对应地址,则服务返回该地址;如果未找到结果,则返回错误。在弹出窗口中使用纬度、经度和地址显示结果。

  1. 在主函数中,将 click 处理器添加至视图。定义 params ,将 location 设置为 evt.mapPoint

    展开
    代码块使用深色
                                                                                 
    添加行。添加行。添加行。添加行。添加行。添加行。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
            const serviceUrl = "https://www.geosceneonline.cn/geocode/rest/GeoScene/GeocodeServer";
    
            view.on("click", function (evt) {
              const params = {
                location: evt.mapPoint
              };
    
            });
    
    展开
  2. 定义 showAddress 函数,实现坐标和对应的地址弹窗显示。

    展开
    代码块使用深色
                                                                                 
    添加行。添加行。添加行。添加行。添加行。添加行。添加行。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
            view.on("click", function (evt) {
              const params = {
                location: evt.mapPoint
              };
    
            });
    
            function showAddress(address, pt) {
              view.openPopup({
                title: +Math.round(pt.longitude * 100000) / 100000 + ", " + Math.round(pt.latitude * 100000) / 100000,
                content: address,
                location: pt
              });
            }
    
    展开
  3. 更新 click 处理程序,调用 locationToAddress,对mapPoint 进行反向地理编码。使用 showAddress 函数显示结果的弹出窗口

    展开
    代码块使用深色
                                                                                 
    添加行。添加行。添加行。添加行。添加行。添加行。添加行。添加行。添加行。添加行。添加行。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
            view.on("click", function (evt) {
              const params = {
                location: evt.mapPoint
              };
    
              locator.locationToAddress(serviceUrl, params).then(
                function (response) {
                  // Show the address found
                  const address = response.address;
                  showAddress(address, evt.mapPoint);
                },
                function (err) {
                  // Show no address found
                  showAddress("No address found.", evt.mapPoint);
                }
              );
    
            });
    
    展开

运行应用程序

CodePen 中,运行代码以显示地图。

单击地图上任意位置进行反向地理编码,返回关于最近地址信息的弹出窗口

下一步是什么?

要了解如何使用其他 API 功能,请参阅以下教程:

您的浏览器不再受支持。请升级您的浏览器以获得最佳体验。