2016-05-31 106 views
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