So I’ve been working on an app that is based around manipulating a Google Map. I start with the map zoomed out and then I want zoom in and center the map on the location of the user. I have two inputs for this 1) Manually enter address 2) Automatically locate.
For the Manually entered address the Geocoding works perfectly. My issue is with the “Auto-location”. The first thing that came to my mind was to use the user’s IP address to locate them via one of the many “geo ip” sites. The issue I’m having is, using the code shown below, I’m getting an “HTTPError 0” if I try to run it in the Form (or Module). But, if I run the same code in the Server Module, the code works as intended but obviously just pulls the IP and location of the server not the user.
So I guess there are really two questions here: 1) Is there a better way to automatically get user location? 2) If using IP address is the way to go, how can I get the below code to not error when run in the Form?
Relevant Code Snippet:
def auto_location():
send_url = 'http://freegeoip.net/json'
r = anvil.http.request(send_url, json=True)
lat = r['latitude']
lon = r['longitude']
print(lat)
print(lon)
return lat, lon
“HTTP Error 0” is what you get when your browser refuses to make the connection. Usually this is because it’s an unencrypted URL (this is what’s happening here - Anvil apps are served over HTTPS, so the browser will refuse to connect to unencrypted HTTP resources), or because the remote site does not allow cross-origin requests. We’re going to make the error message better for this case
You should try using https://freegeoip.net/json, which should just work (I’m guessing they have cross-origin requests enabled). In the longer term, we are going to provide a function for getting the client IP from server code. I’ve +1ed both of these TODO items in our issue tracker.
meredydd, thanks for the suggestion. I tried https://freegeoip.net/json and it is still giving me the HTTPError 0. The site loads normally if I paste the https url into my browser it appears they accept both requests.
Could it be a different issue? I’ll try to troubleshoot other sites
Just a very rough throw together, and you’ll have to change the API URL to whatever your IDE suggests, but it works here.
I fetch the user’s IP through an API call to the server, then I have a server function to fetch the geo information (I couldn’t get the form to fetch it directly).
This isn’t a limitation on trial accounts! You should be able to use this just fine, and I want to get to the bottom of this.
Nick, it would be most helpful if you could create an Anvil app that demonstrates this problem for us, and share the clone link here on the forum.
(BTW, we should expect access to google.com to fail - they disallow cross-site requests! Likewise, accessing HTTP endpoints on someone else’s app from your own app should fail, unless they’ve explicitly added CORS headers. This is why David has modified his app to use get_api_origin() so your clone will access its own URL, not his!)
As you can see in the MarkingModule function auto_mark_self I’m getting the lat and lon from the function auto_location in the same module, which generates the HTTPError0. If you instead use the server call version (commented out initially) you can see what I want to happen and it “works” but obviously pulls the server IP address.
Thanks for the app! That made it really easy for me to test
I ran it, and got the HTTPError code 0. Sadly, the only way to get more detail than that is to open the browser’s developer tools (which are scary and unfriendly, I know), where I saw the following error message:
So, it looks like the URL you were accessing doesn’t permit cross-origin requests, which triggered the error.
Looking more closely, though, it was trying to redirect from https://freegeoip.net/json to https://freegeoip.net/json/ (note the slash at the end). So I tried updating your source code to the new URL – and it turns out that URL does permit cross-origin requests! I got a nice flag showing me (approximately) the Anvil office in Cambridge:
Result!
This is one of those areas where the web is really tricky. Anvil does a lot of work to turn the trickiness of the web into a smooth API; unfortunately cross-origin policies are one of those things we can’t protect you from. Thanks for sticking with it, and I look forward to seeing your app develop further