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:

15 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!!…

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