自定义谷歌地图不能在手机和平板电脑上显示
Custom Google Maps not showing on mobile and tablets
我遇到了一些奇怪的事情,我的自定义谷歌地图在手机或平板电脑上不可见。但在台式机上运行得很好。我不能明确地指出这个问题。
我的html如下:
<div class="wrapper">
<div id="maps-canvas"></div>
<div class="clearfix"></div>
</div>
和我的自定义JS:
$(window).bind("load", function () {
if ($('#maps-canvas').length) {
CustomGoogleMaps();
}
});
function CustomGoogleMaps() {
// Creating a LatLng object containing the coordinate for the center of the map
var latlng = new google.maps.LatLng(52.145022, 5.422421);
var map = new google.maps.Map(document.getElementById('maps-canvas'), {
//options
zoom: 18, // This number can be set to define the initial zoom level of the map
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP, // This value can be set to define the map type ROADMAP/SATELLITE/HYBRID/TERRAIN
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.LEFT_BOTTOM
},
scaleControl: true,
streetViewControl: true,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_BOTTOM
}
});
// Detect the resize event and keep marker in center when on resize
google.maps.event.addDomListener(window, "resize", function () {
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
});
// Define Marker properties
var imagePath = "http://google-maps-icons.googlecode.com/files/sailboat-tourism.png";
var image = new google.maps.MarkerImage(imagePath,
// This marker is 75 pixels wide by 101 pixels tall.
new google.maps.Size(75, 101),
// The origin for this image is 0,0.
new google.maps.Point(0, 0),
// The anchor for this image is the base of the flagpole at 18,101.
new google.maps.Point(18, 101)
);
// Add Marker
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(52.145022, 5.422421),
map: map,
icon: image, // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
animation: google.maps.Animation.DROP, // Animate drop effect of the marker
position: latlng // The position should be the latlng coordinates
});
// Add listener for a click on the pin
google.maps.event.addListener(marker1, 'click', function () {
infowindow1.open(map, marker1);
});
// Add information window
//change address details here
var contentString = '<div class="map-info-box">'
+ '<div class="map-head">'
+ '<h3>Company Title</h3></div>'
+ '<p class="map-address">My Address</p>';
var infowindow1 = new google.maps.InfoWindow({
content: contentString,
});
// Create information window
function createInfo(title, content) {
return '<div class="maps-info-window"><strong>' + title + '</strong><br />' + content + '</div>';
}
}
请参阅我的工作日志
希望有人能在正确的道路上帮助我。
这里有一个更简单的替代方案:
$(window).bind("load", function () {
// Remove this next block of code, or comment out, because I think it is calling your function before it is available
//if ($('#maps-canvas').length) {
//CustomGoogleMaps();
//}
});
相反,无论您在哪里排队您的脚本,添加该函数作为回调:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?callback=CustomGoogleMaps"></script>
如果这有效,我们至少知道这是一个时间问题。
您如何排队您的谷歌地图API脚本?这可能是时间问题。它可能在桌面上加载得更快,因此当代码试图调用:
时,它总是可用的。var latlng = new google.maps.LatLng(52.145022, 5.422421);
var map = new google.maps.Map(document.getElementById('maps-canvas') .....
这是可能的,库是缓慢的移动加载,因此脚本还没有准备好,当你试图调用谷歌。map函数,这会导致一个错误,从而阻止您的map加载。
有一些方法可以确保在您尝试调用库的函数之前加载库。下面是我最近使用的一个方法:CustomGoogleMaps = (function(){
var googleMapsLoaded = $.Deferred();
var init = function(){
var self = this;
googleMapsLoaded.done( function(){
self.initializeMap();
});
}
var initializeMap = function(){
// initialize your map here
var map = new google.maps.Map("yourMapContainer") // etc....
}
// rest of your google maps stuff here
return {
googleMapsLoaded : googleMapsLoaded,
init : init,
initializeMap : initializeMap
}
})();
然后,在你的脚本排队的地方,你可以传递一个回调作为一个参数,这个函数将在google maps库可用后运行,像这样:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?callback=CustomGoogleMaps.googleMapsLoaded.resolve"></script>
当你试图初始化你的map时:
CustomGoogleMaps.Init()
下面是它的工作原理:
你的JS运行CustomGoogleMaps.Init()函数。如果googlemaploaded承诺已经被解析(意味着库已经准备好了),你的initializeMap()函数将继续并立即运行。如果不是,它将等待该承诺得到解决。如果它正在等待解析,一旦google maps脚本加载,它将运行
CustomGoogleMaps.googleMapsLoaded().resolve()
将解析promise,因此initializeMap()函数(当前正在等待promise解析)将运行,您可以开始您的map初始化。
编辑:我的建议需要对你的代码进行一些相对重要的结构更改。一个更快、更简单的解决方案是将CustomGoogleMaps函数作为回调传递,如下所示:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?callback=CustomGoogleMaps"></script>
相关文章:
- 隐藏移动设备/平板电脑中文本字段上的键盘
- plupload智能手机和平板电脑浏览按钮事件未启动
- iOS iPad平板电脑渲染视口不正确
- 当我在平板电脑中关闭应用程序时,如何使其保持会话有效
- 媒体查询:如何仅定位平板电脑
- Amcharts在平板电脑/ipad中没有分散
- 如何让安卓佩戴在手机和平板电脑的应用程序中振动
- 创建一个工具来打开android平板电脑上的本地pdf文件
- 平板电脑:在方向改变页面跳转开始
- jQuery DateBox在平板电脑上加载缓慢
- 在手机和平板电脑浏览器上播放音频片段
- 如何检测用户是否正在使用windows平板电脑
- 用javascript获取页面的当前URL,这在平板电脑上有效
- 图片's的高度与手机/平板电脑的高度相同-只能在Chrome中正常工作
- 如何使用jquery区分桌面浏览器尺寸1024px和平板电脑尺寸1024px
- 在Surface Pro平板电脑上启用jQuery UI拖放
- mousedown事件未在平板电脑/移动设备上启动,HTML5 Canvas
- 平板电脑友好的悬停工具提示,显示基于javascript函数结果的动态文本
- 自定义谷歌地图不能在手机和平板电脑上显示
- 如何显示html内容仅在平板电脑上,而不是在手机或桌面