谷歌地图v3:当函数'失败'呼叫
Google maps v3: adding a new map when function 'failure' called
在我的网站的联系页面上,可能会发生两件事:
-
用户已启用地理定位,地图(
map-canvas
)将显示从其位置到预定义位置dest
的路线。此功能运行良好。 -
用户未启用或选择不允许地理定位。将显示警报(工作正常),并将一些文本添加到
directions-panel
(工作良好)。在这种情况下,我想做的第三件事是向以dest
为中心的map-canvas
添加一个新的映射,这个功能似乎不起作用,我不知道为什么。
下面的代码应该能很好地表示上述情况:
<script type="text/javascript">
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var dest = "Unit 20, Tallaght Business Centre, Whitestown Road, Tallaght Business Park, Ireland";//53.282882, -6.383155
var ourLocation = {lat : 53.282882, lng : -6.383155}//centre attribute of the map must be a LatLng object and not hardcoded as above
function initGeolocation() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition( success, failure );
}
}
//create a new map centered on user's location, display route and show directions
function success(position) {
directionsDisplay = new google.maps.DirectionsRenderer();
coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var mapOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
}
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directions-panel"));
calcRoute();
}
//calculate the route from user's location to 'dest'
function calcRoute() {
var start = coords;
var end = dest;
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
//tell user geoloc isn't enabled and just plot 'ourLocation' on 'map-canvas'
function failure() {
alert("Your browser does not have geolocation enabled so we can't give you directions to our office. Enable geolocation and try again, or consult the map for our address.");
$("#directions-error-message").css({
visibility: 'visible'
});
var destMapOptions = {
zoom:13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(ourLocation.lat, ourLocation.lng)
}
map = new google.maps.Map(document.getElementById("map-canvas"), destMapOptions);
}
</script>
有人知道为什么这不起作用吗?所有的帮助都得到了缓解。
EDIT:高于的工作版本
您需要为地图的中心传递LatLng对象,而不是地址。你需要使用谷歌的本地搜索服务来实现这一点。
var dest = {
lat : 53.282882,
lng : -6.383155
}
var destMapOptions = {
zoom:12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(dest.lat,dest.lng)
}
此外,由于您已经将map声明为var,因此可能会为map使用该变量,而不是函数范围的变量。
映射选项的中心属性必须是LatLng对象。您已将其硬编码为地址字符串。
相关文章:
- 当json解析空响应时,Whatwg-Fetch失败,我该如何防止它
- JsFiddle在分叉后描述失败
- 为什么不'当单元测试出现解析错误时,我的因果报应测试会失败
- 当一些承诺失败时,如何继续使用$q.all()
- 失败:等待Protractor与页面同步时出错:“”;在窗口上找不到角度”;
- 在Jquery中单击传播失败
- 网页上失败的javascript会导致所有其他脚本失败
- 带有对象解析的响应javascript ajax失败
- 一台特定计算机的Ajax请求数据未定义/失败
- Nodejs服务器:加载资源失败:服务器的响应状态为404(未找到)
- jQuery.getJSON失败,语法错误
- ng disabled在放入多个表达式时失败
- Jasmine单元测试在监视服务方法时失败
- jQuery分页下一页和上一页按钮在点击零或超过最后一页后失败
- 在iframe的情况下,jQuery html()将失败
- 在我的情况下,使用带有变量失败的 jquery 选择器
- 谷歌地图v3:当函数'失败'呼叫
- 当我的HTTP呼叫因互联网访问而失败时,有没有办法发出错误警报
- 为什么我的ajax呼叫在谷歌浏览器失败
- Ajax呼叫失败,但仅在特定的手机运营商上