2016-08-15 7 views
1

Polylineについては、マップ上に描画されているという奇妙なシナリオがあります。今余計な余分なポリラインが描画されました - Google Maps API v3

enter image description here

:私はコードを投稿する前に、私が最初に私が(:62.734キロを両方が同じ= 総距離disctance結果として計算)法線方向サービスを利用するときに何が起こるかを説明します私は方向を描くとき、​​私自身が - この直線は、どこからともなく表示されます:

enter image description here

コードスニペット:

<script type="text/javascript"> 
    var markers = [ 
      { 
       "lat": '-26.2036247253418', 
       "lng": '28.0086193084717' 
      } 
     , 
      { 
       "lat": '-26.1259479522705', 
       "lng": '27.9742794036865' 
      } 
     , 
      { 
       "lat": '-25.8434619903564', 
       "lng": '28.2100086212158' 
      } 
    ]; 
    window.onload = function() { 
     var mapOptions = { 
      center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
      zoom: 8, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
     var labelIndex = 0; 
     var totalDistance = 0; 

     var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions); 
     var infoWindow = new google.maps.InfoWindow(); 
     var lat_lng = new Array(); 
     var latlngbounds = new google.maps.LatLngBounds(); 
     //var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'; 
     for (i = 0; i < markers.length; i++) { 
      var data = markers[i] 
      var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
      lat_lng.push(myLatlng); 
      var marker = new google.maps.Marker({ 
       position: myLatlng, 
       map: map, 
       title: data.title, 
       label: labels[labelIndex++ % labels.length], 
       //icon: image 
      }); 
      latlngbounds.extend(marker.position); 
      (function (marker, data) { 
       // google.maps.event.addListener(marker, "click", function (e) { 
       //  infoWindow.setContent(data.description); 
       //  infoWindow.open(map, marker); 
       // }); 
      })(marker, data); 
     } 
     map.setCenter(latlngbounds.getCenter()); 
     map.fitBounds(latlngbounds); 

     //***********ROUTING****************// 

     //Initialize the Path Array 
     var path = new google.maps.MVCArray(); 

     //Initialize the Direction Service 
     var service = new google.maps.DirectionsService(); 

     var directionsDisplay = new google.maps.DirectionsRenderer({ 
      setMap: map 
     }); 

     //Set the Path Stroke Color 
     var poly = new google.maps.Polyline({ map: map, strokeColor: '#4986E7' }); 

     //Loop and Draw Path Route between the Points on MAP 
     for (var i = 0; i < lat_lng.length; i++) { 
      if ((i + 1) < lat_lng.length) { 
       var src = lat_lng[i]; 
       var des = lat_lng[i + 1]; 
       path.push(src); 
       //poly.strokeColor = '#'+Math.floor(Math.random()*16777215).toString(16); 
       poly.setPath(path); 
       service.route({ 
        origin: src, 
        destination: des, 
        travelMode: google.maps.DirectionsTravelMode.DRIVING 
       }, function (result, status) { 
        if (status == google.maps.DirectionsStatus.OK) { 
         directionsDisplay.setDirections(result); 
         var myroute = directionsDisplay.directions.routes[0]; 
         var distance = 0; 

         for (i = 0; i < myroute.legs.length; i++) { 
          distance += myroute.legs[i].distance.value; 
          //for each 'leg'(route between two waypoints) we get the distance and add it to the total 
         } 

         for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {        
          path.push(result.routes[0].overview_path[i]); 
          //console.log(result.routes[0].legs[0].distance); 
         }       
         totalDistance += distance; 
         document.getElementById('total').innerHTML = (totalDistance/1000) + ' km'; 
        }      
       }); 
      } 
     }   
    } 
</script> 
<div id="dvMap"></div> 
<div><p>Total Distance: <span id="total"></span></p></div> 
+0

2間のルートを描く[矛盾した行動の可能性の重複だけで次の行を削除Google Maps v3のポイント](http://stackoverflow.com/questions/23176212/inconsistent-behaviour-drawing-a-route-between-two-points-in-google-maps-v3) – geocodezip

+0

可能[javascriptを使用してGoogleマップパスの直線を削除する]の複製(http://stackoverflow.com/questions/32836341/remove-straight-line-in-google-map-path-using-javascript) – geocodezip

+0

@geocodezipあなたは過去に回答していたので、質問や何かを守っています。-1もう一度やり直してください...良い仕事を続けてください。 –

答えて

0

最初のポリと最後のポリの間に線を描いていませんか? poly0とpoly1、poly1とpoly2などの間に線を描きますが、poly100とpoly0は結び付けないでください(ポリ100が最後の場合)

これは、点Bから形を完成する直線を説明しています。形を完成させたくないので、作図をやめてください。形状を完成させないように設定できる機能はありませんか?

私は、非常に高価な仕事を知っているだけで、それは同じルートに沿ってBからAの逆の順番に戻ることです。しかし、それはおそらくあなたが探しているものではありません

+0

レスポンスには多くの感謝をします。私はそのような方法が存在するかどうか分かりません。たとえポリラインの代わりに線を描くことができたとしても。私は形状の面を完全に忘れていたoO –

+1

http://www.cadtutor.net/tutorials/autocad/drawing_objects/draw-05.gif これは閉じた開いたポリラインの例です。どこかに開いたポリライン、私はそれらに精通していないが、多分あなたはそれを把握するだろう! – AgentM

+0

うん、それは私が推測するものだ!ありがとう! –

関連する問題