谷歌地图方向出现两次
Google Maps Directions Appearing Twice?
我正在尝试创建一个页面,加载后将显示用户在谷歌地图上的当前位置。还将有各种可能目的地的选择列表。当选择列表更改时,地图将显示从当前位置到目的地的路线。到目前为止一切都很好!
哪里出了问题是当我试图添加驾驶方向。出于某种原因,它们出现了两次,但我不明白为什么。这是代码:
var latitude = 0;
var longitude = 0;
$( document ).ready(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(initMap);
} else {
document.getElementById("feedback").innerHTML = "Geolocation is not supported by this browser.";
}
});
function initMap(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
latlong = latitude + "," + longitude;
var myCentre = new google.maps.LatLng(latitude,longitude);
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 14,
center: {lat: latitude, lng: longitude},
mapTypeId:google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: myCentre,
title: 'Your current position'
});
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('dirs'));
marker.setMap(map);
var control = document.getElementById('floating-panel');
control.style.display = 'block';
map.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
var onChangeHandler = function() {
calculateAndDisplayRoute(directionsService, directionsDisplay, myCentre, marker);
};
document.getElementById('end').addEventListener('change', onChangeHandler);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, myStart, marker) {
marker.setMap(null);
directionsService.route({
origin: myStart,
destination: document.getElementById('end').value,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
我猜这与线路有关:
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('dirs'));
但我真的看不出是什么。谷歌网站上的例子(https://developers.google.com/maps/documentation/javascript/examples/directions-panel)有两条非常相似的线,但没有给出相同的问题。很明显,我不必要地多次打电话给某个东西,但怎么了?
感谢
onChange处理程序被激发两次,因为它在initMap函数内部。当移动到initMap函数之外(以及所有参数的变量)时,它按预期工作。所以,现在的代码是:
var latitude = 0;
var longitude = 0;
$( document ).ready(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(initMap);
} else {
document.getElementById("feedback").innerHTML = "Geolocation is not supported by this browser.";
}
});
var directionService = "";
var DirectionsRenderer = "";
var myCentre = "";
var marker = "";
function initMap(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
latlong = latitude + "," + longitude;
myCentre = new google.maps.LatLng(latitude,longitude);
directionsService = new google.maps.DirectionsService;
directionsDisplay = new google.maps.DirectionsRenderer;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 14,
center: {lat: latitude, lng: longitude},
mapTypeId:google.maps.MapTypeId.ROADMAP
});
marker = new google.maps.Marker({
position: myCentre,
title: 'Your current position'
});
directionsDisplay.setMap(map);
var panel = document.getElementById('dirs');
directionsDisplay.setPanel(panel);
marker.setMap(map);
var control = document.getElementById('floating-panel');
control.style.display = 'block';
map.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
}
var onChangeHandler = function() {
var end = document.getElementById('end').value;
calculateAndDisplayRoute(directionsService, directionsDisplay, myCentre, marker, end);
};
document.getElementById('end').addEventListener('change', onChangeHandler);
function calculateAndDisplayRoute(directionsService, directionsDisplay, myStart, marker, end) {
marker.setMap(null);
directionsService.route({
origin: myStart,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
感觉还是有点像一个拼凑的东西,但我应该能把它整理一下。
相关文章:
- Meteor Router数据函数被调用两次
- 从MySQL数据库中获取输入数据需要两次页面刷新
- Module.start()已激发两次
- Jquery点击事件必须点击两次
- Append元素在运行两次函数后不显示
- 防止双击执行两次jQuery post请求
- 我必须点击两次才能激活任何按钮操作(离子/角度)
- 单击jQuery会激发两次
- 如何避免在树上走两次
- button.单击两次删除附加操作后不工作
- 由于$compile,Javascript(Angular)嵌套指令加载了两次
- 单击元素两次后执行操作
- AngularJs正在阻止链接被点击两次
- 为什么我的路线处理程序会触发两次
- 为什么Highchart在我的rails应用程序中加载了两次?(未捕获的Highcharts错误#16)
- 在列表中至少使用两次随机生成的nr
- 检查某个键是否按下两次
- 一次点击,两次'单击'事件已启动
- 我的所有代码在使用Webpack编译时都会运行两次
- 谷歌地图方向出现两次