简体   繁体   English

Google Map Ajax加载

[英]Google Map Ajax load

I am having "fun" with google maps and was after some assistance. 我在Google地图上获得了“乐趣”,并且需要一些帮助。 Basically I have a small block of HTML/Javascript which can be loaded into a standard HTML page or into a div using Ajax. 基本上,我有一小段HTML / Javascript,可以使用Ajax将其加载到标准HTML页面或div中。 All I am attempting to do at present is get the map to appear. 我目前正在尝试做的就是让地图出现。 When the page loads as part of the HTML it is fine, appears, sings, dances and occasionally plays the flute. 当页面作为HTML的一部分加载时,可以正常显示,唱歌,跳舞和偶尔吹奏长笛。 However when it loads via Ajax it doesn't appear, but the javascript google writes does. 但是,当它通过Ajax加载时,它不会出现,但是Google编写的JavaScript会出现。

I think I am missing a load or ready trigger. 我想我缺少负载或就绪触发器。 but can't find out what I am missing. 但找不到我想念的东西。 My search-foo is weak. 我的搜索-foo很弱。 This is the code, not massively complicated as you can see. 这是代码,您不会看到那么复杂。 The google map includes have been left out, but there is no errors, or warnings, reported and as I say it works fine as a HTML page. 谷歌地图包括已被排除在外,但没有错误或警告,没有报告,正如我所说的,它可以作为HTML页面正常工作。

<div id="map_canvas_<?= $userContact->getId() ?>" style="margin: 10px auto; width: 90%; height: 300px;"></div>
<script type="text/javascript">

    var mapInstance = null;

    var startLatitude = 53.975044;
    var startLongitude = -2.153320;
    var startZoomLevel = 5;

// create the new instance.
    mapInstance = new GMap2($('map_canvas_<?= $userContact->getId() ?>'));

    // center the map on the UK
    mapInstance.setCenter(new GLatLng(startLatitude, startLongitude), startZoomLevel);

</script>

Khaine. 卡恩。

Have you tried passing the results from your AJAX call to javascript's eval() method? 您是否尝试过将AJAX调用的结果传递给javascript的eval()方法? That will execute whatever javascript code that you are loading in from the other page. 这将执行从另一页面加载的任何javascript代码。

If you are just loading your js using AJAX and adding it to a div, then the code won't automatically get executed. 如果您只是使用AJAX加载js并将其添加到div中,那么代码将不会自动执行。

I think you need to create your GMap2 instance etc. in a window.onload function. 我认为您需要在window.onload函数中创建GMap2实例等。 For example: 例如:

// ..other code stays the same
function initMap() {
    if (GBrowserIsCompatible()) { 
        mapInstance = new GMap2(..);
        mapInstance.setCenter(..);
    }
}
window.onload = initMap;

Hope that fixes your problem. 希望能解决您的问题。

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

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