Google maps: Change colour of polyline when clicked, but change colour back to original colour when second polyline is clicked









up vote
1
down vote

favorite












I have a site which has numerous hiking paths displayed as polylines in Google maps. All polylines have Strokecolor green as default. I would like it that when a polyline is clicked, the polyline colour turns red. It should remain red until a second (different) polyline is clicked. The first polyline should then turn back to green and the second polyline turns red.



I have searched and search and tried numerous solutions, but none work.



The "test" code I have submitted is "clean" without any of my failed attempts.



 <!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
#map
height: 100%;

html, body
height: 100%;
margin: 0;
padding: 0;

</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
var flightPath;
var pathSymbol;
function initMap()
pathSymbol =
path: google.maps.SymbolPath.CIRCLE,
strokeColor: '#FF0000',
strokeOpacity: 1,
strokeWeight: 4,
fillColor: '#FF0000',
fillOpacity: 1,
scale: 3
;

map = new google.maps.Map(document.getElementById('map'),
center: lat: 45, lng: -87,
zoom: 6
);

var flightPlanCoordinates = [
lat: 42.5, lng: -86.5 ,
lat: 42.5, lng: -87.5,
lat: 43.5, lng: -88.5 ,
lat: 44.5, lng: -88.5 ,
lat: 46.5, lng: -89.5 ,
lat: 49.5, lng: -89.5 ,
];
var flightPlanCoordinates2 = [
lat: 42, lng: -86 ,
lat: 42, lng: -87,
lat: 42, lng: -88 ,
lat: 43, lng: -88 ,
lat: 44, lng: -89 ,
lat: 49, lng: -89 ,
];
var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

for (let i = 0; i < 2; i++)
addPath(
path: arrayOfFlightPlans[i],
strokeColor: '#8CB65F',
strokeOpacity: 1.0,
strokeWeight: 4,
);



function addPath(props)
flightPath = new google.maps.Polyline(
path: props.path,
strokeColor: props.strokeColor,
strokeOpacity: props.strokeOpacity,
strokeWeight: props.strokeWeight
);
flightPath.setMap(map);

flightPath.addListener('mouseover', function(event)
this.setOptions(
zIndex: 10,
icons: [
icon: pathSymbol,
offset: '0',
repeat: '40px',
zIndex: 10
],
);
);

flightPath.addListener('mouseout', function(event)
this.setOptions(
zIndex: 0,
icons: [
visible: false,
zIndex: 0
],
);
);


</script>

</body>
</html>









