1

I've taken a look at all the other questions but basically, I have a long list of coordinates as a string that looks like this: "42.2723998, -81.23239 ... 42.84099, -81.3990398" which I have use javascript .split(" , "); on so that its now an array called coordinate[] of strings each holding one coordinate and then using the below code:

// create a coordinate Array
    var polygonCoords = [];
    // creates a new LatLng
    var j = 0;
    var z = j + 1;
while (z < coordinate.length) {
    if ((j%2) === 0) {
    var co1 = parseFloat(coordinate[z]);
    //document.write(coordinate[j]);
    var co2 = parseFloat(coordinate[j]);
    //document.write(co2);
    var newLatLng = new google.maps.LatLng(co1,co2);

    polygonCoords.push(newLatLng);
} else {
    var co2 = parseFloat(coordinate[z]);
    var co1 = parseFloat(coordinate[j]);
    var newLatLng = new google.maps.LatLng(co1,co2);
    polygonCoords.push(newLatLng);
}
    z++;
    j++;
    }

but when I print out the polygonCoords array, it always returns the longitude as 0 and I've also parsed it from a string as well using parseFloat(). Also when I explicitly return the longitude by its own, it returns the actual number. I just need it to work so that I can create an array of LatLngs that I can later use as a path for a polygon.

12
  • Why such a convoluted way of going through the coordinates? Could you please provide a complete example array? Why are you spliting on " ' "? Doesn't look like that character is in the example string you provided. Commented Jun 10, 2015 at 15:45
  • Oops, sorry, meant to write the comma (",") for split, sorry, wrote this question too fast Commented Jun 10, 2015 at 15:47
  • Basically what I'm working on is creating an array of polygons and then attaching a customized infowindow to each one where the information for the infowindow should be stored in a kml file, however, I cannot change the kml file so I'm using javascript to basically do the job Commented Jun 10, 2015 at 15:50
  • What do you mean you "cannot change the kml file"? Why are you re-inventing the wheel? There are third party KML parsers that will parse KML for you and create native google maps javascript API v3 polygons (geoxml3, geoxml-v3). Commented Jun 10, 2015 at 15:59
  • Here is the original string : -81.2338973051004,42.9872488004691,0 -81.2337121944366,42.9872547337441,0 -81.233528492502,42.9872724884156,0 -81.233347597307,42.9873019293669,0 ... (its too long to put in one comment and i keep getting (42.9872488004691, -81.23389730510041),(42.9872488004691, 0),(42.9872547337441, 0),(42.9872547337441, 0),(42.9872724884156, 0),(42.9872724884156, 0),(42.9873019293669, 0),(42.9873019293669, 0),(42.9873428325462, 0),(42.9873428325462, 0)... also just the kml file holds many different polygons which is why I also need to separate those out Commented Jun 10, 2015 at 16:02

1 Answer 1

1

Ok, I've fixed it, somehow when I grabbed the longitude from the kml file, it would be in the form of 0 -81.290390... and so whenever i parsed it into float, it would recognize it as 0 instead of -81.293848. Now its done properly, and printing out the correct floats.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.