[英]Google Maps GeoComplete with GeoJson
我正在努力让Google Maps向我展示存储在GeoJSON对象中的数据。 如果我在多边形上使用click事件,它将在第一次运行。 代码如下:
// Get the GeoJSON file from the server
HTTP.get(Meteor.absoluteUrl("/geo.json"), function(err,result) {
GoogleMaps.maps.fibreMap.instance.data.loadGeoJson("/geo.json");
});
// Add style and colouring to the map
GoogleMaps.maps.fibreMap.instance.data.setStyle(function(feature) {
// Get the Fibrehood Status
var status = feature.getProperty('status');
// Add colour accoring to status
if (status == "live") {
opacity = 0.65;
} else if (status == "build") {
opacity = 0.4;
} else if (status == "register_i") {
opacity = 0.2;
}
// Return the correct styling
return ({
fillColor: '#ec008c',
strokeColor: '#ec008c',
strokeOpacity: 0.35,
strokeWeight: 0,
fillOpacity: opacity
});
});
GoogleMaps.maps.fibreMap.instance.data.addListener('click', function(event) {
var hood = event.feature.getProperty('name');
var status = event.feature.getProperty('status');
console.log(hood + " : " + status);
});
但是,当尝试使用GeoComplete在地址上放置图钉时,它不会运行。 我知道应该通过某种事件来触发此事件,例如将标记放到地图上或更改Dom元素,但我无法弄清楚。
是否有人对如何从DOM触发事件或在地图上放置标记有任何见识? 我有点菜鸟,非常感谢您的帮助。
谢谢迈克
是否有人对如何从DOM触发事件或在地图上放置标记有任何见识?
当然可以。 Google Maps JS API有一个记录良好的示例,用于处理地图事件和地图标记。
在此示例中,标记将放置在您使用“点击事件”点击的地图上。
// This event listener calls addMarker() when the map is clicked.
google.maps.event.addListener(map, 'click', function(event) {
addMarker(event.latLng, map);
});
// Add a marker at the center of the map.
addMarker(bangalore, map);
}
// Adds a marker to the map.
function addMarker(location, map) {
// Add the marker at the clicked location, and add the next-available label
// from the array of alphabetical characters.
var marker = new google.maps.Marker({
position: location,
label: labels[labelIndex++ % labels.length],
map: map
});
}
完整的演示在这里 。
这是指向侦听DOM事件的示例的链接:
https://developers.google.com/maps/documentation/javascript/examples/event-domListener
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.