share|improve this question



























    up vote
    1
    down vote

    favorite












    I have a site which has numerous hiking paths displayed as polylines in Google maps. All polylines have Strokecolor green as default. I would like it that when a polyline is clicked, the polyline colour turns red. It should remain red until a second (different) polyline is clicked. The first polyline should then turn back to green and the second polyline turns red.



    I have searched and search and tried numerous solutions, but none work.



    The "test" code I have submitted is "clean" without any of my failed attempts.



     <!DOCTYPE html>
    <html>
    <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
    #map
    height: 100%;

    html, body
    height: 100%;
    margin: 0;
    padding: 0;

    </style>
    </head>
    <body>
    <div id="map"></div>
    <script>
    var map;
    var flightPath;
    var pathSymbol;
    function initMap()
    pathSymbol =
    path: google.maps.SymbolPath.CIRCLE,
    strokeColor: '#FF0000',
    strokeOpacity: 1,
    strokeWeight: 4,
    fillColor: '#FF0000',
    fillOpacity: 1,
    scale: 3
    ;

    map = new google.maps.Map(document.getElementById('map'),
    center: lat: 45, lng: -87,
    zoom: 6
    );

    var flightPlanCoordinates = [
    lat: 42.5, lng: -86.5 ,
    lat: 42.5, lng: -87.5,
    lat: 43.5, lng: -88.5 ,
    lat: 44.5, lng: -88.5 ,
    lat: 46.5, lng: -89.5 ,
    lat: 49.5, lng: -89.5 ,
    ];
    var flightPlanCoordinates2 = [
    lat: 42, lng: -86 ,
    lat: 42, lng: -87,
    lat: 42, lng: -88 ,
    lat: 43, lng: -88 ,
    lat: 44, lng: -89 ,
    lat: 49, lng: -89 ,
    ];
    var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

    for (let i = 0; i < 2; i++)
    addPath(
    path: arrayOfFlightPlans[i],
    strokeColor: '#8CB65F',
    strokeOpacity: 1.0,
    strokeWeight: 4,
    );



    function addPath(props)
    flightPath = new google.maps.Polyline(
    path: props.path,
    strokeColor: props.strokeColor,
    strokeOpacity: props.strokeOpacity,
    strokeWeight: props.strokeWeight
    );
    flightPath.setMap(map);

    flightPath.addListener('mouseover', function(event)
    this.setOptions(
    zIndex: 10,
    icons: [
    icon: pathSymbol,
    offset: '0',
    repeat: '40px',
    zIndex: 10
    ],
    );
    );

    flightPath.addListener('mouseout', function(event)
    this.setOptions(
    zIndex: 0,
    icons: [
    visible: false,
    zIndex: 0
    ],
    );
    );


    </script>

    </body>
    </html>









    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a site which has numerous hiking paths displayed as polylines in Google maps. All polylines have Strokecolor green as default. I would like it that when a polyline is clicked, the polyline colour turns red. It should remain red until a second (different) polyline is clicked. The first polyline should then turn back to green and the second polyline turns red.



      I have searched and search and tried numerous solutions, but none work.



      The "test" code I have submitted is "clean" without any of my failed attempts.



       <!DOCTYPE html>
      <html>
      <head>
      <title>Simple Map</title>
      <meta name="viewport" content="initial-scale=1.0">
      <meta charset="utf-8">
      <style>
      #map
      height: 100%;

      html, body
      height: 100%;
      margin: 0;
      padding: 0;

      </style>
      </head>
      <body>
      <div id="map"></div>
      <script>
      var map;
      var flightPath;
      var pathSymbol;
      function initMap()
      pathSymbol =
      path: google.maps.SymbolPath.CIRCLE,
      strokeColor: '#FF0000',
      strokeOpacity: 1,
      strokeWeight: 4,
      fillColor: '#FF0000',
      fillOpacity: 1,
      scale: 3
      ;

      map = new google.maps.Map(document.getElementById('map'),
      center: lat: 45, lng: -87,
      zoom: 6
      );

      var flightPlanCoordinates = [
      lat: 42.5, lng: -86.5 ,
      lat: 42.5, lng: -87.5,
      lat: 43.5, lng: -88.5 ,
      lat: 44.5, lng: -88.5 ,
      lat: 46.5, lng: -89.5 ,
      lat: 49.5, lng: -89.5 ,
      ];
      var flightPlanCoordinates2 = [
      lat: 42, lng: -86 ,
      lat: 42, lng: -87,
      lat: 42, lng: -88 ,
      lat: 43, lng: -88 ,
      lat: 44, lng: -89 ,
      lat: 49, lng: -89 ,
      ];
      var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

      for (let i = 0; i < 2; i++)
      addPath(
      path: arrayOfFlightPlans[i],
      strokeColor: '#8CB65F',
      strokeOpacity: 1.0,
      strokeWeight: 4,
      );



      function addPath(props)
      flightPath = new google.maps.Polyline(
      path: props.path,
      strokeColor: props.strokeColor,
      strokeOpacity: props.strokeOpacity,
      strokeWeight: props.strokeWeight
      );
      flightPath.setMap(map);

      flightPath.addListener('mouseover', function(event)
      this.setOptions(
      zIndex: 10,
      icons: [
      icon: pathSymbol,
      offset: '0',
      repeat: '40px',
      zIndex: 10
      ],
      );
      );

      flightPath.addListener('mouseout', function(event)
      this.setOptions(
      zIndex: 0,
      icons: [
      visible: false,
      zIndex: 0
      ],
      );
      );


      </script>

      </body>
      </html>









      share|improve this question















      I have a site which has numerous hiking paths displayed as polylines in Google maps. All polylines have Strokecolor green as default. I would like it that when a polyline is clicked, the polyline colour turns red. It should remain red until a second (different) polyline is clicked. The first polyline should then turn back to green and the second polyline turns red.



      I have searched and search and tried numerous solutions, but none work.



      The "test" code I have submitted is "clean" without any of my failed attempts.



       <!DOCTYPE html>
      <html>
      <head>
      <title>Simple Map</title>
      <meta name="viewport" content="initial-scale=1.0">
      <meta charset="utf-8">
      <style>
      #map
      height: 100%;

      html, body
      height: 100%;
      margin: 0;
      padding: 0;

      </style>
      </head>
      <body>
      <div id="map"></div>
      <script>
      var map;
      var flightPath;
      var pathSymbol;
      function initMap()
      pathSymbol =
      path: google.maps.SymbolPath.CIRCLE,
      strokeColor: '#FF0000',
      strokeOpacity: 1,
      strokeWeight: 4,
      fillColor: '#FF0000',
      fillOpacity: 1,
      scale: 3
      ;

      map = new google.maps.Map(document.getElementById('map'),
      center: lat: 45, lng: -87,
      zoom: 6
      );

      var flightPlanCoordinates = [
      lat: 42.5, lng: -86.5 ,
      lat: 42.5, lng: -87.5,
      lat: 43.5, lng: -88.5 ,
      lat: 44.5, lng: -88.5 ,
      lat: 46.5, lng: -89.5 ,
      lat: 49.5, lng: -89.5 ,
      ];
      var flightPlanCoordinates2 = [
      lat: 42, lng: -86 ,
      lat: 42, lng: -87,
      lat: 42, lng: -88 ,
      lat: 43, lng: -88 ,
      lat: 44, lng: -89 ,
      lat: 49, lng: -89 ,
      ];
      var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

      for (let i = 0; i < 2; i++)
      addPath(
      path: arrayOfFlightPlans[i],
      strokeColor: '#8CB65F',
      strokeOpacity: 1.0,
      strokeWeight: 4,
      );



      function addPath(props)
      flightPath = new google.maps.Polyline(
      path: props.path,
      strokeColor: props.strokeColor,
      strokeOpacity: props.strokeOpacity,
      strokeWeight: props.strokeWeight
      );
      flightPath.setMap(map);

      flightPath.addListener('mouseover', function(event)
      this.setOptions(
      zIndex: 10,
      icons: [
      icon: pathSymbol,
      offset: '0',
      repeat: '40px',
      zIndex: 10
      ],
      );
      );

      flightPath.addListener('mouseout', function(event)
      this.setOptions(
      zIndex: 0,
      icons: [
      visible: false,
      zIndex: 0
      ],
      );
      );


      </script>

      </body>
      </html>






      google-maps






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 19:32









      geocodezip

      123k10137167




      123k10137167










      asked Nov 8 at 18:10









      Alastair Green

      245




      245






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          add a global var .. previous path and assign the object value when you click an object then change the color for th actual object and restore th color for the prev_path object



          var prev_path;


          flightPath.addListener('click', function(event)

          this.setOptions(
          zIndex: 10,
          strokeColor: your_strokeColor,
          strokeOpacity: your_strokeOpacity,
          strokeWeight: your__strokeWeight
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );

          if (prevPath)
          prev_path.setOptions(
          zIndex: 10,
          strokeColor: your_color_for_not_select_strokeColor,
          strokeOpacity: your_color_for_not_select_strokeOpacity,
          strokeWeight: your_color_for_not_select_strokeWeight
          );


          prev_path = this;

          );





          share|improve this answer






















          • Perfect. I have updated your code with an if - else there is no prev_path. Only once prev_path is assigned a value should it run through that code
            – Alastair Green
            Nov 9 at 16:19










          • Done. Great answer
            – Alastair Green
            Nov 9 at 16:21

















          up vote
          0
          down vote













          One option would be to keep references to all the polylines, process through that list setting them back to the default value on a click, then set the current polyline to the "selected color.



          var polylines = ;
          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i=0; i<polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )



          proof of concept fiddle



          screenshot of resulting map



          code snippet:






          <script src="https://maps.googleapis.com/maps/api/js"></script>
          <style>
          #map
          height: 100%;


          html,
          body
          height: 100%;
          margin: 0;
          padding: 0;

          </style>
          <div id="map"></div>
          <script>
          var map;
          var flightPath;
          var pathSymbol;

          function initMap()
          pathSymbol =
          path: google.maps.SymbolPath.CIRCLE,
          strokeColor: '#FF0000',
          strokeOpacity: 1,
          strokeWeight: 4,
          fillColor: '#FF0000',
          fillOpacity: 1,
          scale: 3
          ;

          map = new google.maps.Map(document.getElementById('map'),
          center:
          lat: 45,
          lng: -87
          ,
          zoom: 6
          );

          var flightPlanCoordinates = [
          lat: 42.5, lng: -86.5 ,
          lat: 42.5, lng: -87.5,
          lat: 43.5, lng: -88.5 ,
          lat: 44.5, lng: -88.5 ,
          lat: 46.5, lng: -89.5 ,
          lat: 49.5, lng: -89.5 ,
          ];
          var flightPlanCoordinates2 = [
          lat: 42, lng: -86 ,
          lat: 42, lng: -87,
          lat: 42, lng: -88 ,
          lat: 43, lng: -88 ,
          lat: 44, lng: -89 ,
          lat: 49, lng: -89 ,
          ];
          var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

          for (let i = 0; i < 2; i++)
          addPath(
          path: arrayOfFlightPlans[i],
          strokeColor: '#8CB65F',
          strokeOpacity: 1.0,
          strokeWeight: 4,
          );


          var polylines = ;

          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i = 0; i < polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )

          google.maps.event.addDomListener(window, 'load', initMap);
          </script>








          share|improve this answer




















          • This is great and has helped me with another problem. But I'll use the solution from scaisEdge - it's a little simpler. Thank you.
            – Alastair Green
            Nov 9 at 16:17











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213746%2fgoogle-maps-change-colour-of-polyline-when-clicked-but-change-colour-back-to-o%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          add a global var .. previous path and assign the object value when you click an object then change the color for th actual object and restore th color for the prev_path object



          var prev_path;


          flightPath.addListener('click', function(event)

          this.setOptions(
          zIndex: 10,
          strokeColor: your_strokeColor,
          strokeOpacity: your_strokeOpacity,
          strokeWeight: your__strokeWeight
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );

          if (prevPath)
          prev_path.setOptions(
          zIndex: 10,
          strokeColor: your_color_for_not_select_strokeColor,
          strokeOpacity: your_color_for_not_select_strokeOpacity,
          strokeWeight: your_color_for_not_select_strokeWeight
          );


          prev_path = this;

          );





          share|improve this answer






















          • Perfect. I have updated your code with an if - else there is no prev_path. Only once prev_path is assigned a value should it run through that code
            – Alastair Green
            Nov 9 at 16:19










          • Done. Great answer
            – Alastair Green
            Nov 9 at 16:21














          up vote
          1
          down vote



          accepted










          add a global var .. previous path and assign the object value when you click an object then change the color for th actual object and restore th color for the prev_path object



          var prev_path;


          flightPath.addListener('click', function(event)

          this.setOptions(
          zIndex: 10,
          strokeColor: your_strokeColor,
          strokeOpacity: your_strokeOpacity,
          strokeWeight: your__strokeWeight
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );

          if (prevPath)
          prev_path.setOptions(
          zIndex: 10,
          strokeColor: your_color_for_not_select_strokeColor,
          strokeOpacity: your_color_for_not_select_strokeOpacity,
          strokeWeight: your_color_for_not_select_strokeWeight
          );


          prev_path = this;

          );





          share|improve this answer






















          • Perfect. I have updated your code with an if - else there is no prev_path. Only once prev_path is assigned a value should it run through that code
            – Alastair Green
            Nov 9 at 16:19










          • Done. Great answer
            – Alastair Green
            Nov 9 at 16:21












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          add a global var .. previous path and assign the object value when you click an object then change the color for th actual object and restore th color for the prev_path object



          var prev_path;


          flightPath.addListener('click', function(event)

          this.setOptions(
          zIndex: 10,
          strokeColor: your_strokeColor,
          strokeOpacity: your_strokeOpacity,
          strokeWeight: your__strokeWeight
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );

          if (prevPath)
          prev_path.setOptions(
          zIndex: 10,
          strokeColor: your_color_for_not_select_strokeColor,
          strokeOpacity: your_color_for_not_select_strokeOpacity,
          strokeWeight: your_color_for_not_select_strokeWeight
          );


          prev_path = this;

          );





          share|improve this answer














          add a global var .. previous path and assign the object value when you click an object then change the color for th actual object and restore th color for the prev_path object



          var prev_path;


          flightPath.addListener('click', function(event)

          this.setOptions(
          zIndex: 10,
          strokeColor: your_strokeColor,
          strokeOpacity: your_strokeOpacity,
          strokeWeight: your__strokeWeight
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );

          if (prevPath)
          prev_path.setOptions(
          zIndex: 10,
          strokeColor: your_color_for_not_select_strokeColor,
          strokeOpacity: your_color_for_not_select_strokeOpacity,
          strokeWeight: your_color_for_not_select_strokeWeight
          );


          prev_path = this;

          );






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 15:14









          Alastair Green

          245




          245










          answered Nov 8 at 18:54









          scaisEdge

          89.3k94668




          89.3k94668











          • Perfect. I have updated your code with an if - else there is no prev_path. Only once prev_path is assigned a value should it run through that code
            – Alastair Green
            Nov 9 at 16:19










          • Done. Great answer
            – Alastair Green
            Nov 9 at 16:21
















          • Perfect. I have updated your code with an if - else there is no prev_path. Only once prev_path is assigned a value should it run through that code
            – Alastair Green
            Nov 9 at 16:19










          • Done. Great answer
            – Alastair Green
            Nov 9 at 16:21















          Perfect. I have updated your code with an if - else there is no prev_path. Only once prev_path is assigned a value should it run through that code
          – Alastair Green
          Nov 9 at 16:19




          Perfect. I have updated your code with an if - else there is no prev_path. Only once prev_path is assigned a value should it run through that code
          – Alastair Green
          Nov 9 at 16:19












          Done. Great answer
          – Alastair Green
          Nov 9 at 16:21




          Done. Great answer
          – Alastair Green
          Nov 9 at 16:21












          up vote
          0
          down vote













          One option would be to keep references to all the polylines, process through that list setting them back to the default value on a click, then set the current polyline to the "selected color.



          var polylines = ;
          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i=0; i<polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )



          proof of concept fiddle



          screenshot of resulting map



          code snippet:






          <script src="https://maps.googleapis.com/maps/api/js"></script>
          <style>
          #map
          height: 100%;


          html,
          body
          height: 100%;
          margin: 0;
          padding: 0;

          </style>
          <div id="map"></div>
          <script>
          var map;
          var flightPath;
          var pathSymbol;

          function initMap()
          pathSymbol =
          path: google.maps.SymbolPath.CIRCLE,
          strokeColor: '#FF0000',
          strokeOpacity: 1,
          strokeWeight: 4,
          fillColor: '#FF0000',
          fillOpacity: 1,
          scale: 3
          ;

          map = new google.maps.Map(document.getElementById('map'),
          center:
          lat: 45,
          lng: -87
          ,
          zoom: 6
          );

          var flightPlanCoordinates = [
          lat: 42.5, lng: -86.5 ,
          lat: 42.5, lng: -87.5,
          lat: 43.5, lng: -88.5 ,
          lat: 44.5, lng: -88.5 ,
          lat: 46.5, lng: -89.5 ,
          lat: 49.5, lng: -89.5 ,
          ];
          var flightPlanCoordinates2 = [
          lat: 42, lng: -86 ,
          lat: 42, lng: -87,
          lat: 42, lng: -88 ,
          lat: 43, lng: -88 ,
          lat: 44, lng: -89 ,
          lat: 49, lng: -89 ,
          ];
          var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

          for (let i = 0; i < 2; i++)
          addPath(
          path: arrayOfFlightPlans[i],
          strokeColor: '#8CB65F',
          strokeOpacity: 1.0,
          strokeWeight: 4,
          );


          var polylines = ;

          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i = 0; i < polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )

          google.maps.event.addDomListener(window, 'load', initMap);
          </script>








          share|improve this answer




















          • This is great and has helped me with another problem. But I'll use the solution from scaisEdge - it's a little simpler. Thank you.
            – Alastair Green
            Nov 9 at 16:17















          up vote
          0
          down vote













          One option would be to keep references to all the polylines, process through that list setting them back to the default value on a click, then set the current polyline to the "selected color.



          var polylines = ;
          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i=0; i<polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )



          proof of concept fiddle



          screenshot of resulting map



          code snippet:






          <script src="https://maps.googleapis.com/maps/api/js"></script>
          <style>
          #map
          height: 100%;


          html,
          body
          height: 100%;
          margin: 0;
          padding: 0;

          </style>
          <div id="map"></div>
          <script>
          var map;
          var flightPath;
          var pathSymbol;

          function initMap()
          pathSymbol =
          path: google.maps.SymbolPath.CIRCLE,
          strokeColor: '#FF0000',
          strokeOpacity: 1,
          strokeWeight: 4,
          fillColor: '#FF0000',
          fillOpacity: 1,
          scale: 3
          ;

          map = new google.maps.Map(document.getElementById('map'),
          center:
          lat: 45,
          lng: -87
          ,
          zoom: 6
          );

          var flightPlanCoordinates = [
          lat: 42.5, lng: -86.5 ,
          lat: 42.5, lng: -87.5,
          lat: 43.5, lng: -88.5 ,
          lat: 44.5, lng: -88.5 ,
          lat: 46.5, lng: -89.5 ,
          lat: 49.5, lng: -89.5 ,
          ];
          var flightPlanCoordinates2 = [
          lat: 42, lng: -86 ,
          lat: 42, lng: -87,
          lat: 42, lng: -88 ,
          lat: 43, lng: -88 ,
          lat: 44, lng: -89 ,
          lat: 49, lng: -89 ,
          ];
          var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

          for (let i = 0; i < 2; i++)
          addPath(
          path: arrayOfFlightPlans[i],
          strokeColor: '#8CB65F',
          strokeOpacity: 1.0,
          strokeWeight: 4,
          );


          var polylines = ;

          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i = 0; i < polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )

          google.maps.event.addDomListener(window, 'load', initMap);
          </script>








          share|improve this answer




















          • This is great and has helped me with another problem. But I'll use the solution from scaisEdge - it's a little simpler. Thank you.
            – Alastair Green
            Nov 9 at 16:17













          up vote
          0
          down vote










          up vote
          0
          down vote









          One option would be to keep references to all the polylines, process through that list setting them back to the default value on a click, then set the current polyline to the "selected color.



          var polylines = ;
          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i=0; i<polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )



          proof of concept fiddle



          screenshot of resulting map



          code snippet:






          <script src="https://maps.googleapis.com/maps/api/js"></script>
          <style>
          #map
          height: 100%;


          html,
          body
          height: 100%;
          margin: 0;
          padding: 0;

          </style>
          <div id="map"></div>
          <script>
          var map;
          var flightPath;
          var pathSymbol;

          function initMap()
          pathSymbol =
          path: google.maps.SymbolPath.CIRCLE,
          strokeColor: '#FF0000',
          strokeOpacity: 1,
          strokeWeight: 4,
          fillColor: '#FF0000',
          fillOpacity: 1,
          scale: 3
          ;

          map = new google.maps.Map(document.getElementById('map'),
          center:
          lat: 45,
          lng: -87
          ,
          zoom: 6
          );

          var flightPlanCoordinates = [
          lat: 42.5, lng: -86.5 ,
          lat: 42.5, lng: -87.5,
          lat: 43.5, lng: -88.5 ,
          lat: 44.5, lng: -88.5 ,
          lat: 46.5, lng: -89.5 ,
          lat: 49.5, lng: -89.5 ,
          ];
          var flightPlanCoordinates2 = [
          lat: 42, lng: -86 ,
          lat: 42, lng: -87,
          lat: 42, lng: -88 ,
          lat: 43, lng: -88 ,
          lat: 44, lng: -89 ,
          lat: 49, lng: -89 ,
          ];
          var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

          for (let i = 0; i < 2; i++)
          addPath(
          path: arrayOfFlightPlans[i],
          strokeColor: '#8CB65F',
          strokeOpacity: 1.0,
          strokeWeight: 4,
          );


          var polylines = ;

          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i = 0; i < polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )

          google.maps.event.addDomListener(window, 'load', initMap);
          </script>








          share|improve this answer












          One option would be to keep references to all the polylines, process through that list setting them back to the default value on a click, then set the current polyline to the "selected color.



          var polylines = ;
          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i=0; i<polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )



          proof of concept fiddle



          screenshot of resulting map



          code snippet:






          <script src="https://maps.googleapis.com/maps/api/js"></script>
          <style>
          #map
          height: 100%;


          html,
          body
          height: 100%;
          margin: 0;
          padding: 0;

          </style>
          <div id="map"></div>
          <script>
          var map;
          var flightPath;
          var pathSymbol;

          function initMap()
          pathSymbol =
          path: google.maps.SymbolPath.CIRCLE,
          strokeColor: '#FF0000',
          strokeOpacity: 1,
          strokeWeight: 4,
          fillColor: '#FF0000',
          fillOpacity: 1,
          scale: 3
          ;

          map = new google.maps.Map(document.getElementById('map'),
          center:
          lat: 45,
          lng: -87
          ,
          zoom: 6
          );

          var flightPlanCoordinates = [
          lat: 42.5, lng: -86.5 ,
          lat: 42.5, lng: -87.5,
          lat: 43.5, lng: -88.5 ,
          lat: 44.5, lng: -88.5 ,
          lat: 46.5, lng: -89.5 ,
          lat: 49.5, lng: -89.5 ,
          ];
          var flightPlanCoordinates2 = [
          lat: 42, lng: -86 ,
          lat: 42, lng: -87,
          lat: 42, lng: -88 ,
          lat: 43, lng: -88 ,
          lat: 44, lng: -89 ,
          lat: 49, lng: -89 ,
          ];
          var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

          for (let i = 0; i < 2; i++)
          addPath(
          path: arrayOfFlightPlans[i],
          strokeColor: '#8CB65F',
          strokeOpacity: 1.0,
          strokeWeight: 4,
          );


          var polylines = ;

          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i = 0; i < polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )

          google.maps.event.addDomListener(window, 'load', initMap);
          </script>








          <script src="https://maps.googleapis.com/maps/api/js"></script>
          <style>
          #map
          height: 100%;


          html,
          body
          height: 100%;
          margin: 0;
          padding: 0;

          </style>
          <div id="map"></div>
          <script>
          var map;
          var flightPath;
          var pathSymbol;

          function initMap()
          pathSymbol =
          path: google.maps.SymbolPath.CIRCLE,
          strokeColor: '#FF0000',
          strokeOpacity: 1,
          strokeWeight: 4,
          fillColor: '#FF0000',
          fillOpacity: 1,
          scale: 3
          ;

          map = new google.maps.Map(document.getElementById('map'),
          center:
          lat: 45,
          lng: -87
          ,
          zoom: 6
          );

          var flightPlanCoordinates = [
          lat: 42.5, lng: -86.5 ,
          lat: 42.5, lng: -87.5,
          lat: 43.5, lng: -88.5 ,
          lat: 44.5, lng: -88.5 ,
          lat: 46.5, lng: -89.5 ,
          lat: 49.5, lng: -89.5 ,
          ];
          var flightPlanCoordinates2 = [
          lat: 42, lng: -86 ,
          lat: 42, lng: -87,
          lat: 42, lng: -88 ,
          lat: 43, lng: -88 ,
          lat: 44, lng: -89 ,
          lat: 49, lng: -89 ,
          ];
          var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

          for (let i = 0; i < 2; i++)
          addPath(
          path: arrayOfFlightPlans[i],
          strokeColor: '#8CB65F',
          strokeOpacity: 1.0,
          strokeWeight: 4,
          );


          var polylines = ;

          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i = 0; i < polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )

          google.maps.event.addDomListener(window, 'load', initMap);
          </script>





          <script src="https://maps.googleapis.com/maps/api/js"></script>
          <style>
          #map
          height: 100%;


          html,
          body
          height: 100%;
          margin: 0;
          padding: 0;

          </style>
          <div id="map"></div>
          <script>
          var map;
          var flightPath;
          var pathSymbol;

          function initMap()
          pathSymbol =
          path: google.maps.SymbolPath.CIRCLE,
          strokeColor: '#FF0000',
          strokeOpacity: 1,
          strokeWeight: 4,
          fillColor: '#FF0000',
          fillOpacity: 1,
          scale: 3
          ;

          map = new google.maps.Map(document.getElementById('map'),
          center:
          lat: 45,
          lng: -87
          ,
          zoom: 6
          );

          var flightPlanCoordinates = [
          lat: 42.5, lng: -86.5 ,
          lat: 42.5, lng: -87.5,
          lat: 43.5, lng: -88.5 ,
          lat: 44.5, lng: -88.5 ,
          lat: 46.5, lng: -89.5 ,
          lat: 49.5, lng: -89.5 ,
          ];
          var flightPlanCoordinates2 = [
          lat: 42, lng: -86 ,
          lat: 42, lng: -87,
          lat: 42, lng: -88 ,
          lat: 43, lng: -88 ,
          lat: 44, lng: -89 ,
          lat: 49, lng: -89 ,
          ];
          var arrayOfFlightPlans = [flightPlanCoordinates, flightPlanCoordinates2];

          for (let i = 0; i < 2; i++)
          addPath(
          path: arrayOfFlightPlans[i],
          strokeColor: '#8CB65F',
          strokeOpacity: 1.0,
          strokeWeight: 4,
          );


          var polylines = ;

          function addPath(props)
          flightPath = new google.maps.Polyline(
          path: props.path,
          strokeColor: props.strokeColor,
          strokeOpacity: props.strokeOpacity,
          strokeWeight: props.strokeWeight,
          normalOptions:
          strokeColor: props.strokeColor,
          ,
          selectedOptions:
          strokeColor: "red",

          );
          flightPath.setMap(map);
          polylines.push(flightPath);

          flightPath.addListener('mouseover', function(event)
          this.setOptions(
          zIndex: 10,
          icons: [
          icon: pathSymbol,
          offset: '0',
          repeat: '40px',
          zIndex: 10
          ],
          );
          );

          flightPath.addListener('mouseout', function(event)
          this.setOptions(
          zIndex: 0,
          icons: [
          visible: false,
          zIndex: 0
          ],
          );
          );
          flightPath.addListener('click', function(event)
          for (var i = 0; i < polylines.length; i++)
          polylines[i].setOptions(polylines[i].normalOptions)

          this.setOptions(this.selectedOptions);
          )

          google.maps.event.addDomListener(window, 'load', initMap);
          </script>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 19:31









          geocodezip

          123k10137167




          123k10137167











          • This is great and has helped me with another problem. But I'll use the solution from scaisEdge - it's a little simpler. Thank you.
            – Alastair Green
            Nov 9 at 16:17

















          • This is great and has helped me with another problem. But I'll use the solution from scaisEdge - it's a little simpler. Thank you.
            – Alastair Green
            Nov 9 at 16:17
















          This is great and has helped me with another problem. But I'll use the solution from scaisEdge - it's a little simpler. Thank you.
          – Alastair Green
          Nov 9 at 16:17





          This is great and has helped me with another problem. But I'll use the solution from scaisEdge - it's a little simpler. Thank you.
          – Alastair Green
          Nov 9 at 16:17


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213746%2fgoogle-maps-change-colour-of-polyline-when-clicked-but-change-colour-back-to-o%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Crossroads (UK TV series)

          ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế