按需异步加载谷歌地图
load google map Asynchronously on demand
我在一个jQuery插件中为我的一个页面调用谷歌地图,但它没有在tabdiv中加载整个地图。我猜解决方案是Ajax。我在谷歌上试过,但似乎不起作用。
非常感谢。。。
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%;
}
我发现这个问题的解决方案是在页面加载完成后,通过注入脚本标记来响应window.onload事件/函数调用,按需异步加载映射。因为我的地图显示在jquery选项卡中,所以我已经向选项卡注册了事件。所以当特定选项卡被点击时。。。document.ready中的map Load函数在插件中调用,然后初始化保存实际映射请求数据的函数。
jQuery插件
$.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
});
}
在document.ready()中调用插件函数。。。
$("#map_canvas_tab").on("click", function () {
$(this).loadGoogleMap();
window.onload = loadScript;
});
相关文章:
- 如何在谷歌字体加载时显示加载图像
- 谷歌地图加载后隐藏加载图像
- 如何在不下载的情况下将谷歌字体加载到chrome打包的应用程序中
- 无法让谷歌地图加载到页面上
- 让谷歌地图加载*后*找到用户的位置
- 成功登录谷歌后加载页面
- 谷歌地图加载静态图像
- 谷歌地图加载路线
- 谷歌地图加载需要很长时间
- 谷歌地图加载后发出ajax请求
- 谷歌地图加载部分点击隐藏标签
- 谷歌地图-加载KML覆盖
- 使用javascript从谷歌地图加载json数据
- 如何检测如果谷歌地图加载成功
- 防止谷歌地图加载多次与涡轮链接
- 谷歌地图加载问题
- 谷歌地图加载后会覆盖所有内容
- 谷歌地图加载在不同的拉长从AJAX调用
- 谷歌地图加载图像像
- 谷歌地图加载了Ajax