【问题标题】:why my feature object can not use feature.getGeometry method?为什么我的特征对象不能使用 feature.getGeometry 方法?
【发布时间】:2017-03-12 14:25:44
【问题描述】:

当我绘制几何图形时,我想在边界内添加几何标签,并将站点添加到谷歌地图上 geojson 层边界的中心。

map.data.addListener("addfeature", function(feature){

var g=feature.getGeometry();
console.log("test ok");
//var bounds = new google.maps.LatLngBounds();
console.log(g.get());
return feature;
});

我根据 google 开发参考 (https://developers.google.com/maps/documentation/javascript/reference#Data) 添加带有监听器 addfeature 的 map.data

根据page(https://developers.google.com/maps/documentation/javascript/reference#Data.AddFeatureEvent),函数catch up addfeature listener with argument应该是feature object。

根据要素类页面(https://developers.google.com/maps/documentation/javascript/reference#Data.Feature),要素对象应该有getGeometry()方法。

但是当我执行上面的代码时,我从浏览器中得到一个错误。

Uncaught TypeError: feature.getGeometry is not a function
at He.<anonymous> (bakb.html:37)
at qe._.y.trigger (js?sensor=true&callback=myMap:104)
at qe.<anonymous> (js?sensor=true&callback=myMap:38)
at Object._.y.trigger (js?sensor=true&callback=myMap:104)
at qe._.k.add (js?sensor=true&callback=myMap:116)
at js?sensor=true&callback=myMap:69
at Object._.ab (js?sensor=true&callback=myMap:35)
at Object._.re (js?sensor=true&callback=myMap:69)
at Ub (data.js:11)
at OF (util.js:136)

我通过以下方式调用google map api:

<script src="//maps.google.com/maps/api/js?sensor=true&callback=myMap></script>

我认为我正确使用了谷歌地图。 但是为什么没有定义 feature.getGeometry 方法呢? 如何从特征中获取几何?回调函数中的特征争论真的是特征对象吗?如果不是如何从争论中获取几何?

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 gis geojson


    【解决方案1】:

    addfeature 事件监听器返回一个addFeatureEvent object,它有一个特性属性,它本身不是一个特性。

    Data.AddFeatureEvent 对象规范
    google.maps.Data.AddFeatureEvent 对象规范
    addfeature 事件的属性。

    属性 功能 |类型:Data.Feature
    添加到 FeatureCollection 的特征。

    map.data.addListener("addfeature", function(event) {
      var g = event.feature.getGeometry();
      console.log("test ok");
      //var bounds = new google.maps.LatLngBounds();
      // assuming all the features are points
      console.log(g.get());
    });
    

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 2015-02-18
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多