Google Maps API:getFeatureById功能集合无效 [英] Google Maps API: getFeatureById for feature collection not working

查看:717
本文介绍了Google Maps API:getFeatureById功能集合无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试更改特征集合数据叠加层的特定功能的样式。这是我的json的一个片段:

  {type:FeatureCollection ,features:[{type:Feature,id:1,properties:{name:1 CBD  -  Bankenviertel,color:transparent,isHovered false,isActive:false},geometry:{type:Polygon,coordinates:[[[[8.67279430349,50.1143807311]] [8.67280054398,50.1143975981]]}}  



这是我的map.js中的相关片段



  map.data.loadGeoJson('some.json'); console.log(map.data.getFeatureById(1));  



我总是在控制台中看到undefined。 b

我在这里做错了什么?



谢谢,

您需要调用 map.data.getFeatureById(1)内部的

解决方案回调函数(所以它不会在GeoJson加载之前执行)。

来自

/javascript/3.exp/reference#Datarel =nofollow>文档


loadGeoJson (url:string,options?:Data.GeoJsonOptions, callback?:function(Array)

返回值:无



加载GeoJS


概念证明小提琴



代码段:

  var geocoder; var map; function initialize(){var map = new google.maps.Map(document.getElementById(map_canvas),{ zoom:4,center:{lat:-28,lng:137},mapTypeId:google.maps.MapTypeId.ROADMAP}); // map.data.addGeoJson(geoJson); map.data.loadGeoJson('https://api.myjson.com/bins/1teyu',{},函数(features){console.log(map.data.getFeatureById(1)); console.log(map。 data.getFeatureById(1).getProperty(letter));});} google.maps.event.addDomListener(window,load,initialize);  

html,body,#map_canvas {height:100%;宽度:100%; margin:0px; < script src =https://


I try to change the style of a specific feature of a feature collection data overlay. This is a snippet of my json:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": 1,
      "properties": {
        "name": "1 CBD - Bankenviertel",
        "color": "transparent",
        "isHovered": false,
        "isActive": false
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              8.67279430349,
              50.1143807311
            ],
            [
              8.67280054398,
              50.1143975981
            ]
        ]
        ]
      }
    }

and this is the relevant snippet from my map.js

map.data.loadGeoJson('some.json');
console.log(map.data.getFeatureById(1));

And I am always getting "undefined" in the console.

What am I doing wrong here?

Thanks,

Robert

解决方案

You need to call map.data.getFeatureById(1) inside the callback function (so it doesn't execute before the GeoJson has loaded).

from the documentation:

loadGeoJson(url:string, options?:Data.GeoJsonOptions, callback?:function(Array))

Return Value: None

Loads GeoJS

proof of concept fiddle

code snippet:

var geocoder;
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      zoom: 4,
      center: {
        lat: -28,
        lng: 137
      },
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  // map.data.addGeoJson(geoJson);
  map.data.loadGeoJson(
    'https://api.myjson.com/bins/1teyu', {},
    function(features) {
      console.log(map.data.getFeatureById(1));
      console.log(map.data.getFeatureById(1).getProperty("letter"));
    });
}
google.maps.event.addDomListener(window, "load", initialize);

html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

这篇关于Google Maps API:getFeatureById功能集合无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