2015-11-13 83 views
1

我使用相同的代码从http://www.aspsnippets.com/Articles/Google-Maps-V3-Draw-route-line-between-two-geographic-locations-Coordinates-Latitude-and-Longitude-points.aspx在这里,我可以有一点 我试图改变笔触颜色的n个它没有反映在JavaScript改变颜色的谷歌地图V3的多折线行程

<!DOCTYPE html > 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
</head> 
<body> 
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 
     var markers = [ 
      { 
       "title": 'Alibaug', 
       "lat": '18.641400', 
       "lng": '72.872200', 
       "description": 'xxxx' 
      } 
     , 
      { 
       "title": 'Mumbai', 
       "lat": '18.964700', 
       "lng": '72.825800', 
       "description": 'yyyy' 
      } 
     , 
      { 
       "title": 'Pune', 
       "lat": '18.523600', 
       "lng": '73.847800', 
       "description": 'zzz' 
      } 
]; 
     window.onload = function() { 
      var mapOptions = { 
       center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
       zoom: 10, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      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(); 
      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 
       }); 
       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****************// 

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

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

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



      //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.setPath(path); 
        service.route({ 
         origin: src, 
         destination: des, 
         travelMode: google.maps.DirectionsTravelMode.DRIVING 
        }, function (result, status) { 
         if (status == google.maps.DirectionsStatus.OK) { 
          for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) { 
           path.push(result.routes[0].overview_path[i]); 
          } 
         } 
        }); 
       } 
      } 
     } 
    </script> 
    <div id="dvMap" style="width: 500px; height: 500px"> 
    </div> 
</body> 
</html> 

并替换该行与

var poly = new google.maps.Polyline({ map: map, strokeColor: 'red' }); 

and i tried to add color using the below code as 

var colorVariable = ["white","green","blue","yellow","rose"]; 



    for(var a =0;a<=5;a++){ 
      var polyOptions = { 
       strokeColor: colorVariable[a], 
       strokeOpacity: 1.0, 
       strokeWeight: 2 
      } 
      poly = new google.maps.Polyline({ map: map, strokeColor: polyOptions }); 
      poly.setMap(map); 
     } 

,但我没有得到不同颜色的笔,请说如何整合它什么是错在它

+0

你是如何改变线的颜色? –

+0

使用代码的第二部分 –

+0

我知道这一点,但是你想通过点击一个按钮来改变所有路线吗? –

回答

2

我用一招找出哪个请求正在处理白衣你的代码则strokeColor,通过看,如果(result.request.origin == lat_lng [1])。 (可能存在更优雅的解决方案)

我添加了第4个点(只看到1个颜色,看它是否有效)。

这是你想要的吗?

<!DOCTYPE html > 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Changing color for the Multiple Polyline stroke on Google maps</title> 
</head> 
<body> 
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 
     var polylines = []; // let's make 1 polyline per color 

     var markers = [ 
      { 
       "title": 'Alibaug', 
       "lat": 18.641400, 
       "lng": 72.872200, 
       "description": 'xxxx' 
      }, 
      { 
       "title": 'Mumbai', 
       "lat": 18.964700, 
       "lng": 72.825800, 
       "description": 'yyyy' 
      }, 
      { 
       "title": 'Pune', 
       "lat": 18.523600, 
       "lng": 73.847800, 
       "description": 'zzz' 
      }, 
      { 
       "title": 'Kolhapur', 
       "lat": 16.696530010128207, 
       "lng": 74.23864138126372, 
       "description": 'Extra point' 
      } 
     ]; 
     window.onload = function() { 
      var mapOptions = { 
       center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
       zoom: 10, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      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(); 

      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 
       }); 
       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****************// 
      // Make the polyline 
      //Loop and Draw Path Route between the Points on MAP 
      for (var i = 0; i< lat_lng.length; i++) { 
       generatePath(i); // I put this all in a function 
      } 

      // we want to know which request is being processed 
      function indexOfRequest(result) { 
       for (var i = 0; i <= lat_lng.length; i++) { 
       if (result.request.origin == lat_lng[i]) { 
        return i; 
       } 
       } 
      } 
      function generatePath(i) { 
       // colors 
       var colorVariable = ["white", "green", "blue", "yellow", "rose"]; 
       //Intialize the Direction Service 
       var service = new google.maps.DirectionsService(); 
       //Intialize the Path Array 
       var path = new google.maps.MVCArray(); 
       var color = colorVariable[i % colorVariable.length]; // the % makes sure the array cycles, so after rose, it's white again 
       var poly = new google.maps.Polyline({ map: map, strokeColor: color }); 
       polylines.push(poly); 
       var src = lat_lng[i]; 
       var des = lat_lng[i + 1]; 
       path.push(src); 
       poly.setPath(path); 
       // route service 
       service.route({ 
        origin: src, 
        destination: des, 
        travelMode: google.maps.DirectionsTravelMode.DRIVING 
       }, function (result, status) { 
        if (status == google.maps.DirectionsStatus.OK) { 
         var routeIndex = indexOfRequest(result); 
         polylines[routeIndex].setPath(result.routes[0].overview_path); 
        } 
       }); 

      } 
     } 
    </script> 
    <div id="dvMap" style="width: 500px; height: 500px"> 
    </div> 
