gpt4 book ai didi

javascript - 如何访问 Google map 上放置的 geoJSON 数据中的数据对象

转载 作者:行者123 更新时间:2023-11-27 23:20:59 25 4
gpt4 key购买 nike

我做了什么:我正在使用 Google map Javascript API。我的地理数据存储在 geoJSON 文件中。我已使用数据图层将数据放置在 map 上。我制作了一个用于显示 popUpWindows 的 clickEvent。

我想要什么:我只想在“类别”属性中具有“学校”值的标记上显示一个圆圈。

我的 geoJSON 看起来像这样:

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"category": "School",
"name":"De Hooge Waai",
"popupContent": "Basisschool De Hooge Waai in Raamsdonk",
"icon": "http://maps.google.com/mapfiles/kml/paddle/S.png"
},
"geometry": {
"type": "Point",
"coordinates": [4.905370,51.686385]
}
},
{
"type": "Feature",
"properties": {
"category": "Museum",
"name":"Landgoed Het Broeck",
"popupContent": "Landgoed 'Het Broeck heeft rijtuigmuseum",
"icon": "http://maps.google.com/mapfiles/kml/paddle/M.png"
},
"geometry": {
"type": "Point",
"coordinates": [4.900267,51.686103]
}
}
]
}

我的 JavaScript 看起来像这样:

<script>
function initMap() {

//----------
// Map
//----------
var mapOptions = {
zoom: 15,
center:{lat: 51.687762, lng: 4.909900}
};

var map = new google.maps.Map(document.getElementById("map"),mapOptions);

//-----------
// Assets:
//-----------
infowindow = new google.maps.InfoWindow({
content: ""
});
var regionCircle = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
radius: 500
});


// JSON:
map.data.loadGeoJson('test.json');


// ICON:
map.data.setStyle(function(feature) {
return ({
icon:{
url:feature.getProperty('icon'),
scaledSize: new google.maps.Size(32, 32)
}
});
});

//---------------
// Events:
//---------------
map.data.addListener('click', function(event) {
var myHTML = "<h1>"+event.feature.getProperty("category")+
"</h1><h2>"+event.feature.getProperty("name")+"</h2>" +
event.feature.getProperty("popupContent");
infowindow.setContent(myHTML);
infowindow.setPosition(event.feature.getGeometry().get());
infowindow.setOptions({pixelOffset: new google.maps.Size(0,-30)});
infowindow.open(map);


});
google.maps.event.addListener(map,'click',function() {
infowindow.close();
});
}
</script>

问题:如何才能做到这一点?

最佳答案

一种选择是在 DataLayer 的“addfeature”事件上使用监听器。请注意,javascript 区分大小写,因此“school”与“School”不同。

map.data.addListener('addfeature', function(evt) {
if (evt.feature.getProperty('category') == "School") {
var regionCircle = new google.maps.Circle({
center: evt.feature.getGeometry().get(),
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
radius: 500,
map: map
});
}
});

proof of concept fiddle

代码片段:

function initMap() {

//----------
// Map
//----------
var mapOptions = {
zoom: 14,
center: {
lat: 51.687762,
lng: 4.909900
}
};

var map = new google.maps.Map(document.getElementById("map"), mapOptions);

//-----------
// Assets:
//-----------
infowindow = new google.maps.InfoWindow({
content: ""
});
var regionCircle = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
radius: 500
});


map.data.addListener('addfeature', function(evt) {
if (evt.feature.getProperty('category') == "School") {
var regionCircle = new google.maps.Circle({
center: evt.feature.getGeometry().get(),
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
radius: 500,
map: map
});
}
});

// JSON:
map.data.addGeoJson(geoJson);
// ICON:
map.data.setStyle(function(feature) {
return ({
icon: {
url: feature.getProperty('icon'),
scaledSize: new google.maps.Size(32, 32)
}
});
});

//---------------
// Events:
//---------------
map.data.addListener('click', function(event) {
var myHTML = "<h1>" + event.feature.getProperty("category") +
"</h1><h2>" + event.feature.getProperty("name") + "</h2>" +
event.feature.getProperty("popupContent");
infowindow.setContent(myHTML);
infowindow.setPosition(event.feature.getGeometry().get());
infowindow.setOptions({
pixelOffset: new google.maps.Size(0, -30)
});
infowindow.open(map);


});
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
}
google.maps.event.addDomListener(window, "load", initMap);
var geoJson = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"category": "School",
"name": "De Hooge Waai",
"popupContent": "Basisschool De Hooge Waai in Raamsdonk",
"icon": "http://maps.google.com/mapfiles/kml/paddle/S.png"
},
"geometry": {
"type": "Point",
"coordinates": [4.905370, 51.686385]
}
}, {
"type": "Feature",
"properties": {
"category": "Museum",
"name": "Landgoed Het Broeck",
"popupContent": "Landgoed 'Het Broeck heeft rijtuigmuseum",
"icon": "http://maps.google.com/mapfiles/kml/paddle/M.png"
},
"geometry": {
"type": "Point",
"coordinates": [4.900267, 51.686103]
}
}]
};
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map"></div>

关于javascript - 如何访问 Google map 上放置的 geoJSON 数据中的数据对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35366806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com