Google maps load geojson with ajax

google maps -- Posted on Jan. 21, 2018

Google maps load geojson with ajax

              
                function populateGeometry(url,type,data,datatype, infowindow){
  /*
  url : the url link for ajax Call
  type: method post or get
  data: a dict {},
  datatype: xml or json,
  infowindow: google maps InfoWindow
  */
  $.ajax({
      url: url,
      type: type,
      data: data,
      datatype: datatype,
      success: function(data)
      {
          $('#loading').hide();
          for (var i = 0; i < data.features.length; i++){
  	     map.data.addListener('click', function(event) {
  		var myHTML = event.feature.getProperty("name");
  		var mytype = event.feature.getGeometry().getType();
  		infowindow.setContent("<div style='width:150px; text-align: center;'>"+myHTML+"</div>");
                infowindow.setPosition(event.latLng);
		infowindow.setOptions({pixelOffset: new google.maps.Size(0,-30)});
	        infowindow.open(map);
  	     });
    	     map.data.addGeoJson(data.features[i]);
  	 };
         var bounds = new google.maps.LatLngBounds();
         map.data.forEach(function(feature){
             feature.getGeometry().forEachLatLng(function(latlng){
                 bounds.extend(latlng);
              });
          });
          map.fitBounds(bounds);
      }
  });
}
                  
   
            

Related Posts