开发者

Iphone HTML5 Geolocation always return a error

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?


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);
    }

}


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