Iphone HTML5 Geolocation always returns an error

I try to use html5 geolocation on Iphone, but always return an error.

var win = function (position) {alert ("Everything is in order!" + position)};
var fail = function (e) {alert ('Can \' t get position. \ nError: '+ e)};
navigator.geolocation.getCurrentPosition (win, fail);

I am testing an iOS simulator and my device, but it is not working. In Safari, I can get my position using a wireless Internet connection (??).

Is there any way to make this work?

+6
source share
2 answers

Make sure your location services on the device are turned on for Safari. Go to Settings> Privacy> Location Services> Safari and make sure it's set to ON .

Then try this code:

 function doGPS() { try{ //position request navigator.geolocation.getCurrentPosition( function (position) { alert(position.coords.latitude); alert(position.coords.longitude); }); } catch(evt) { alert(evt.message); } } 
+15
source

I used appMobi js and it works fine if I reload the site in Safari on the iPhone. However, bookmarked applications on the main screen do not do this (even if I add a new version to the web application).

The new js is loaded into the bookmarked application (I added a pop-up message with a new invitation every time I updated the code to make sure the web application loads the new js), and iPhone promtps me if I want to allow the site to check, but nothing happens when I say yes. If lat / long is displayed in Safari.

Has anyone had a similar problem?

UPDATE: it works, but only the first time you load the page - the second time it displays "start", but does not display lat / long ...

 if (navigator.geolocation) { alert('Geolocation is supported!'); } else { alert('Geolocation is not supported for this Browser/OS version yet.'); } try{ //position request alert('start'); navigator.geolocation.getCurrentPosition( function (position) { alert(position.coords.latitude); alert(position.coords.longitude); }); } catch(evt) { alert('fail'+evt.message); } 
0
source

Source: https://habr.com/ru/post/889997/


All Articles