使用GeoJson进行Google Maps GeoComplete

时间:2016-05-31 14:20:53

标签: javascript google-maps meteor google-maps-api-3 geocomplete

我正在努力让Google地图向我展示存储在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触发事件或将标记放到地图上?我有点像菜鸟,非常感谢任何帮助。

由于 麦克

1 个答案:

答案 0 :(得分: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