K.Z
K.Z

Reputation: 5075

load google map Asynchronously on demand

i am calling google map in one my jQuery plugin for one of my page but its not loading entire map in tab div. I am guessing solution is Ajax. I have tried as says in Google but it doesn't seems working.

many thanks in advance...

function initializeMap(address) {

 var mapVar = {
    latitude: "",
    longitude: "",
    myLatlng: ""
 };

var geocoder = new google.maps.Geocoder();

geocoder.geocode({ 'address': address }, function (results, status) {

    if (status == google.maps.GeocoderStatus.OK) {

        mapVar.latitude = results[0].geometry.location.lat();
        mapVar.longitude = results[0].geometry.location.lng();

        mapVar.myLatlng = new google.maps.LatLng(mapVar.latitude, mapVar.longitude);

        //-----define map options---//
        var mapOptions = {
            center: mapVar.myLatlng,
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        var marker = new google.maps.Marker({
            position: mapVar.myLatlng,
            map: map,
            title: address
        });

    } //end if 
   });
}

html

   <div id="map_canvas" style="width:61.4em; height:400px;"></div>

css

 #map_canvas { 
 width:61.4em;
 height: 100%;
}

Upvotes: 0

Views: 2578

Answers (1)

K.Z
K.Z

Reputation: 5075

i have found solution to this problem is load map Asynchronously on demand after page has finished loading by injecting script tag in response to a window.onload event/ function call. Because my map is showing in jquery Tab so i have register event with tab. so when the specific tab is click ... a map-Load function in document.ready is called in plugin followed by initializing function that holds actual map request data.

jQuery plugin

  $.fn.loadGoogleMap = function () {

    var script_tag = document.createElement('script');

    script_tag.type = 'text/javascript';

    script_tag.src = "https://maps.googleapis.com/maps/api/js?key=yourKey=false&callback=initializeMap"

    document.body.appendChild(script_tag);

}


function initializeMap() {

address = PropertyDetail.d_fullAddress;

var mapVar = {
    latitude: "",
    longitude: "",
    myLatlng: ""
};

var geocoder = new google.maps.Geocoder();

geocoder.geocode({ 'address': address }, function (results, status) {

    if (status == google.maps.GeocoderStatus.OK) {

        mapVar.latitude = results[0].geometry.location.lat();
        mapVar.longitude = results[0].geometry.location.lng();

        mapVar.myLatlng = new google.maps.LatLng(mapVar.latitude, mapVar.longitude);

        //-----define map options---//
        var mapOptions = {
            center: mapVar.myLatlng,
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        var marker = new google.maps.Marker({
            position: mapVar.myLatlng,
            map: map,
            title: address
        });

    } //end if 
  });
 }

in document.ready() call the plugin function...

 $("#map_canvas_tab").on("click", function () {

      $(this).loadGoogleMap();

      window.onload = loadScript;
  });

Upvotes: 1

Related Questions