There are so many fun things you can do with web services, and most of them are trivial to use in , and type:
http.get("https://api.ipify.org")
This should return an IP address, something like 66.212.203.27.
Now, to get information about that IP address, including the city it's in and an approximate latitude/longitude, we'll use a different web service:
http.get("http://ip-api.com/json/66.212.203.27")
You can replace the IP address above with whatever you got from the first query. The result will be a string of JSON code that contains your city, region, country, latitude, and longitude, along with the name of your internet service provider (ISP) and other details.
Putting this all together into a little program. Use edit to enter the code editor, then paste in the following:
import "json"
import "stringUtil"
clear
// What's my external IP address?
myIP = http.get("https://api.ipify.org")
// And where am I?
locJson = http.get("http://ip-api.com/json/" + myIP)
locInfo = json.parse(locJson)
pprint locInfo
print "You are in {city}, {region} ({country})".fill(locInfo)
Run this, and it should tell you where you are (along with all those other details). Neat, right?
Don't Tell Me, Show Me!
But we can make it more fun. Let's pinpoint you on a map of the Earth! edit your program again, delete the last two lines (pprint and print), and add in this additional code:
mapImage = file.loadImage("/sys/pics/earth_day.jpg")
// Find the pixel coordinates of our lat/long in this image
px = (locInfo.lon + 180) / 360 * mapImage.width
py = mapImage.height - (90 - locInfo.lat) / 180 * mapImage.height
// Draw a marker (using an offscreen PixelDisplay)
g = new PixelDisplay
g.clear color.black, mapImage.width, mapImage.height
g.drawImage mapImage
g.line 0, py, g.width, py, "#00FFFF88", 3
g.line px, 0, px, g.height, "#00FFFF88", 3
g.fillEllipse px-10, py-10, 20, 20, "#00FFFFFF"
g.drawEllipse px-15, py-15, 30, 30, "#00FFFFAA"
g.drawEllipse px-20, py-20, 40, 40, "#00FFFF88"
markedMap = g.getImage(0, 0, g.width, g.height)
// And draw the mapImage, fit to the screen!
scale = 960/mapImage.width
gfx.drawImage markedMap,
480 - mapImage.width*scale/2, 320 - mapImage.height*scale/2,
mapImage.width*scale, mapImage.height*scale
s = "{city}, {region}, ({countryCode})".fill(locInfo)
text.row = 2
text.column = 34 - s.len/2
print s
The result, if you happen to be my neighbor right now, should look like this:
Never Be Lost Again
The http module is an often-overlooked gem in the Mini Micro APIs. With one little call we can get our external IP address, and with a second little call, get all sorts of information about that, including the info we need to place ourselves on a map. We also looked at how you are able — nay, encouraged! — to steal from the demo code and make it your own.
What do you think of this? Let me know in the comments below!
SOCIAL SHARE CARD GENERATOR