admin管理员组文章数量:1305712
How to set Google Map's feature's id
from geojson?
id
is what returned by getId()
function.
The following code does not work (prints undefined
) although id
property exists in properties:
var uluru = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var data = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [131.044, -25.363]
},
"properties": {
"Id": 12
}
};
map.data.addGeoJson(data);
map.data.forEach(function(feature){
console.log("Id from Google's feature: " + feature.getId());
});
Fiddle: /
UDPATE
I can write
var data = {
"type": "Feature",
"id": 13,
"geometry": {
"type": "Point",
"coordinates": [131.044, -25.363]
},
but won't this be incorrect Geojson?
How to set Google Map's feature's id
from geojson?
id
is what returned by getId()
function.
The following code does not work (prints undefined
) although id
property exists in properties:
var uluru = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var data = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [131.044, -25.363]
},
"properties": {
"Id": 12
}
};
map.data.addGeoJson(data);
map.data.forEach(function(feature){
console.log("Id from Google's feature: " + feature.getId());
});
Fiddle: https://jsfiddle/dimskraft/hj2t5je3/5/
UDPATE
I can write
var data = {
"type": "Feature",
"id": 13,
"geometry": {
"type": "Point",
"coordinates": [131.044, -25.363]
},
but won't this be incorrect Geojson?
Share Improve this question edited Jun 30, 2018 at 20:20 xomena 32.2k6 gold badges96 silver badges125 bronze badges asked Apr 8, 2017 at 17:43 DimsDims 51.2k130 gold badges358 silver badges652 bronze badges 1- indeed it would be incorrect, it should be in the properties object – Kat Lim Ruiz Commented Feb 4, 2021 at 19:49
1 Answer
Reset to default 11You should pass a second parameter to addGeoJson() method that defines what propery in your GeoJSON will be used as id.
The second parameter is Data.GeoJsonOptions object
https://developers.google./maps/documentation/javascript/reference#Data.GeoJsonOptions
So the change will be:
map.data.addGeoJson(data, {
idPropertyName: "Id"
});
Code snippet
function initMap() {
var uluru = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var data = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [131.044, -25.363]
},
"properties": {
"Id": 12
}
};
map.data.addGeoJson(data, {
idPropertyName: "Id"
});
map.data.forEach(function(feature){
console.log("Id from Google's feature: " + feature.getId());
});
}
#map {
height: 400px;
width: 100%;
}
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer
src="https://maps.googleapis./maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&callback=initMap">
</script>
本文标签: javascriptHow to set Google Map39s feature ID from geojsonStack Overflow
版权声明:本文标题:javascript - How to set Google Map's feature ID from geojson? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741809986a2398734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论