Justin Erswell
Justin Erswell

Reputation: 708

Google Maps Waypoints using Checkboxes

Can anyone tell me how to convert the following code to work using checkboxes instead..

var checkboxArray = document.getElementById("waypoints");
for (var i = 0; i < checkboxArray.length; i++) {
  if (checkboxArray.options[i].checked == true) {
    waypts.push({
        location:checkboxArray[i].value,
        stopover:true});

I have a number of checkboxes on the page with different values in them and I would like the user to click the points they would like to add to their route.

Help!!

Cheers

Justin

Upvotes: 0

Views: 547

Answers (1)

Jayapal Chandran
Jayapal Chandran

Reputation: 11150

This is the sample code from google documentation for google map version 3 which plots waypoints between already set start and end locations. and here is the answer.

assume that you have checkboxes like the following

<input type='checkbox' name='waypoints[]' value='someplace1'>
<input type='checkbox' name='waypoints[]' value='someplace2'>
<input type='checkbox' name='waypoints[]' value='someplace3'>
<input type='checkbox' name='waypoints[]' value='someplace4'>
<input type='checkbox' name='waypoints[]' value='someplace5'>

and then the following code will work for you.

var checkboxArray = document.getElementsByName("waypoints[]");
for (var i = 0; i < checkboxArray.length; i++) {
  if (checkboxArray[i].checked == true) {
    waypts.push({
        location:checkboxArray[i].value,
        stopover:true});

But if the user clicks the checkbox in random order then .... it is left to you to decide.

Upvotes: 2

Related Questions