Reputation: 81
I trying to use html5 geolocation on Iphone, but it always returns a error.
var win = function(position) { alert('Everything is fine! ' + position) };
var fail = function(e) { alert('Can\'t retrieve position.\nError: ' + e) };
navigator.geolocation.getCurrentPosition(win, fail);
I testing on IOS Simulator and my device, but doesnt work. On Safari i just can get my position using a wireless internet connection (??).
There's a way to make this work?
Upvotes: 7
Views: 17404
Reputation: 779
Make sure that your Location Services on the device are on for Safari. Go to Settings > Privacy > Location Services > Safari and make sure it is 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);
}
}
Upvotes: 18
Reputation: 2181
I've used appMobi's js, and it works fine if I reload a site in Safari on the iPhone. However, home screen bookmarked apps aren't doing it (even if I bookmark a new version on the web app).
The new js is loading into the bookmarked app fine (I added a pop up message with a new prompt each time I updated the code, just to make sure the web app is loading the new js), and the iPhone promtps me if I want to allow the site to check, but nothing happens when I say yes. Whereas in Safari, it displays the lat/long.
Anyone had a similar problem?
UPDATE: it works, but only the first time you load a page - the second time through, it displays 'start', but won't display the 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);
}
Upvotes: 0