Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

One of the actions a user can perform using the ESRI Flex Viewer application is to draw a shape on a map (e.g. a line). Once the user is finished drawing the line, they will double-click the mouse which fires a DrawEvent.DRAW_END event. When this is fired, the line is measured using a call to an API function geometryService.project([geometryObject], spatialReference);.

I want to be able to call this function after a MouseEvent.MOUSE_MOVE event in order to measure the line on the fly, instead of only after the DRAW_END event. Unfortunately, the only two DrawEvents that get dispatched are the DrawEvent.DRAW_START and DrawEvent.DRAW_END. These ESRI classes are compiled classes, so I cannot make any changes to them.

Is there a way to add an eventListener to a MOUSE_MOVE event that can grab the current geometry of the line that is being drawn and call my API function to measure on the fly?

Thanks in advance.

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

I found a solution to this problem by circumventing the geometry service altogether and building a solution of my own. There is a great site that implements the Vincenty algorithm for measuring the distance between two coordinates. This is a very accurate approximation algorithm (within .5mm on Vincenty's earth ellipsoid), and is meant to be very efficient so that it can be used frequently (e.g. after a mouse moves).

Here's the basic steps to implement this:

  1. Add an event listener to the map that listens for a mouse click
  2. Once the mouse is clicked, convert the coordinates of the location of the click on the screen to map coordinates and add a listener to watch for a mouse move event
  3. When the mouse moves, grab the coordinates of the mouse (as in step 2) and perform the Vincenty calculation. Create a custom tooltip on the map and add the Vincenty result to the tooltip text.
  4. Upon double-click, send the result to the Geometry service and remove the tooltip from the map
share|improve this answer
add comment

I want to be able to call this function after a MouseEvent.MOUSE_MOVE event in order to measure the line on the fly

First off, some words of caution. I implemented similar functionality in a Flex app awhile back i.e. When user moved mouse around the map, it kept pinging the geometry service for the distance or xy coordinates and displayed it alongside the cursor.

Issue - The Geometry service could not keep up, and often the lag would result in the label showing the geometry service result would be incorrect, as the mouse had moved on. e.g. You moved the mouse quickly one direction and then another, it would show the XY coordinates from the previous location.

So, I got around this, by only firing a request to the geometry service, once the cursor had not moved for longer than 0.2s. This worked a lot better.

Please refer to the Flex3 mouse events reference or add a listener via AS3 with this reference.

Ill see if I can dig out the code, but not sure if I still have it.

Edit

How to grab the geometry that is being drawn when the event fires

Refer to this sample.
- This shows you how to ping the geometry service with the current geometry of the polyline to get the length.
The listener for this is when the graphic has been finished (onDrawEnd). You just need to modify this to fire when your cursor stops moving.

Few Esri forum posts on this topic as well.

share|improve this answer
    
Simon thanks for the help. This is a concern of mine as well, since the geometry service takes a significant time to perform the measurement as is. Firing the event listener only after a .2 second wait period should be fairly trivial, but I am still stumped on how to grab the geometry that is being drawn when the event fires. Any ideas on how to do this? –  Kyle May 13 '11 at 14:19
    
Another thing u might want to consider - do your users want the length to be the last segment length, or the total length. I would imagine the segment. –  Simon May 13 '11 at 23:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.