0

I have a XML file like this :

<datas>
<title>AAA</title>
<lat>111</lat>
<lng>5454</lng>
</datas>
<datas>
<title>BBB</title>
<lat>112121</lat>
<lng>544254</lng>
</datas>

....

Example : Here is a "real" example :

<productFeed>
<Deal>
<Deal_ID>201090531</Deal_ID>
<Price>9.50</Price>
<Destination_Name>Green River</Destination_Name>
<Latitude>38.989869999999996</Latitude>
<Longitude>-110.13261</Longitude>
<Country_Name>United States of America</Country_Name>
</Deal>
</productFeed>

The initialize function :

function initialize() {
  var myOptions = {
    zoom: 7,
    center: new google.maps.LatLng(35.82672127366604,-118.916015625),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  downloadUrl("test.xml", function(doc) {
      var xmlDoc = xmlParse(doc);
      var markers = xmlDoc.documentElement.getElementsByTagName("Deal");
      for (var i = 0; i < markers.length; i++) {
        var lat = parseFloat(markers[i].getAttribute("Latitude"));
        var lng = parseFloat(markers[i].getAttribute("Longitude"));
        var latlng = new google.maps.LatLng(lat,lng);
        var html = markers[i].getAttribute("Deal_ID");
        var label = markers[i].getAttribute("Price");
        var marker = createMarker(latlng,label,html);
        }
      });
    }

Is Google Map API able to read it and display it on a map ? If so, how ?

I tried adapt this https://developers.google.com/maps/documentation/javascript/layers?hl=en but does not work.

Thanks for your help. Dominique

3
  • 1
    what is "rad"? Is that supposed to be "read"? Those numbers are not valid latitude and longitudes (at lease in Google Maps standard coordinate system), are they just random numbers or do they need to be converted to WGS84?
    – geocodezip
    Jul 30, 2013 at 12:51
  • Yes, read. Lat and lng are exemples, not true data. Just for explain the structure of the xml file.
    – dommm063
    Jul 30, 2013 at 12:56
  • Is there a reason you can't put your XML in the format your code is expecting?
    – geocodezip
    Jul 30, 2013 at 15:42

1 Answer 1

1

The Google Maps Javascript API v3 does not contain any XML parsing. But you can parse XML with javascript and display the results on a Google Maps Javascript API v3 map.

Your code currently parses XML of this format:

 <Deal Latitude="38.989869999999996" Longitude="-110.13261" Deal_ID="201090531" Price="9.50" />

function from geoxml3 that returns the text content of an element (like <lat>):

//nodeValue: Extract the text value of a DOM node, with leading and trailing whitespace trimmed
geoXML3.nodeValue = function(node, defVal) {
  var retStr="";
  if (!node) {
    return (typeof defVal === 'undefined' || defVal === null) ? '' : defVal;
  }
   if(node.nodeType==3||node.nodeType==4||node.nodeType==2){
      retStr+=node.nodeValue;
   }else if(node.nodeType==1||node.nodeType==9||node.nodeType==11){
      for(var i=0;i<node.childNodes.length;++i){
         retStr+=arguments.callee(node.childNodes[i]);
      }
   }
   return retStr;
};

relevant discussion from the Google Maps Javascript API v2 group (but v3 doesn't have the GXml functionality).

another relevant discussion

8
  • OK. Any exemple of a map displaying markers from a XML file like mine ?
    – dommm063
    Jul 30, 2013 at 13:22
  • I know I have seen them, couldn't find any easily. Your format is similar to (but not identical to) some RSS/atom feeds. The 3rd party KML parser geoxml3 contains code to parse out the contents of an XML element nodeValue, added to the answer.
    – geocodezip
    Jul 30, 2013 at 13:52
  • If you had posted XML that made sense to parse, I might have done that. Why don't you give it a shot and ask more specific questions when/if you run into trouble.
    – geocodezip
    Jul 30, 2013 at 14:21
  • Here is a "real" example (I can't answer, just make comments): <productFeed> <Deal> <Deal_ID>201090531</Deal_ID> <Price>9.50</Price> <Destination_Name>Green River</Destination_Name> <Latitude>38.989869999999996</Latitude> <Longitude>-110.13261</Longitude> <Country_Name>United States of America</Country_Name> </Deal> </productFeed>
    – dommm063
    Jul 30, 2013 at 15:01
  • Please add that to your question. What does your code look like to parse it?
    – geocodezip
    Jul 30, 2013 at 15:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.