Get marker reference from geoJSON feature added with Data Layers API

839 views Asked by At

I'm using the Data Layers API to add geojson data to map.

But the problem is that the addGeoJson method returns a list of features, and I can't find an interface method to retrieve the marker obj reference associated with that feature.

I don't want to create a google Marker objects on the fly, add them to the map and remove the feature. It's an overkill.

I just want the proper approach before I move away from data layers and manage my own markers instead.

Thanks,

Seb.

1

There are 1 answers

1
Brice On

I have this issue as well. I couldn't find much in the API doc. You need a Marker object and you don't have one, you've got Features because you uploaded them to the data layer.

However, in the InfoWindow documentation , it says you can create an MVCObject with position set. I did this below in the event handler. You don't have to put it on the map, just handle the click event by creating the MVCObject and submitting that to the infoWindow constructor.

Example:

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