0

我很努力让谷歌地图显示存储在GeoJSON对象中的数据。如果我在多边形上使用点击事件,它首次运行。代码如下:谷歌地图GeoComplete与GeoJson

// 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触发事件或将标记放到地图上?我有点小菜,真的很感谢任何帮助。

感谢 迈克

+0

是否有'geo.json'和geocomplete之间的关系?我不相信你可以通过谷歌以外的其他地址定位地址 – amenadiel

回答

0

没有人有任何深入了解如何触发从DOM或删除标记到地图上的事件?

当然,有。 Google Maps JS API有一个与map events和地图标记一起使用的完整文档示例。

在此示例中,标记将放在您使用'点击事件'点击的地图上。

// 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 
    }); 
} 

完整的示例是here

下面是一个样本的链接监听DOM事件:

https://developers.google.com/maps/documentation/javascript/examples/event-domListener