Problems with map.setextent()

804
1
04-26-2011 11:21 AM
DavidMcCutcheon
New Contributor
I am building a website that will pull multiple lat/long points from a database and plot them on a map.  The problem I'm having is when I go to set the extent to show only the points plotted, the extent only shows the first point.

I'm using "map.setExtent(NewExtent, true)", and when I step through the code the line is hit for every point, but only the first call seems to matter.

I've included a code snippet below.  Also, this is my first ESRI project so if you see me doing something dumb, let me know and I'll correct it.

Thanks for the help.

Code below:
function init() {
            map = new esri.Map("CallMap");
            var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer", {opacity:0});
            map.addLayer(basemap);
            map.setExtent(esri.geometry.geographicToWebMercator(new esri.geometry.Extent((-75.646399), (40.824849), (-74.883529), (41.248469), new esri.SpatialReference({ wkid: 2271 }))));

            
            dojo.connect(map, 'onLoad', function () {
                var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,255,0.65]), 2), new dojo.Color([0,0,255,0.35]));
  addToMap(map, 41.092236000, -75.323746000, 'images/pnt.png', '149');
  addToMap(map, 40.970196000, -75.277355000, 'images/pnt2.png', '19');

            });

            
        }



        function addToMap(map, lat, lon, pic, dis) {
            var myExtent = new esri.geometry.Extent((lon - 0.003), (lat - 0.003), (lon + 0.003), (lat + 0.003), new esri.SpatialReference({ wkid: 2271 }));
            myExtent = esri.geometry.geographicToWebMercator(myExtent);
            var point = new esri.geometry.Point(lon, lat, new esri.SpatialReference({ wkid: 2271 }));
            var point_wm = esri.geometry.geographicToWebMercator(point);
            var picsymbol = esri.symbol.PictureMarkerSymbol(pic, 33, 40);
            var attr = { "XCoord": lon, "YCoord": lat, "Desc": dis };
            var infoTemplate1 = new esri.InfoTemplate("Latitude: ${YCoord} <br/>Longitude: ${XCoord}", "POI: <br/>${Desc}");




            polygonGraphic = new esri.Graphic(point_wm, picsymbol, attr, infoTemplate1);
            map.graphics.add(polygonGraphic);

            map.setExtent(myExtent.expand(1.25), true);
        }
 dojo.addOnLoad(init);
0 Kudos
1 Reply
MarioObendorfer
New Contributor
Hi,

the code you posted should show the second point not the first. But that doesn't matter ... you are setting the map's extend every time when you add a point theirfore you won't see all points you've added.

You can do 2 things to see all points:
- Add every point you add to the map additional to a polygon. After all points are added set the extent to the geometry of this polygon.
- Save the coordinates of the points in xmin,ymin,xmax,ymax variables (if they are min or max) and after you have added all points build an extent out of this 4 variables and then set the map extent.

Hope that heplsp.

Greets,
Tol
0 Kudos