gpt4 book ai didi

javascript - 删除具有相同链接的折线

转载 作者:行者123 更新时间:2023-12-03 06:53:28 25 4
gpt4 key购买 nike

我在使用 Polylines google map api v3 时遇到问题。

当我单击链接时,我会绘制多段线,因为我使用同一链接删除或隐藏多段线?

这是我的代码:HTML 和 JavaScript

function mostrarRuta(ltOrigen, lgOrigen, ltDestino, lgDestino) {

var start = new google.maps.LatLng(ltOrigen, lgOrigen);
var end = new google.maps.LatLng(ltDestino, lgDestino);

var directionsDisplay = new google.maps.DirectionsRenderer();// also, constructor can get "DirectionsRendererOptions" object
directionsDisplay.setMap(map); // map should be already initialized.

var request = {
origin : start,
destination : end,
travelMode : google.maps.TravelMode.DRIVING
};
var directionsService = new google.maps.DirectionsService();
//var bounds = new google.maps.LatLngBounds();

directionsService.route(request, function(response, status) {
/*if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}*/
if (status == google.maps.DirectionsStatus.OK) {
var path = new google.maps.MVCArray();
var poly = new google.maps.Polyline({
map: map,
strokeColor: '#F3443C'
});
for (var i = 0, len = response.routes[0].overview_path.length; i < len; i++) {
path.push(response.routes[0].overview_path[i]);
}
poly.setPath(path);
//map.fitBounds(bounds);
}
});
}
<a href="mostrarRuta(ltOrigen, lgOrigen, ltDestino, lgDestino);">MI ruta</a>

最佳答案

您需要在全局范围内保留对折线的引用。如果它已经创建,请将其从 map 中删除(如果您愿意,可以通过在未显示链接时单击链接时将其重新添加到 map 来切换它。

proof of concept fiddle

代码片段:

var geocoder;
var map;
var poly;
var ltOrigen = 40.7127837,
lgOrigen = -74.0059413,
ltDestino = 40.735657,
lgDestino = -74.1723667;

function initialize() {
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});;
}

function mostrarRuta(ltOrigen, lgOrigen, ltDestino, lgDestino) {
if (poly && poly.setMap && poly.getMap && (poly.getMap() != null)) {
poly.setMap(null);
return;
}
var start = new google.maps.LatLng(ltOrigen, lgOrigen);
var end = new google.maps.LatLng(ltDestino, lgDestino);

var directionsDisplay = new google.maps.DirectionsRenderer(); // also, constructor can get "DirectionsRendererOptions" object
directionsDisplay.setMap(map); // map should be already initialized.

var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
var directionsService = new google.maps.DirectionsService();
//var bounds = new google.maps.LatLngBounds();

directionsService.route(request, function(response, status) {
/*if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}*/
if (status == google.maps.DirectionsStatus.OK) {
var path = new google.maps.MVCArray();
poly = new google.maps.Polyline({
map: map,
strokeColor: '#F3443C'
});
var bounds = new google.maps.LatLngBounds();
for (var i = 0, len = response.routes[0].overview_path.length; i < len; i++) {
path.push(response.routes[0].overview_path[i]);
bounds.extend(response.routes[0].overview_path[i]);
}
poly.setPath(path);
map.fitBounds(bounds);
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<a href="javascript:mostrarRuta(ltOrigen, lgOrigen, ltDestino, lgDestino);">MI ruta</a>
<div id="map_canvas"></div>

关于javascript - 删除具有相同链接的折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37395379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com