【问题标题】:Calling Google Maps Api by ajax doesn't work通过 ajax 调用 Google Maps Api 不起作用
【发布时间】:2015-08-07 17:15:36
【问题描述】:

我有一些问题。在这个网站 (http://hyp.altervista.org/home.html) 我想去页面:位置,它应该出现我的谷歌地图,由 ajax 函数调用。

我有由 AJAX 调用的 getLocation.php

 <?php echo '<div id="map-canvas"></div>'; ?>

同时调用的脚本:

window.onload = loadScript;
                $.ajax({
                method: "POST",
                //dataType: "json", //type of data
                crossDomain: true, //localhost purposes
                url: "../getLocation.php", //Relative or absolute path to file.php file
                success: function(response) {


                    $(".contenuti").html(" "+response);

                },
                error: function(request,error) 
                {
                    console.log("Error");
                }
            });

但我看不到地图。我把google的脚本放在家里:

 function initialize() {
        var mapOptions = {
          zoom: 8,
          center: new google.maps.LatLng(-34.397, 150.644)
        };
        var map = new google.maps.Map(document.getElementById('map-canvas'),
            mapOptions);
      }

      function loadScript() {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp' +
            '&signed_in=true&callback=initialize';
        document.body.appendChild(script);
      }

【问题讨论】:

    标签: javascript php jquery ajax google-maps


    【解决方案1】:

    尝试将google maps api的loadScript()移到windows.onload的loadScript()之前,记得添加initialize();在脚本的末尾

    【讨论】:

      【解决方案2】:

      替换地图 div 开启

      <div id='map-canvas' style="display:block;height:600px;"></div>
      

      在脚本写入结束时

      initialize();
      

      【讨论】:

      • 谢谢,但在控制台上显示:“未捕获的 ReferenceError: google is not defined”。
      • 我试着把 window.onload = loadScript();而不是 window.onload = loadScript;但有时有效,有时说:Uncaught TypeError: Cannot read property 'offsetWidth' of null
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 2019-01-15
      • 2011-12-20
      • 2012-10-25
      • 2021-10-31
      • 1970-01-01
      相关资源
      最近更新 更多