【发布时间】:2016-09-25 14:27:32
【问题描述】:
我开发了一个 Web 应用程序,在我的 Web 应用程序中使用 Goole Maps。
HTML:
<div id="showMap">
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map"></div>
</div>
<div id="errorMap">
<h1>No Map available due to Internet connectivity problems.</h1>
</div>
JS:
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 31.601043, lng: 74.169527},
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function () {
searchBox.setBounds(map.getBounds());
});
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function (marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
loadMarkersDynamically(); // load markers function
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDwEvCr3alGTKBFt23yk4WVq5jg34I7CJs&libraries=places&callback=initMap" async defer></script>
一切正常。但是当没有互联网连接时我遇到一个问题,然后出现错误并且地图不显示。我希望如果互联网不可用,那么我想隐藏showMap div 并显示errorMap div。
我尝试了很多解决方案:来自 Stack Overflow 的This、This 和 This,但我无法解决我的问题。
也许我没有在上述答案中提供的正确位置调用函数,请告诉我如何修复它。
【问题讨论】:
-
是用于 phonegap 中的应用程序还是内部 Web 应用程序?我不明白人们应该如何在没有互联网连接的情况下访问您的网站...
-
基本上它是一个房地产应用程序,我将使用 WAR 文件交付它,它不是网站,地图是可选的,如果互联网不可用,我必须隐藏地图 div 并显示错误消息...它每次都在客户端的本地服务器上运行。
-
请告诉我如何解决它
-
专家提问技巧:不要试图通过提及截止日期来催促读者;不要明确要求读者为您修复代码;并且不要特别要求人们应该(或不应该)以某种方式对问题进行投票。
-
好的明白你的意思,你能帮帮我吗?因为我的问题还没有解决
标签: javascript jquery html google-maps google-maps-api-3