在谷歌方向api给路径的颜色

时间:2014-04-25 11:32:34

标签: javascript google-maps google-maps-api-3 directions

https://developers.google.com/maps/documentation/javascript/directions

这是我的代码显示路径

function calcRoute() {
  var request = {
          origin: "Chicago, IL",
          destination: "Los Angeles, CA",
          waypoints: [
            {
              location:"Joplin, MO",
              stopover:false
            },{
              location:"Oklahoma City, OK",
              stopover:true
            }],
          provideRouteAlternatives: false,
          travelMode: google.maps.TravelMode.DRIVING,
          unitSystem: google.maps.UnitSystem.IMPERIAL
        };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

如何更改默认为蓝色的路径颜色?

2 个答案:

答案 0 :(得分:1)

您可以在API Reference

中找到

DirectionsRendererOptions对象采用polylineOptions属性,您可以在其中定义其样式。

例如:

var directionsOptions = {
    polylineOptions: {
        strokeColor: 'red'
    }
}

directionsDisplay = new google.maps.DirectionsRenderer(directionsOptions);

希望这有帮助!

答案 1 :(得分:0)

向地图添加折线时,只需使用PolylineOptions对象,就可以调用width()color()方法:

    private GoogleMap map;

    //get the map reference from the layout

    //Set the polyline configurations
    PolylineOptions rectLine = new PolylineOptions().width(8).color(Color.RED);

    //add the points to the polyline
    rectLine.add(new LatLng(40.3, 18.6));
    rectLine.add(new LatLng(40.3, 20));

    //add the polyline to the map
    map.addPolyline(rectLine);