Search code examples
javascriptgoogle-mapsgmaps4rails

Gmaps4Rails appending markers to existing map


I am trying to asynchonios loading maps to my already rendered map:

 handler = Gmaps.build('Google');
            handler.buildMap({ provider: {
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                styles: mapStyle_dark },
                internal: {
                    id: 'map'}}, function () {
                var json_array = $.getJSON("locations/async.json", function (data) {
                    return data;
                });
                var markers = handler.addMarkers(json_array);
                handler.fitMapToBounds();
                handler.getMap().setZoom(2);
            });

My ajax response looks like this:

[{"lat":48.21042800000001,"lng":16.3822238,"infowindow":{"id":"53a0a0012d289ec127000020","image":"veq3r13nbgn5qg9c7zqk","name":"Blended Shisha Lounge Bar","categories":[{"name":"Bars","slug":"bars","icon":"bars"}],"coordinates":[16.3822238,48.21042800000001]},"picture":{"anchor":[17,17],"url":"/assets/map/icons/categories/bars-38.png","width":38,"height":38}}]

But the markers get not appended.

What I am doing wrong?


Solution

  • replace:

    var json_array = $.getJSON("locations/async.json", function (data) {
       return data;
    });
    var markers = handler.addMarkers(json_array);
    

    with:

    $.getJSON("locations/async.json", function (json_array) {
       var markers = handler.addMarkers(json_array);
    });