gpt4 book ai didi

javascript - 当下一条路线出现时,谷歌地图方向路线不会消失

转载 作者:行者123 更新时间:2023-12-03 05:47:00 24 4
gpt4 key购买 nike

我在 Android WebView 中使用 Google Maps Javascript API V3现在我遇到一个问题,当我使用 A 到 B 之类的方向时,它可以工作,但是当我将 A 更改为 C 时,A 到 B 的路线不会消失,因此 A 到 B 的路线与 A 到 C 的路线重叠。

A到B A to B

从A到C A to C

直接执行按钮

direction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setVisibility(View.INVISIBLE);
infowindow.setVisibility(View.INVISIBLE);
String centerUrl = "javascript:direction(" + Lat + "," + Lng + "," + LatNew + "," + LngNew + ")";
webView.loadUrl(centerUrl);
}
});

HTML

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple markers</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var myLatLng,endLatLng;
var map;
var marker,beachMarker,eatMarker,drinkMarker;
var location;

function initMap() {

map = new google.maps.Map(document.getElementById('map'), {
zoom: 17,
center: myLatLng
});

marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
beachMarker = new google.maps.Marker({
position: {lat: 25.078838,lng: 121.574204},
map: map,
icon: 'file:///android_asset/Marker.png'
});
beachMarker.addListener('click',function(){
Android.responseResult("beach");
});

eatMarker = new google.maps.Marker({
position: {lat: 25.080643,lng: 121.569551},
map: map,
icon: 'file:///android_asset/Marker.png'
});
eatMarker.addListener('click',function(){
Android.responseResult("eat");
});

drinkMarker = new google.maps.Marker({
position: {lat: 25.079734,lng: 121.569519},
map: map,
icon: 'file:///android_asset/Marker.png'
});
drinkMarker.addListener('click',function(){
Android.responseResult("drink");
});

}


function centerAt(latitude,longitude){
myLatLng = new google.maps.LatLng(latitude,longitude);
marker.setMap(null);
map.panTo(myLatLng);

marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: 'file:///android_asset/location.png'
});

}
function direction(latitude,longitude,latitudeNew,longitudeNew){
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
myLatLng = new google.maps.LatLng(latitude,longitude);
endLatLng = new google.maps.LatLng(latitudeNew,longitudeNew);
directionsService.route({
origin: myLatLng,
destination: endLatLng,
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);
}






</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBMqm4dbVtq1OiUpyDxzRcU5196guci_jg&callback=initMap"></script>

如何删除最后一条路线?

最佳答案

您应该为 directionsDisplay 使用全局变量,而不是方向函数内的局部变量。然后在方向函数中,您应该将 DirectionDisplay 设置为 null 并将其分配给新的。

<script>
var myLatLng,endLatLng;
var map;
var marker,beachMarker,eatMarker,drinkMarker;
var location;
var directionsDisplay;

function direction(latitude,longitude,latitudeNew,longitudeNew) {
var directionsService = new google.maps.DirectionsService;
if (directionsDisplay != null) {
directionsDisplay.setMap(null);
}
directionsDisplay = new google.maps.DirectionsRenderer;

.....

关于javascript - 当下一条路线出现时,谷歌地图方向路线不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40302288/

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