简体   繁体   English

jQuery load()-无法加载谷歌地图

[英]jquery load() - unable to load google maps

I have trouble to load google maps while loading the contents of new page using load() in JQuery without reloading window. 我在使用JQuery中的load()新页面的内容时没有重新加载窗口的情况下加载Google地图时遇到了麻烦。

Jquery ajax call is: jQuery ajax的调用是:

$("form").submit(function(){

        $(".cs-container").load("set-location.php .cs-container");
        return false;

    });

I am trying to load the contents of .cs-container from another set-location.php page in the current div.cs-container class element. 我正在尝试从当前div.cs-container类元素中的另一个set-location.php页面加载.cs-container的内容。 it's loaded successfully but the google maps does't load . 它已成功加载, 但google maps未加载 The g-maps container is placed in .cs-container in the set-location.php page and it's script files are contained in the head of that page. g-maps容器位于set-location.php页面的.cs-container中, 脚本文件位于该页面的开头。

google maps works very well but does't load in the ajax call. google maps工作得很好,但不会在ajax调用中加载。

when i try something like this: 当我尝试这样的事情:

$("form").submit(function(){

            $(".cs-container").load("set-location.php");
            return false;

        });

it works very well but it load the complete page in the .cs-container . 它工作得很好,但是将整个页面加载到.cs-container that I don't want. 我不想要的

the google maps initializing script is as (in the file cs-loc.js ) : 谷歌地图初始化脚本是(在文件cs-loc.js

 /*check if geolocaiton supported*/
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success);
} else {
  error('Geo Location is not supported');
}




function success(position) {


   var lat = position.coords.latitude; /*getting cordinates*/
   var long = position.coords.longitude;

   var input = document.getElementById("latLong");
   input.value = lat + ", " + long;

   var poos = new google.maps.LatLng(lat,long); /*set lat long for maps*/

    var mapProp = {
      center: poos,
      zoom:16,
      mapTypeId:google.maps.MapTypeId.ROADMAP
    };

    var map=new google.maps.Map(document.getElementById("googleMap"),mapProp); /*init maps*/

    /*create marker*/
    var marker=new google.maps.Marker({
      position:poos,
      title: 'Your Store Location',
      draggable: true
      });

    marker.setMap(map); /*set marker to map*/

    google.maps.event.addListener(marker, 'dragend', function(a) {
        console.log(a);
        input.value = a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4);
    });


}


success();

How it can be solve? 怎么解决?

You could use $.getScript() in the load() callback 您可以在load()回调中使用$.getScript()

$(".cs-container").load("set-location.php .cs-container", function(){
    $.getScript('google.maps....').done(function(){           
           $.getScript('cs-loc.js');
   });    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM