Monday, November 3, 2008

Find Nearby Place

So... if you are given a latitude and a longitude values and you want to find a nearby place, then you have to check out GeoNames. They have a web API that you can access for free (as in beer :-) Here is a simple usage of the API using the Flex API for AGS:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:esri="http://www.esri.com/2008/ags"
layout="absolute"
>
<mx:Script>
<![CDATA[
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.Graphic;
import com.esri.ags.events.MapMouseEvent;
import mx.rpc.events.ResultEvent;

private function mapClickHandler( event : MapMouseEvent ) : void
{
const urlVar : URLVariables = new URLVariables();
urlVar.lat = event.mapPoint.y.toFixed( 6 );
urlVar.lng = event.mapPoint.x.toFixed( 6 );
httpService.send( urlVar );
}

private function resultHandler( event : ResultEvent ) : void
{
const geonames : XML = event.result as XML;
const geoname : XML = geonames.geoname[0];
const name : String = geoname.name;
const lat : Number = geoname.lat;
const lng : Number = geoname.lng;
const graphic : Graphic = new Graphic( new MapPoint( lng, lat));
graphic.toolTip = name;
gl.add( graphic );
}
]]>
</mx:Script>
<mx:HTTPService id="httpService"
useProxy="false"
resultFormat="e4x"
url="http://ws.geonames.org/findNearbyPlaceName"
result="resultHandler(event)"/>
<esri:Map
openHandCursorVisible="false"
mapClick="mapClickHandler(event)">
<esri:ArcGISTiledMapServiceLayer
url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer id="gl">
<esri:symbol>
<esri:SimpleMarkerSymbol color="0xFF0000"/>
</esri:symbol>
</esri:GraphicsLayer>
</esri:Map>
</mx:Application>

3 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

Hy,
I tried to get working your example.

Unfortanly, I didn't find the "mapClick"-event-method in the map class or the MapMouseEvent-class in the agslib-1.0-2008-10-22 release.

On which Flex-API version is your example based? How can I get running it?

Thanks,
Tom

thunderhead said...

Yea - saw that :-( This will be in the next version ! In the meantime u have to use "click" and then in the click handler, u have to convert the stageX/Y to a mapPoint using map.toMapFromStage - Sorry about that