显示 Web 地图

字数统计: 812
阅读时长: 约 2 分钟
当前版本: 4.29

了解使用 web 地图显示存储在 GeoScene Online 或 GeoScene Enterprise 中的地图。

Web 地图是存储在 GeoScene Online 或 GeoScene Enterprise 中的项目。Web 地图项目包含地图的配置信息 (JSON 格式),如底图图层、数据图层、图层样式和弹出窗口等信息。应用程序可以通过其项目 ID 访问和显示 Web 地图。在本教程中,演示加载并显示存储在 GeoScene Online 中的web 地图。

步骤

创建新 Pen

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

添加模块

  1. require 语句中,删除 Map 模块。添加 WebMapScaleBarLegend 模块。

    更多内容

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

    html
    <script>
    require([
        "geoscene/config",
        "geoscene/Map",
        "geoscene/WebMap",
        "geoscene/views/MapView",
        "geoscene/widgets/ScaleBar",
        "geoscene/widgets/Legend"
    ], function(geosceneConfig, WebMap, MapView, ScaleBar, Legend) {

加载 web 地图

使用门户项目 ID 创建 WebMap。然后将Web 地图赋予视图。

  1. 创建 WebMap。将 portalItem ID 设置为 2a78a76e9a4144f9873355367ae76e26

    js
    const webmap = new WebMap({
        portalItem: {
            id: "2a78a76e9a4144f9873355367ae76e26"
        }
    });
  2. 使用 webmap 元素更新 MapView 中的 map 属性。移除 centerzoom 属性,因 Web 地图提供定位信息。

    js
    const webmap = new WebMap({
        portalItem: {
            id: "2a78a76e9a4144f9873355367ae76e26"
        }
    });
    
    const view = new MapView({
        container: "viewDiv",
    
        map: map,
        center: [-118.80500,34.02700],
        zoom: 13,
    
        map: webmap
    });
  3. 在右上角,单击运行以验证 Web 地图是否已成功加载并显示。

添加微件

添加 LegendScaleBar微件。Legend 微件显示视图中可见图层的图例。ScaleBar 以公制或非公制值显示比例尺。

  1. 创建 ScaleBar。将 scalebar 添加到 view 的左下角 bottom-left

    js
    const view = new MapView({
        container: "viewDiv",
        map: webmap
    });
    
    const scalebar = new ScaleBar({
        view: view  
    });
    
    view.ui.add(scalebar, "bottom-left");
  2. 创建 Legend。将 legend 添加到 view 的右上角 top-right

    js
    const scalebar = new ScaleBar({
        view: view
    });
    
    view.ui.add(scalebar, "bottom-left");
    
    const legend = new Legend ({
        view: view
    });
    view.ui.add(legend, "top-right");

运行应用程序

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

现在,左下角有个比例尺,右上角有地图的图例信息。

下一步是什么?

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