dallen
dallen

Reputation: 2651

HTML5 Geolocation - Detect location within a specific area

I haven't had much of an opportunity to look at HTML5 Geolocation yet, but I'm curious: Is it possible to build a web app that can detect when a user enters a certain area (perimeter) and then return a message or something like that?

Upvotes: 0

Views: 1017

Answers (1)

Paul V
Paul V

Reputation: 1292

You can use watchPosition to get periodic updates of the browser's location, and then in your callback, test to see if the new position is within your area of interest. So if you've defined a function isInArea that checks a position to see if it's in your area of interest, you could do something like:

function positionCallback(position) {
  if (isInArea(position)) {
    alert("Honey, I'm home!");
  }
}

function handleError(error) {
  alert("Error!")
}

// Request repeated updates.
var watchId = navigator.geolocation.watchPosition(positionCallback, handleError);

Based on Example of requesting repeated position updates from w3c.

Upvotes: 2

Related Questions