【问题标题】:Is "here maps javascript api" supporting GeoJSON or any file format alternative to KML“here maps javascript api”是否支持 GeoJSON 或任何替代 KML 的文件格式
【发布时间】:2013-08-20 12:46:29
【问题描述】:

是否可以以正确的方式将 GeoJson 格式的数据加载到这里 maps js api 中?我正在使用 AJAX 发送来自 mysql 的数据,该数据采用 GeoJSON 格式。我不想存储任何 kml 文件。

【问题讨论】:

    标签: kml geojson here-api


    【解决方案1】:

    legacy 2.x API 中没有直接可用的 GeoJSON 解析器,您必须自己编写。由于 Google Maps API 存在外部 GeoJSON parser library,因此只需将 Google 特定的地图对象替换为等效的 HERE 地图对象即可。

    我在原始端口的基础上创建了一个端口,但保留了 HERE 地图语法 here

    GeoJSONContainer的基本用法是通过parseGeoJSON()方法,如下:

    function extend(B, A) {
        function I() {}
        I.prototype = A.prototype;
        B.prototype = new I();
        B.prototype.constructor = B;
    }
    
    function createGeoJsonParser(){
        extend(GeoJSONContainer, nokia.maps.map.Container);
        parser = new GeoJSONContainer();
    
    }
    
    function parseJson(jsonObject){
    
        result = parser.parseGeoJSON(jsonObject);   
        if (parser.state == "finished") {
            map.objects.addAll(result);
            map.set("center", map.objects.get(0).getBoundingBox().getCenter());
    
            map.addListener("click" ,  function(evt) {
                var text = JSON.stringify(evt.target.properties);
                bubble = infoBubbles.addBubble(text!== undefined ? 
                           text : "properties undefined",
                     evt.target.getBoundingBox().getCenter());
            }, false);
        } else {
            console.log(result);
        }
    }
    

    查看链接:Simple geoJSON parsing

    由于 GeoJSONContainerContainer 的扩展,您也可以使用 addGeoJSON()

    将 geoJSON 数据直接添加到地图上>
    var err = resultSet.addGeoJSON(jsonManager.object);
    if (resultSet.state == "finished") {
        map.zoomTo(container.getBoundingBox());
        container.addListener("click" ,  function(evt) {
            infoBubbles.addBubble(evt.target.properties.Description,
             evt.target.getBoundingBox().getCenter());
        }, false);
    } else {
        alert(err);
    }
    

    基本示例用法: - Russia Provinces

    您也可以像 clustering 组件一样添加和设置数据点样式

    样式示例:

    当然,它没有任何保证。

    对于当前的 3.x API,geojson 阅读器作为标准包含在内,您可以参考以下 fxxxit 的回答:

    var reader = new H.data.geojson.Reader('/path/to/geojson/file.json');
    reader.parse();
    //assuming that map already exists
    map.addLayer(reader.getLayer());
    

    【讨论】:

      【解决方案2】:

      似乎有一个“新”版本的 API 允许您直接获取 GeoJSON 数据 (https://developer.here.com/documentation/maps/api_reference/H.data.geojson.Reader.html)

      var reader = new H.data.geojson.Reader('/path/to/geojson/file.json');
      reader.parse();
      //assuming that map already exists
      map.addLayer(reader.getLayer());
      
      猜你喜欢
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 2020-02-03
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2017-09-16
      相关资源
      最近更新 更多