【问题标题】:Get marker reference from geoJSON feature added with Data Layers API从使用 Data Layers API 添加的 geoJSON 功能获取标记参考
【发布时间】:2014-12-22 04:55:48
【问题描述】:

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

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

我不想动态创建谷歌标记对象,将它们添加到地图并删除该功能。这有点过头了。

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

谢谢,

Seb.

【问题讨论】:

    标签: data-layers


    【解决方案1】:

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

    但是,在 InfoWindow documentation 中,它说您可以创建一个设置了位置的 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);
        }); 
    }
    

    【讨论】:

    • 我相信 AF 是缩小的内部名称,它们对我来说是不同的。相反,我做了fakeMarker.set('position', event.latLng)
    猜你喜欢
    • 2011-12-03
    • 1970-01-01
    • 2019-03-11
    • 2020-08-02
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    相关资源
    最近更新 更多