最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing the Multiple Polyline stroke color on google map v3 - Stack Overflow

programmeradmin5浏览0评论

I need to create multiple polylines ,each with its own color, and their markers are connected to each other,I am using google map v3.

For example,I have five markers and they are all the connected to each other through red color polylines. Now I want to show this red color polyline in multiple color schemes, first polyline in green, second in Blue, third in Black and so on.

Here is my code

<script type='text/javascript'>
var poly;
var map;
var path;
var i = parseInt(0, 10);
var marker;          

function initialize() {
  var chicago = new google.maps.LatLng(41.879535, -87.624333);
  var myOptions = {
    zoom: 7,
    center: chicago,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
  var polyOptions = {
    strokeColor: '#000000',
    strokeOpacity: 1.0,
    strokeWeight: 3
  }
  poly = new google.maps.Polyline(polyOptions);
  poly.setMap(map);
  // Add a listener for the click event
  google.maps.event.addListener(map, 'click', addLatLng);
}

/**
* Handles click events on a map, and adds a new point to the Polyline.
* @param {MouseEvent} mouseEvent
*/
function addLatLng(event) {
  i++;
  //var path = poly.getPath();
  path = poly.getPath();
  var polyOptions2 = {
    strokeColor: '#FFFFFF',
    strokeOpacity: 1.0,
    strokeWeight: 3
  }
  if (i == 2) {
    poly.setOptions(polyOptions2);

  }
  else {
    polyOptions2.strokeColor = "#FF0000";
    poly.setOptions(polyOptions2);
  }
  path.push(event.latLng);
  // Add a new marker at the new plotted point on the polyline.
  marker = new google.maps.Marker({
    position: event.latLng,
    title: '#' + path.getLength(),
    map: map
  });
}
</script>

I need to create multiple polylines ,each with its own color, and their markers are connected to each other,I am using google map v3.

For example,I have five markers and they are all the connected to each other through red color polylines. Now I want to show this red color polyline in multiple color schemes, first polyline in green, second in Blue, third in Black and so on.

Here is my code

<script type='text/javascript'>
var poly;
var map;
var path;
var i = parseInt(0, 10);
var marker;          

function initialize() {
  var chicago = new google.maps.LatLng(41.879535, -87.624333);
  var myOptions = {
    zoom: 7,
    center: chicago,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
  var polyOptions = {
    strokeColor: '#000000',
    strokeOpacity: 1.0,
    strokeWeight: 3
  }
  poly = new google.maps.Polyline(polyOptions);
  poly.setMap(map);
  // Add a listener for the click event
  google.maps.event.addListener(map, 'click', addLatLng);
}

/**
* Handles click events on a map, and adds a new point to the Polyline.
* @param {MouseEvent} mouseEvent
*/
function addLatLng(event) {
  i++;
  //var path = poly.getPath();
  path = poly.getPath();
  var polyOptions2 = {
    strokeColor: '#FFFFFF',
    strokeOpacity: 1.0,
    strokeWeight: 3
  }
  if (i == 2) {
    poly.setOptions(polyOptions2);

  }
  else {
    polyOptions2.strokeColor = "#FF0000";
    poly.setOptions(polyOptions2);
  }
  path.push(event.latLng);
  // Add a new marker at the new plotted point on the polyline.
  marker = new google.maps.Marker({
    position: event.latLng,
    title: '#' + path.getLength(),
    map: map
  });
}
</script>
Share Improve this question edited Jan 25, 2012 at 11:02 qiao 18.2k7 gold badges59 silver badges46 bronze badges asked Jan 24, 2012 at 10:20 amirhadiamirhadi 511 gold badge1 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Load your marker options and color for the polylines into an json array and loop through them creating the markers and polylines.

Hope this gets you going.

set this portion of your code in for loop like

       var colorVariable = ["red","green","blue","yellow","black"];

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

It will work fine

发布评论

评论列表(0)

  1. 暂无评论