Select to view content in your preferred language

Displaying point from an XML

2856
9
10-29-2010 10:34 AM
ZahidChaudhry
Deactivated User
Lets say that i have an xml file that look like this
<?xml version="1.0"?>
<points>
   <point>
      <lat>38.12</lat>
      <long>-81.25</long>
   </point>
   <point>
      <lat>39.23</lat>
      <long>-81.25</long>
   </point>
</Points>

what is the simplest way to display them on a map....
Assume that xml and app are going to be on same server...i know its piece of cake with FLEx and silver light API but  failed trying with javascript

Thanks

Zahid
0 Kudos
9 Replies
derekswingley1
Deactivated User
There's a sample that shows how to get (cross-domain) xml data via a proxy page:  Access xml data.

But I guess that's probably not what you want...try this:
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title> points from json^H^H^H^H xml! </title>
  <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.1/js/dojo/dijit/themes/claro/claro.css">
  <style>
   html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
   #map{ padding:0; }
  </style>
  <script type="text/javascript">var djConfig = {parseOnLoad: true};</script>
  <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.1"></script>
  <script type="text/javascript">
   dojo.require("dijit.layout.BorderContainer");
   dojo.require("dijit.layout.ContentPane");
   dojo.require("esri.map");
   var map;

   function init() {
    var initExtent = new esri.geometry.Extent({"xmin":-13632648,"ymin":4542594,"xmax":-13621699,"ymax":4546875,"spatialReference":{"wkid":102100}});
    map = new esri.Map("map",{extent:initExtent}); 
    var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
    map.addLayer(basemap);

    var resizeTimer;
    dojo.connect(map, 'onLoad', function(theMap) {
     dojo.connect(dijit.byId('map'), 'resize', function() { 
      clearTimeout(resizeTimer);
      resizeTimer = setTimeout( function() {
       map.resize();
       map.reposition();
      }, 500);
     });
     getXML();
    });
   }
   function getXML() {
    dojo.xhrGet({
     'url': 'points.xml',
     'content': {},
     'handleAs': 'xml',
     'load': displayPoints,
     'error': error
    });
   }
   function displayPoints(pts) {
    console.log('success: ', pts);
    xmlpts = pts;
    var sym = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 20, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,0]), 1), new dojo.Color([255,0,0,0.5]));
    dojo.forEach(dojo.query('point', pts), function(pt) {
     // might be a cleaner way to do this with dojox.xml.parser but this works so...
     var lat = parseFloat(dojo.query('lat', pt)[0].childNodes[0].data);
     var lng = parseFloat(dojo.query('long', pt)[0].childNodes[0].data);
     var geom = esri.geometry.geographicToWebMercator(new esri.geometry.Point(lng, lat, new esri.SpatialReference({wkid: 4326})));
     var graphic = new esri.Graphic(geom, sym);
     map.graphics.add(graphic);
    });
    // zoom to our newly added graphics
    map.setExtent(esri.graphicsExtent(map.graphics.graphics), true);
   }
   function error(error) {
    console.log('failed: ', error);
   }
   dojo.addOnLoad(init);
  </script>
 </head>
 
 <body class="claro">
  <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false"
  style="width: 100%; height: 100%; margin: 0;">
   <div id="map" dojotype="dijit.layout.ContentPane" region="center" style="overflow:hidden;">
   </div>
  </div>
 </body>
</html>
0 Kudos
ZahidChaudhry
Deactivated User
Thanks. Thats exactly what i was looking for.
0 Kudos
derekswingley1
Deactivated User
Glad to demonstrate it's a piece of cake in JS as well :).
0 Kudos
ZahidChaudhry
Deactivated User
Glad to demonstrate it's a piece of cake in JS as well :).

Impressive..
One more thing and i think you can help me here. what if my url to xml is somthing like this...how i use this xml(considering it is the same xml mentioned above in the thread)

http://myhost/myxml.php?aNo=1234

this is php script that produces the xml dynamically.

Thanks
0 Kudos
derekswingley1
Deactivated User
What happens when you use that url?
0 Kudos
ZahidChaudhry
Deactivated User
What happens when you use that url?

As i mentioned earlier i m totally new to JS, and still not quite sure, how i am gona pass the parameters in my submit request....
Thanks for your continued help
0 Kudos
derekswingley1
Deactivated User
In the getXML() function in the code I posted, change the 'url' property from 'points.xml' to the url to your php page.
0 Kudos
ManojrajTeli
Deactivated User
1) Is it possible to bind info Window mentioned http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm#jssamples/widget_popupfl... on click to show some fields from the same node where lat lng is stored.
2) Since my data is coming from sql server and converted to xml my second question is that is it posible to display line feature and polygon feature ?
0 Kudos
derekswingley1
Deactivated User
1) Is it possible to bind info Window mentioned http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm#jssamples/widget_popupfl... on click to show some fields from the same node where lat lng is stored.
2) Since my data is coming from sql server and converted to xml my second question is that is it posible to display line feature and polygon feature ?


Please post a new question rather than reviving a nearly two year old thread.
0 Kudos