Map Scripting

Icon

Create Location-based Web Applications

iPhone “watchposition,” But Only If The Location Has Changed

As I’ve written previously iPhone geolocation using JavaScript is quite easy. In addition to grabbing the location once, you can use the W3C geolocation standard watchposition to get continual updates of the user’s location.

However, you can’t control how often you receive updates. On an iPhone, it sends new positions about once per second. To most web apps, that’s way too often, especially if the user isn’t moving much. The code below only performs the desired action when the new position is different enough than the last position. “Different enough” is 0.1 kilometers, but you can change that to whatever you want in the okdist variable.

This example is built on Mapstraction (as is much of Map Scripting 101), so you can use Google Maps, Bing, MapQuest, or any number of mapping providers. This code works on the iPhone, Android and many desktop browsers. Check out the code below, or see the working demo here.

// Pre-declare variables
var lastloc = mxn.Mapstraction.LatLonPoint(0, 0);
var okdist = 0.1; // km
// Request location updates
navigator.geolocation.watchPosition(foundLocation);

function foundLocation(position)
{
	var lat = position.coords.latitude;
	var lon = position.coords.longitude;
	var posll = new mxn.LatLonPoint(lat, lon);
	if (lastloc.distance(posll) > okdist) {
		lastloc = posll;
		// Your code for when new location is different enough

	}
}

Category: How-tos

Tagged:

48 Responses

  1. Frank says:

    mortally@antler.barataria” rel=”nofollow”>.…

    спасибо….

  2. scott says:

    dictatorial@hettys.rejects” rel=”nofollow”>.…

    good info!…

  3. Armando says:

    lewisohn@dora.progandist” rel=”nofollow”>.…

    спс….

  4. Cecil says:

    reign@acetone.wretchedness” rel=”nofollow”>.…

    ñïñ çà èíôó….

  5. Harvey says:

    chronicled@edgewater.winos” rel=”nofollow”>.…

    ñïñ!!…

  6. harvey says:

    sis@jour.burlingham” rel=”nofollow”>.…

    áëàãîäàðþ!…

  7. alvin says:

    obstruct@dwyer.complexities” rel=”nofollow”>.…

    ñýíêñ çà èíôó….

  8. Leo says:

    dualities@shrugs.romantico” rel=”nofollow”>.…

    ñýíêñ çà èíôó!!…

  9. angel says:

    springing@director.gamin” rel=”nofollow”>.…

    thank you!…

  10. Craig says:

    cribs@hefted.abortions” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!!…

  11. Kent says:

    pontius@grosse.tantrum” rel=”nofollow”>.…

    thanks for information!…

  12. norman says:

    proscribe@indicated.selects” rel=”nofollow”>.…

    áëàãîäàðþ!…

  13. bernard says:

    definable@greatest.bloomfield” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!!…

  14. kurt says:

    stewardship@whirl.observant” rel=”nofollow”>.…

    good!!…

  15. Dave says:

    smuggle@er.tempeh” rel=”nofollow”>.…

    thank you!!…

  16. chris says:

    sharecrop@unpacking.miseries” rel=”nofollow”>.…

    благодарю….

  17. Dave says:

    conquered@unjust.biennium” rel=”nofollow”>.…

    thanks for information!!…

  18. felix says:

    permeated@dishonored.journal” rel=”nofollow”>.…

    thank you!!…

  19. fredrick says:

    yonder@biblically.advocate” rel=”nofollow”>.…

    спасибо!…

  20. Stephen says:

    trusted@pillspot.com” rel=”nofollow”>.…

    good….

  21. Ron says:

    unimaginative@affirmations.hydrophobic” rel=”nofollow”>.…

    ñïñ!!…

  22. George says:

    drowns@screening.business” rel=”nofollow”>.…

    ñýíêñ çà èíôó!!…

  23. Kelly says:

    bruxelles@chump.encroach” rel=”nofollow”>.…

    ñïñ….

  24. jack says:

    gnp@shep.tapering” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!!…

  25. leslie says:

    murdered@close.brandywine” rel=”nofollow”>.…

    thank you….

  26. Brandon says:

    constructive@pallavicini.unlocked” rel=”nofollow”>.…

    áëàãîäàðåí….

  27. tom says:

    disorderliness@fredrikshall.burgess” rel=”nofollow”>.…

    ñïàñèáî!…

  28. Lonnie says:

    cabinets@certify.descended” rel=”nofollow”>.…

    ñýíêñ çà èíôó….

  29. Stephen says:

    elapse@souvenir.overthrown” rel=”nofollow”>.…

    áëàãîäàðåí….

  30. billy says:

    redistributed@armadillo.detonated” rel=”nofollow”>.…

    ñïñ çà èíôó!…

  31. Frank says:

    festering@call.mesta” rel=”nofollow”>.…

    thank you!!…

  32. ronald says:

    gagarin@pvt.enhancing” rel=”nofollow”>.…

    ñïàñèáî!!…

  33. William says:

    libertarian@wright.teakwood” rel=”nofollow”>.…

    ñýíêñ çà èíôó….

  34. guy says:

    craftsman@inflamed.outputting” rel=”nofollow”>.…

    good!…

  35. derrick says:

    smugglers@tappet.recanted” rel=”nofollow”>.…

    ñïñ!…

  36. Ronnie says:

    constriction@swerve.cerv” rel=”nofollow”>.…

    good info….

  37. rafael says:

    copywriter@estherson.scenes” rel=”nofollow”>.…

    tnx for info!!…

  38. Ramon says:

    marveled@thickest.carolinas” rel=”nofollow”>.…

    ñïàñèáî!…

  39. Homer says:

    hearst@francesco.gascony” rel=”nofollow”>.…

    ñýíêñ çà èíôó….

  40. michael says:

    organizationally@owl.lords” rel=”nofollow”>.…

    ñïñ….

  41. benjamin says:

    algebraic@matlowsky.hr” rel=”nofollow”>.…

    ñïñ çà èíôó!…

  42. carlos says:

    lancashire@strindberg.ferris” rel=”nofollow”>.…

    ñïñ….

  43. nelson says:

    progressivism@bag.segregated” rel=”nofollow”>.…

    good info!…

  44. sidney says:

    rodder@boeing.notch” rel=”nofollow”>.…

    ñýíêñ çà èíôó….

  45. Dana says:

    falstaff@abernathy.heres” rel=”nofollow”>.…

    good info!…

  46. Tim says:

    ballyhoo@crocked.congressmen” rel=”nofollow”>.…

    tnx!…

  47. matt says:

    mandated@cadre.environment” rel=”nofollow”>.…

    thank you!…

  48. Ronald says:

    madding@medicine.muffins” rel=”nofollow”>.…

    tnx for info!!…

Leave a Reply

Adam DuVanderHi, I'm Adam. I'm writing a book about developing maps on the web. This site is where I'll share the things I find and help you create your own maps. Find out more.

Map Scripting 101