2016-10-28 55 views
0

我使用谷歌地圖JavaScript API V3到我的Android WebView 現在我得到一個problum,當我使用方向像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> 

我能做些什麼,以除去最後的路線?

回答

1

對於directionsDisplay,您應該使用全局變量var,而不是方向函數內的局部變量。然後在方向函數中,您應該將directionsDisplay設置爲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; 

    ..... 
+0

非常感謝!!!!!它適用於我 –