</body> 
</html> 
+0

非常感谢你,我试图解决在控制台中生成的错误,当我运行代码时,你可以看看错误是InvalidValueError:属性目标:不是字符串;而不是LatLng或LatLngLiteral:不是对象;而不是一个对象 ... essage} return new vb(a + c)}; _。xb = function(a){if(!(a instanceof vb))throw a; window ... js?传感器=假(线34) 类型错误:a为null \t ... 1,R:1}; F [106] = {类型: “B”,标记:1,R:1}}! f = _。zg.j(aN,$ W); _。Nk(window.document,d,_... –

0

只能更改使用的SetOption,这样的笔触颜色:

poly.setOptions({strokeColor: 'blue'}); 

你polyOption不是则strokeColor属性的合法值。
可以assing使用obj.setOptions({attribute: value})模式

2

单个折线只能有一种颜色,你需要一个单独的折线为每种颜色:

function getDirections(src, des, color, map) { 
    //Intialize the Direction Service 
    var service = new google.maps.DirectionsService(); 
    service.route({ 
     origin: src, 
     destination: des, 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }, function (result, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 
      //Intialize the Path Array 
      var path = []; 
      for (var i = 0; i < result.routes[0].overview_path.length; i++) { 
       path.push(result.routes[0].overview_path[i]); 
      } 
      //Set the Path Stroke Color 
      var polyOptions = { 
       strokeColor: color, 
       strokeOpacity: 1.0, 
       strokeWeight: 2, 
       path: path, 
       map: map 
      } 
      poly = new google.maps.Polyline(polyOptions); 
      poly.setMap(map); 

     } 
    }); 
} 

proof of concept fiddle

代码片段:

var markers = [{ 
 
    "title": 'Alibaug', 
 
    "lat": '18.641400', 
 
    "lng": '72.872200', 
 
    "description": 'xxxx' 
 
}, { 
 
    "title": 'Mumbai', 
 
    "lat": '18.964700', 
 
    "lng": '72.825800', 
 
    "description": 'yyyy' 
 
}, { 
 
    "title": 'Pune', 
 
    "lat": '18.523600', 
 
    "lng": '73.847800', 
 
    "description": 'zzz' 
 
}]; 
 
// white is hard to see on the map tiles, removed. 
 
var colorVariable = ["green", "blue", "yellow", "rose"]; 
 
window.onload = function() { 
 
    var mapOptions = { 
 
    center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
 
    zoom: 10, 
 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }; 
 
    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(); 
 
    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 
 
    }); 
 
    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****************// 
 

 

 

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

 
    //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]; 
 
     getDirections(src, des, colorVariable[i], map); 
 
    } 
 
    } 
 
} 
 

 
function getDirections(src, des, color, map) { 
 
    //Intialize the Direction Service 
 
    var service = new google.maps.DirectionsService(); 
 
    service.route({ 
 
    origin: src, 
 
    destination: des, 
 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
 
    }, function(result, status) { 
 
    if (status == google.maps.DirectionsStatus.OK) { 
 
     //Intialize the Path Array 
 
     var path = []; 
 
     for (var i = 0; i < result.routes[0].overview_path.length; i++) { 
 
     path.push(result.routes[0].overview_path[i]); 
 
     } 
 
     //Set the Path Stroke Color 
 
     var polyOptions = { 
 
     strokeColor: color, 
 
     strokeOpacity: 1.0, 
 
     strokeWeight: 2, 
 
     path: path, 
 
     map: map 
 
     } 
 
     poly = new google.maps.Polyline(polyOptions); 
 
     poly.setMap(map); 
 

 
    } 
 
    }); 
 
}
html, 
 
body, 
 
#dvMap { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="dvMap"></div>

+0

谢谢,我正在获得循环,即使ploy为直线请运行代码编辑情节,如果它是一个直我需要没有必要的循环请道歉我工作,但我无法想出它的任何帮助将节省我 –

+0

我固定travelMode到TRANSIT,解决了我的问题,谢谢 –

+0

您正在获得的循环编辑中的要点是因为点在单向道路上,并且在该道路上的行驶方向错误顺序。如果我[反转它们,环路消失](http://jsfiddle.net/ geocodezip/kcnL9cqg/2 /) – geocodezip