Iphone HTML5 Geolocation always return a error

孤街醉人 提交于 2019-12-10 04:24:37

问题


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?


回答1:


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

}



回答2:


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


来源:https://stackoverflow.com/questions/6271191/iphone-html5-geolocation-always-return-a-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!