Get approximate location from IP

This is a simple (Windows) function that returns the latitude and longitude determined from your IP address by looking up up at ipinfo.io.

Not as good as being able to use e.g. browser location data which is pretty accurate however I haven't figured how to do that in Igor, yet....

Should work in all versions of Igor that run on Windows for as far back as the ExecuteScriptText command goes. On Mac I'm sure there is an easy modification. 

//___________________________________________________________________________________________________  
// funtion to get the approximate location of the computer from the IP address
Function [Variable latitude, Variable Longitude] getApproximateLocation()

    Variable i
    String linestr
    ExecuteScriptText/B/Z "cmd.exe /C curl ipinfo.io" // ipinfo.io

    do
        LineStr = StringFromList(i,S_value, "\n")
        // Success
        if (Stringmatch(lineStr,"*loc*"))
            sscanf lineStr, "   \"loc\": \"%f,%f\",", latitude, longitude
            break
        endif
        i+=1
    while (i < ItemsInList(S_value, "\n")) 
   
    return [latitude, longitude]
end
//___________________________________________________________________________________________________  

 

You can use the built-in FetchURL function instead of ExecuteScriptText. That's been available since Igor 6.something (6.3 IIRC). However we only started supporting the https:// scheme in Igor 7, and it appears that ipinfo.io always redirects to https, so you can't use IP6 at that site. But even with IP8 the string returned by the server doesn't contain the location information (I think that requires javascript to be executed, which curl can't do).

A Google search for "free geolocation api" turned up https://ip-api.com/docs. Their service works with IP6 and later:

 

print fetchurl("http://ip-api.com/json/")

Igor does not have a documented json parser, but the json returned by this query is simple enough to parse using regular text commands.

Here is a more neat solution:

//___________________________________________________________________________________________________  
// function to get the approximate location of the computer and UTC offset from the IP address
Function [Variable latVar,Variable lonVar,Variable offVar] getApproxLocationTimeZone()
   
    String rtnStr = fetchurl("http://ip-api.com/json/?fields=status,lat,lon,offset&quot;)
    latVar = NumberByKey("\"lat\"",rtnStr,":",",") 
    lonVar = NumberByKey("\"lon\"",rtnStr,":",",")
    offVar = NumberByKey("\"offset\"",rtnStr,":",",")
       
    return [latVar,lonVar,offVar]
End
//___________________________________________________________________________________________________

 

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More