Select to view content in your preferred language

Building Extent From Map Cords to Select a Point

742
2
12-09-2010 07:57 AM
MattMoyles
Occasional Contributor
I am trying to select a point feature on the map using the mouse.  Currently I have some code like this:

var xMin:Number = map.toScreen(event.mapPoint).x - 50; 
var yMin:Number = map.toScreen(event.mapPoint).y - 50; 
var xMax:Number = map.toScreen(event.mapPoint).x + 50; 
var yMax:Number = map.toScreen(event.mapPoint).y + 50;
var mp1:MapPoint = map.toMapFromStage(xMin,yMin);
var mp2:MapPoint = map.toMapFromStage(xMax,yMax);
var ext:Extent = new Extent(mp1.x, mp1.y, mp2.x, mp2.y, map.spatialReference);
                        
trace("Clicked at: " + event.mapPoint);
trace("Built Extent: (" + mp1.x + ", " + mp1.y + ") to (" + mp2.x + ", " + mp2.y + ")"); 
                        
var q:Query = new Query();
q.geometry = ext;
layer.selectFeatures(q);


It looks like it is doing everything correctly by the trace output I get, but it is not selecting the feature.  Here is some of my trace output:

Clicked at: MapPoint[x=-8907144.365098419,y=4748988.529237461,SpatialReference[102100]]
Built Extent: (-8937749.7512238, 4804053.764414099) to (-8876600.128595663, 4742904.141785962)
Clicked at: MapPoint[x=-8907144.365098419,y=4745931.048106054,SpatialReference[102100]]
Built Extent: (-8937749.7512238, 4800965.708471378) to (-8876600.128595663, 4739816.08584324)
Clicked at: MapPoint[x=-8907144.365098419,y=4743485.063200928,SpatialReference[102100]]
Built Extent: (-8937749.7512238, 4798519.723566253) to (-8876600.128595663, 4737370.100938114)


Also my code works fine if I just use this as the extent:
var ext:Extent = new Extent(event.mapPoint.x - 2000, event.mapPoint.y - 2000, event.mapPoint.x + 2000, event.mapPoint.y + 2000, map.spatialReference);

but the problem is that this makes the selection area to large when you scroll down to a closer level of detail, so I thought converting mouse cords to map cords might make for a cleaner solution
Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus
Matt,

   Try this instead:

var xMin:Number = map.toScreen(event.mapPoint).x - 50; 
var yMin:Number = map.toScreen(event.mapPoint).y - 50; 
var xMax:Number = map.toScreen(event.mapPoint).x + 50; 
var yMax:Number = map.toScreen(event.mapPoint).y + 50;
var mp1:MapPoint = map.toMap(new Point(xMin,yMin));
var mp2:MapPoint = map.toMap(new Point(xMax,yMax));
var ext:Extent = new Extent(mp1.x, mp1.y, mp2.x, mp2.y, map.spatialReference);
0 Kudos
MattMoyles
Occasional Contributor
Thanks Robert!

I also found that I needed to do this as well....

q.geometry = WebMercatorUtil.webMercatorToGeographic(ext) as Extent;
0 Kudos