我试图更改功能集合数据覆盖的特定特性的样式。这是我的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));
而我总是在控制台中得到“未定义”。
我在这里做错什么了?
谢谢,
罗伯特
发布于 2016-10-04 14:18:41
您需要在回调函数中调用map.data.getFeatureById(1) (因此它不会在加载GeoJson之前执行)。
来自文献资料
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', {},
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?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>
https://stackoverflow.com/questions/39852468
复制相似问题