1

我正在使用数据层 API 将 geojson 数据添加到地图。

但问题是 addGeoJson 方法返回一个特征列表,我找不到接口方法来检索与该特征关联的标记 obj 引用。

我不想动态创建谷歌标记对象,将它们添加到地图并删除该功能。这是矫枉过正。

在我离开数据层并管理我自己的标记之前,我只想要正确的方法。

谢谢,

赛博。

4

1 回答 1

1

我也有这个问题。我在 API 文档中找不到太多内容。您需要一个 Marker 对象,但您没有,您拥有 Features,因为您将它们上传到数据层。

但是,在InfoWindow 文档中,它说您可以创建一个带有位置集的 MVCObject。我在下面的事件处理程序中做了这个。您不必将它放在地图上,只需通过创建 MVCObject 并将其提交给 infoWindow 构造函数来处理单击事件。

例子:

function initialize(mapID) {
    var myLatlng = new google.maps.LatLng(38.951644, -92.334127);
    var mapOptions = {
        zoom: 12,
        center: myLatlng
    }
    var map = new google.maps.Map(document.getElementById(mapID), mapOptions);
    map.data.addGeoJson(realestate_js.geojson_results);
    map.data.setStyle(function(feature){
        // setStyle executes this function on each feature...
        // each feature then receives the "google.maps.Data.StyleOptions" object
        // which defines its icons and stuff.
        // https://developers.google.com/maps/documentation/javascript/3.exp/reference#Data.StyleOptions
        return {
            'clickable':true,
            'icon':feature.A.icon,
            'title':feature.A.address1,
        }
    });
    var fakeMarker = new google.maps.MVCObject();
    var infoWindow = new google.maps.InfoWindow({
        'content':"",
    });
    map.data.addListener('click', function(event){
        fakeMarker.set('position', {'lat':event.latLng.A, 'lng':event.latLng.F});
        var infoWindow = new google.maps.InfoWindow({
            'content':"<h1>Info Window Content here</h1><p>omg this so text</p>" + ,
        });
        infoWindow.open(map, fakeMarker);
    }); 
}
于 2015-05-15T18:55:03.630 回答