Search code examples
apache-flexairgeolocation

Adobe Air - IP to location


Is there a way to get the broad position of a user based on the IP address? (I'm building an Adobe Air desktop app for notebook users)


Solution

  • Just tried out http://www.netimpact.com/, like you were looking for it's broad position, it placed me 3.7 miles away from my actual location, seems relatively simple to use and can respond with delimited data that can be split using the split method or else can choose to get JSON encoded object data which can be easily read into an AS3 object in Flex using JSON.parse(). Be sure to read up the FAQ on their site to get a better understanding of how they retrieve the data and what the accuracy is generally like, they allow 1000 requests per developer per day if you need more they have pricing starting at 20 bucks a month.

    NOTE: Netimpact has shut down and they migrated users to their new provider, KickFire. They've also broadened their offering to include not only IP2GEO, but also IP2Company, & Domain2IP.

    EDIT:

    A bit more information on exploring this further and some sample code. So the only problem I'm seeing with my solution is getting the WAN IP address, I think you'll need to use some sort of server side script to bounce back the users external IP in order to make this work entirely. I attempted using the NetworkInfo but this will always be reporting based on LAN IP addresses which won't work for the geoip look-up. So this leads me to using PHP or some other server side script for obtaining the IP. Further discussion on getting the IP using PHP here: http://www.phpbuilder.com/board/showthread.php?t=10327697

    <?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/mx"
                           creationComplete="windowedapplication1_creationCompleteHandler(event)">
    
        <fx:Script>
            <![CDATA[
                import mx.events.FlexEvent;
                import mx.rpc.events.ResultEvent;
                import mx.rpc.http.HTTPService;
    
                private const MY_API_KEY:String = "PUT YOUR OWN KEY HERE";
    
                [Bindable]
                private var ipLookupResult:Object;
    
                protected function button1_clickHandler(event:MouseEvent):void
                {
                    var httpService:HTTPService = new HTTPService();
                    httpService.url = "http://api.geoio.com/q.php?key="+MY_API_KEY+"&qt=geoip&d=json&q=PUT_YOUR_IP_HERE";
                    httpService.addEventListener(ResultEvent.RESULT, lookupResultHandler);
                    httpService.send();
                }
                private function lookupResultHandler(event:ResultEvent):void
                {
                    ipLookupResult = JSON.parse(event.result as String);
                }
    
                protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
                {
                    for each(var ni:NetworkInterface in NetworkInfo.networkInfo.findInterfaces())
                    {
                        var addresses:Vector.<InterfaceAddress> = ni.addresses;
                        var userIp:String = addresses[0].address;
                        trace(userIp);
                    }
                }
    
            ]]>
        </fx:Script>
    
        <fx:Declarations>
            <!-- Place non-visual elements (e.g., services, value objects) here -->
        </fx:Declarations>
        <s:layout>
            <s:VerticalLayout/>
        </s:layout>
        <s:Button click="button1_clickHandler(event)" label="Look me up"/>
        <s:Form>
            <s:FormItem label="City">
                <s:Label text="{ipLookupResult[0][0]}"/>
            </s:FormItem>
            <s:FormItem label="State">
                <s:Label text="{ipLookupResult[0][1]}"/>
            </s:FormItem>
            <s:FormItem label="Country">
                <s:Label text="{ipLookupResult[0][2]}"/>
            </s:FormItem>
            <s:FormItem label="ISP">
                <s:Label text="{ipLookupResult[0][3]}"/>
            </s:FormItem>
            <s:FormItem label="Latitude">
                <s:Label text="{ipLookupResult[0][4]}"/>
            </s:FormItem>
            <s:FormItem label="Longitude">
                <s:Label text="{ipLookupResult[0][5]}"/>
            </s:FormItem>
            <s:FormItem label="Country Code">
                <s:Label text="{ipLookupResult[0][6]}"/>
            </s:FormItem>
        </s:Form>
    </s:WindowedApplication>
    

    If you don't have access to write your own server side scripts you can also look to external services to obtain the information, I believe whatismyip.com offers this but not sure about licensing/cost and I know they specifically don't want you to use the main page to scrape out the IP (there's undoubtedly some free version of this service available if they charge).