Can iPhone web apps get GPS position?

家住魔仙堡 提交于 2019-12-02 17:11:30

Here's a snippet on how to read location from the iPhone. Looks like it requires 3.0:

 navigator.geolocation.getCurrentPosition(foundLocation, noLocation);

 function foundLocation(position)
 {
   var lat = position.coords.latitude;
   var long = position.coords.longitude;
   alert('Found location: ' + lat + ', ' + long);
 }
 function noLocation()
 {
   alert('Could not find location');
 }

See: http://mapscripting.com/how-to-use-geolocation-in-mobile-safari

By the way, if you want to use web code on the iPhone, there are a couple middle-ground solutions you could try that don't force you to create a native app but allow you to wrap your site in an app and get access to GPS and other native features.

Check out the app SendLocation under navigation by Jouni Erola.

Its a simply app that will send out the lat & lon of the iPhone to YOUR server. Simply enter your server url to receive the location as HTTP-GET methid

I've don this without any programming. You can:

  1. Download the iPhone app "Basic GPS" from iPhone Store.
  2. Start an account at Twitter.com (if you don't already got one).
  3. Start an e-mail-to-twitter account at Twittermail.com.
  4. In Basic GPS settings use your secret e-mail address from Twittermail.
  5. At Twitter.com/widgets click "Other" to get your HTML-code for publishing your Tweets elsewhere.
  6. Put this HTML code on your homepage.
  7. In Basic GPS you just click the blue "I" (on) button, "Email" and "Send" to send your position to Twittermail, which publish it on Twitter. And Twitter will automaticly publish it on your homepage.

See a working axample at http://CharlieBloom.com. Only the 3 latest (customizeable) Tweets are visible, click on "Follow me on Twitter" to se "My position ....." and a Google Maps-link. Positions are updated on my website 2-3 minutes after I have sent them.

Best regards, Charlie Bloom

Yes, but the accuracy is pretty bad. I just built a web form that posts GPS settings from the watchPosition() function to my database and then maped the results. While paddling down a local river it got only two of seven posts correct. The rest were outside the river, and two of those were about a mile away! I'm now looking for a better solution.

Here's the code I use to update the form fields:

<script type="text/javascript">
function getGPSLocation()
{
  if(navigator.geolocation)
  {
   navigator.geolocation.watchPosition(onGeoSuccess);
  } else {
   alert("Your browser or device doesn't support Geolocation");
  }
}

function onGeoSuccess(position) {
  document.getElementById("Latitude").value =  position.coords.latitude;
  document.getElementById("Longitude").value = position.coords.longitude;
}
</script>

And this is the form. Update to suite your needs.

<form>
<img src="spacer.gif" OnLoad="getGPSLocation();">
<input type="Text" name="Latitude" id="Latitude" value="">
<input type="Text" name="Longitude" id="Longitude" value="">
</form>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!