Search code examples
jqueryjsongoogle-mapsjquery-gmap3

Gmap 3- How to use Ajax Json Request for Markers + have click event?


I am wondering how can I turn this

   $(function(){

        $('#test1').gmap3({
          map:{
            options:{
              center:[46.578498,2.457275],
              zoom: 5
            }
          },
          marker:{
            values:[
              {latLng:[48.8620722, 2.352047], data:"Paris !"},
              {address:"86000 Poitiers, France", data:"Poitiers : great city !"},
              {address:"66000 Perpignan, France", data:"Perpignan ! <br> GO USAP !", options:{icon: "http://maps.google.com/mapfiles/marker_green.png"}}
            ],
            options:{
              draggable: false
            },
            events:{
              mouseover: function(marker, event, context){
                var map = $(this).gmap3("get"),
                  infowindow = $(this).gmap3({get:{name:"infowindow"}});
                if (infowindow){
                  infowindow.open(map, marker);
                  infowindow.setContent(context.data);
                } else {
                  $(this).gmap3({
                    infowindow:{
                      anchor:marker, 
                      options:{content: context.data}
                    }
                  });
                }
              },
              mouseout: function(){
                var infowindow = $(this).gmap3({get:{name:"infowindow"}});
                if (infowindow){
                  infowindow.close();
                }
              }
            }
          }
        });
      });

To something where the markers are loaded via ajax? I also need the events to stay as the data(more that "great city") I need to pull is to big and needs to be in demand

I am guessing on the server side I will need something like

 public class Marker
    {
        public string Title { get; set; }
        public double Lat { get; set; }
        public double Lng { get; set; }
    }

Solution

  • A naive but (I think) perfectly serviceable solution would be to abstract the map initialisation logic into a function, let's call it initMap, which takes an array of markers as a parameter:

    function initMap(markers) {
        $('#test1').gmap3({
          map:{
            options:{
              center:[46.578498,2.457275],
              zoom: 5
            }
          },
          marker:{
            values: markers || [], // Pass it an empty array if no markers are specified
            options:{
              draggable: false
            }
    
         ..............
    }
    

    You can now initialise the map on document ready (with either default marker data or none) and within the success handler for your AJAX call:

    $('button').on('click', function() {
        $.ajax({
            url: 'marker.json'
        }).done(function(data) {
            // Re-initialise the map with loaded marker data
            initMap(data);
        });
    });
    

    You want the server to spit out JSON. Something like the following static example (which you could happily use to test until you get the back-end set up):

    [
        { 
            "latLng":[48.8620722, 2.352047], 
            "data":"Paris !" },
        { 
            "address":"86000 Poitiers, France", 
            "data":"Poitiers : great city !" 
        },
        { 
            "address":"66000 Perpignan, France", 
            "data":"Perpignan ! <br> GO USAP !", 
            "options": {
                "icon": "http://maps.google.com/mapfiles/marker_green.png"
            }
        }
    ]
    

    You could look into updating the markers more atomically as I'm sure the plugin will allow you to add / remove markers after it has been initialised, however the solution I've outlined may be good enough for your purposes.