duanrang9348 2013-07-27 18:56
浏览 47
已采纳

Googlemaps V3和Ajax load()菜单

Well I've been working on this for a while, but i still have no response..

What I am trying to do, is load a googlemap with an ajax no refreshing menu. So here is the main page which loads all of the menu pages, i'm gonna call main.php:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false">
</script>

<ul id="nav">
    <li><a href="page_main">Main</a></li>
    <li><a href="page_weather">Weather</a></li>
    </ul>


    <div id="content"></div>


    <script>
    $(document).ready(function(){
        //initial
        $('#content').load('menu/page_main.php');

        //handle menu clicks
        $('ul#nav li a').click(function() {
            var page = $(this).attr('href');
            $('#content').load('menu/' + page + '.php');
            return false;

        });

    });

    </script> 

And here is the page with the map, let's call page_weather.php:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false">
</script>

<script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(-15.869167, -47.920834),
          zoom: 3,
          disableDefaultUI: true,
          mapTypeId: google.maps.MapTypeId.HYBRID
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
        }



      google.maps.event.addDomListener(window, 'load', initialize);
</script>

<h1>Principal</h1>

</br>
</br>

<body onLoad="initialize()">

<div id="map_canvas" style="width:800; height:400;"></div>

When page_weather.php is loaded directly in the browser, the map loads properly, but when the page is loaded using the ajax no refreshing menu, i only get a 800x400 white box.

How i can fix this?

Already tryed:

Executing <script> inside <div> retrieved by AJAX

Can scripts be inserted with innerHTML?

  • 写回答

2条回答 默认 最新

  • dongzhihong3940 2013-07-27 19:08
    关注

    jQuery will in fact take care of executing <script> tags for you via jQuery.load(). Your problem lies in the fact you are using the window.load event to initialize the google map. This event will never be triggered if you inject this script into the document after the initial window load.

    I'd have a better workaround if google maps didn't insist on a window.load event rather than the dom ready event like most libraries. But since your initialize() function is a global function, you should simply be able to change your load call to this:

    $('#content').load('menu/page_main.php', function () {
        initialize();
    });
    

    By the way, remove the onLoad="initialize()". You already have the window.load event being set by the google.maps.event.addDomListener(window, 'load', initialize); line.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11无法启动 持续蓝屏且系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码