Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geolocation for iPhone JavaScript Web App

I am building a full-stack web app in JavaScript that is intended to be used on mobile devices (Android and iPhone). Using the HTML5 Geolocation API on Android phones works great, but there appears to be an authorization issue with the iPhone. When interacting with the web app as an iPhone user, iOS automatically denies the user permission to use location services, and so unlike the Android phones, there is no pop-up that gives the user the option to allow location services. I've done a lot of tinkering with the iPhone location services settings to no avail. Is there a JavaScript snippet that enables the user to allow permission for location services? Thanks.

like image 504
Jacob Wallace Avatar asked Aug 14 '26 20:08

Jacob Wallace


1 Answers

Your location setting for Safari might be set to OFF, you can change it here: Settings > Privacy > Location Services > Safari

You could also try something like this:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction);
} else {
    // Make API call to the GeoIP services
}

And use some service like http://ip-api.com (but geodata based on ip is not very accurate)

Here you can find more info about getCurrentPosition():

  • https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition
  • https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/GettingGeographicalLocations/GettingGeographicalLocations.html
like image 173
d2718nis Avatar answered Aug 16 '26 11:08

d2718nis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!