Select to view content in your preferred language

CenterAt not working

3889
6
Jump to solution
10-25-2012 03:33 AM
TimMcCurdy
Occasional Contributor
Hello all,

I signed up to ask this question: Does CenterAt work? And if so, what am I doing wrong?

I've created a simple function to move to some coordinates:

 this.goto = function () {     var coordinates = prompt("Enter coordinates: ", "502982,221818").split(",");     if (coordinates.length != 2) {        return;     }     var point = new esri.geometry.Point({ "x": coordinates[0], "y": coordinates[1], " spatialReference": { " wkid": 27700} });     self.map.centerAt(point);     self.map.setLevel(10);  }


However, only the setLevel seems to happen. I am never centered to the new results.

Any help would be appreciated!
Thanks.
1 Solution

Accepted Solutions
TimMcCurdy
Occasional Contributor
Using that code on that page seems to work, something must be wrong with my mapping setup I suppose.

I swapped to using self.map.spacialReference instead of creating a new one, but this has not helped.

However, I tried carrying out similar commands in the console, and for some reason it works:

var coordinates = prompt("Enter coordinates: ", "502982,221818").split(","); var mapControl = Symology.MapMgr.getControl('984604c6-0bd2-4ba5-a990-aa9d6675d1ed'); mapControl.map.centerAt(new esri.geometry.Point(coordinates[0], coordinates[1], mapControl.map.spatialReference));


I was a bit suspicious, so I removed the line that setLevel(10), and now my original code works.

Swaping around the commands, so setLevel THEN centerAt, seems to work. However my original, centerAt THEN setLevel seems to override the centerAt command.

Thanks for your help!

View solution in original post

6 Replies
derekswingley1
Deactivated User
The x and y properties of the object passed to the point constructor need to be numbers. Try converting to numbers with parseInt:
var point = new esri.geometry.Point({ "x": parseInt(coordinates[0]), "y": parseInt(coordinates[1]), " spatialReference": { " wkid": 27700} });
0 Kudos
TimMcCurdy
Occasional Contributor
The x and y properties of the object passed to the point constructor need to be numbers. Try converting to numbers with parseInt:
var point = new esri.geometry.Point({ "x": parseInt(coordinates[0]), "y": parseInt(coordinates[1]), " spatialReference": { " wkid": 27700} });


This did not resolve the issue, and I'm not sure why it would since its a JSON object.

Just to be sure, I changed to standard parameters and this did not work either:

var point = new esri.geometry.Point(parseInt(coordinates[0]), parseInt(coordinates[1]), new esri.SpatialReference({ wkid: 27700 }));
0 Kudos
derekswingley1
Deactivated User
That's what I get for posting without testing...my bad.

What is your map's spatial reference?

You can confirm that centerAt works by going to this sample:  http://help.arcgis.com/en/webapi/javascript/arcgis/samples/map_topo/index.html

Then entering this in the console: 
var coordinates = prompt("Enter coordinates: ", "-13600000,4545000").split(",");


Press OK. Then enter this to center the map:
map.centerAt(new esri.geometry.Point(coordinates[0], coordinates[1], map.spatialReference));


Or...
map.centerAt(new esri.geometry.Point({ "x": coordinates[0], "y": coordinates[1], "spatialReference": map.spatialReference}));
0 Kudos
TimMcCurdy
Occasional Contributor
Using that code on that page seems to work, something must be wrong with my mapping setup I suppose.

I swapped to using self.map.spacialReference instead of creating a new one, but this has not helped.

However, I tried carrying out similar commands in the console, and for some reason it works:

var coordinates = prompt("Enter coordinates: ", "502982,221818").split(","); var mapControl = Symology.MapMgr.getControl('984604c6-0bd2-4ba5-a990-aa9d6675d1ed'); mapControl.map.centerAt(new esri.geometry.Point(coordinates[0], coordinates[1], mapControl.map.spatialReference));


I was a bit suspicious, so I removed the line that setLevel(10), and now my original code works.

Swaping around the commands, so setLevel THEN centerAt, seems to work. However my original, centerAt THEN setLevel seems to override the centerAt command.

Thanks for your help!
JeffPace
MVP Alum
i think this is do to javascript being asynchronous.  centerat fires (but does not complete), so when setlevel fires the map center is still the old one. 

centerat then completes, and then setlevel does.  So the final command is setlevel using the original center.  Hence it looks like centerat never fired

have you tried the centerAndZoom method?

http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/map.htm#centerAndZoom
0 Kudos
TimMcCurdy
Occasional Contributor
i think this is do to javascript being asynchronous.  centerat fires (but does not complete), so when setlevel fires the map center is still the old one. 

centerat then completes, and then setlevel does.  So the final command is setlevel using the original center.  Hence it looks like centerat never fired

have you tried the centerAndZoom method?

http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/map.htm#centerAndZoom


I am using a dynamic service in this instance, so zooming by a factor is not what I want.

Thank you for the help in this though, I don't need any more assistance now that I realise I just had to swap my commands around.
0 Kudos