0

Hello I hope I explain this correctly. I am trying to make polygons in Google Maps with coordinates from a XML feed. Problem is the cordinates in the XML feed as not shown as Lat Lon they are shown like this in the feed.

<cap:polygon>
29.66,-97.19 29.40,-97.24 29.11,-97.61 29.35,-97.85 29.57,-97.66 29.68,-97.36 29.66,-97.19
</cap:polygon>

So I need to parse the coordinates out of the XML feed, convert them into google.maps.LatLng objects, so I can push them into an array, so I can provide that array as the paths property in the google.maps.Polygon constructor.

Now this is where I am stuck as I am not sure how to go about parsing that feed element so they are properly formatted the way I need them so I can construct the polygons unless there is a way I can construct the polygons with the way it's formatted now.

Any suggestions or advice would be great!

-Thanks!

Texan78
  • 687
  • 2
  • 16
  • 42
  • Please follow this link, Ther is two methods name drawPth() and decodePoly() which will solve your problem- http://stackoverflow.com/questions/14702621/answer-draw-path-between-two-points-using-google-maps-android-api-v2 – Ashutosh Singh Apr 30 '13 at 07:14
  • Thanks but that is for poly lines not polygons and that is also for Android not Google Maps V3. – Texan78 Apr 30 '13 at 07:28
  • Actually you asked, how to parse the response in the desired way. so i suggested to follow those two methods only mentioned above. – Ashutosh Singh Apr 30 '13 at 07:30
  • how would a JSONParser help to parse a XML-document? – Dr.Molle Apr 30 '13 at 07:36
  • I clearly stated Polygons in my post not poly lines. Thanks for your comment. – Texan78 Apr 30 '13 at 07:56

1 Answers1

2
  1. Get the content of that XML element into a string. Note that with the "cap" namespace you will have to write browser independent code (different browsers treat that differently).

  2. split the string on spaces (" "):

    var coordinates = polygonElemStr.split(" ");
    
  3. split each set of coordinates on the comma (","), create a google.maps.LatLng from the two numbers and push it onto an array:

    var path = [];
    for (var i=0; i<coordinates.length; i++) {
      var coord = coordinates[i].split(",");
      path.push(new google.maps.LatLng(parseFloat(coord[0]),
                                       parseFloat(coord[1])));
    }
    
  4. use that array of coordinates to create your polygon.

example

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • That makes sense thank you. Do I add that to my existing map code? I am assuming yes as it would need to know what data to parse correct? What would the new element be called? coord? So for the dumb questions, I am kind of out of me element on this as I am trying to learn new things. – Texan78 Apr 30 '13 at 07:59
  • Yes sorry, I just saw it. That is exactly what I am looking for. That makes sense now. path is the new name for path. I have an example map http://www.mesquiteweather.net/googlemap_poly.html but it's still not working for some reason. Map shows but the polygons don't. Is there script include I am missing that needs to be called by any chance? – Texan78 Apr 30 '13 at 08:58