Select to view content in your preferred language

Overlay Point from local text file

2538
2
04-19-2010 02:07 AM
by Anonymous User
Not applicable
Original User: khayer

I have a CSV file which contains lat,lon,ID fields. I want to read the csv and add those point as new graphics layer. Read the csv using javascript is not problem. But i can add those point to map using ESRI javascript which i can do very easily by google map api. Below i mention my code,

      function init() {
 
       var startExtent = new esri.geometry.Extent(-102.5,36.8,-99.8,38.4, new    esri.SpatialReference({wkid:4326}) );
      
       var map = new esri.Map("map", {extent:startExtent});

        var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");
        map.addLayer(tiledMapServiceLayer);
 
  //Create graphics layer for counties
          var countyLayer = new esri.layers.GraphicsLayer();
         
  var lat=null
  var lon=null;

  var markerSymbol = new esri.symbol.PictureMarkerSymbol("flagNormal.png", 30, 30);
  
  var atts = {};
  atts["ID"] = 1;
     
  lat=37.514;
  lon=-101.1719;
  var location = new esri.geometry.Point(lon, lat, new esri.SpatialReference({wkid:4326}));
  var graphic = new esri.Graphic(location, markerSymbol, atts);
  graphic.titleField = "ID";
 
  countyLayer.add(graphic);
 
  map.addLayer(countyLayer);
      }

Here, map can not add new graphic layer. i have tested map.graphics that is always null. I have also tested the code for both 2.0 and 1.6. But i is failed to load graphics. Do i am in right track? Is there other alternative to do this kind of task?

Thanks
Khayer
GIS developer,
CEGIS,
Bangladesh
0 Kudos
2 Replies
KellyHutchins
Esri Notable Contributor
Your code was close - but you have to wait until the map loads to add graphics. In the snippet below note that it waits until the onLoad event fires to add the graphics to the map.
 
    function init() {
        var startExtent = new esri.geometry.Extent(-102.5,36.8,-99.8,38.4, new esri.SpatialReference({wkid:4326}) );
        var map = new esri.Map("map", {extent:startExtent});

        var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");
        map.addLayer(tiledMapServiceLayer);

        dojo.connect(map, 'onLoad', function(map) {
          //Create graphics layer for counties
          var countyLayer = new esri.layers.GraphicsLayer();

          var lat=null
          var lon=null;

          var markerSymbol = new esri.symbol.SimpleMarkerSymbol();

          var atts = {};
          atts["ID"] = 1;

          lat=37.514;
          lon=-101.1719; 
          var location = new esri.geometry.Point(lon, lat, new esri.SpatialReference({wkid:4326}));
          var graphic = new esri.Graphic(location, markerSymbol, atts);
          graphic.titleField = "ID";

          countyLayer.add(graphic);

          map.addLayer(countyLayer);
        
        });

      }
0 Kudos
by Anonymous User
Not applicable
Original User: khayer

It's working .. Thank Kelly.
0 Kudos