Javascript 按需异步加载google地图
我在我的jQuery插件中为我的一个页面调用了GoogleMap,但它并没有在tab div中加载整个地图。我猜解决方案是Ajax。我已经试过了,就像谷歌上说的那样,但似乎不起作用 非常感谢Javascript 按需异步加载google地图,javascript,jquery,google-maps-api-3,Javascript,Jquery,Google Maps Api 3,我在我的jQuery插件中为我的一个页面调用了GoogleMap,但它并没有在tab div中加载整个地图。我猜解决方案是Ajax。我已经试过了,就像谷歌上说的那样,但似乎不起作用 非常感谢 function initializeMap(address) { var mapVar = { latitude: "", longitude: "", myLatlng: "" }; var geocoder = new google.maps.Geocoder();
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
我发现这个问题的解决方案是在页面完成加载后,通过注入脚本标记响应window.onload事件/函数调用,按需异步加载映射。因为我的映射显示在jquery选项卡中,所以我必须向选项卡注册事件。因此,当特定选项卡被选中时,请单击。。。在插件中调用document.ready中的映射加载函数,然后初始化保存实际映射请求数据的函数 jQuery插件 在document.ready()中调用插件函数
$("#map_canvas_tab").on("click", function () {
$(this).loadGoogleMap();
window.onload = loadScript;
});
它目前是如何显示的;地图的哪一部分被切断了?我有61.4em宽,它从拉夫脱到右边大约200px;
#map_canvas {
width:61.4em;
height: 100%;
}
$.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
});
}
$("#map_canvas_tab").on("click", function () {
$(this).loadGoogleMap();
window.onload = loadScript;
});