Skip to content

Instantly share code, notes, and snippets.

@cgkio
Created September 25, 2013 15:11
Show Gist options
  • Save cgkio/6701098 to your computer and use it in GitHub Desktop.
Save cgkio/6701098 to your computer and use it in GitHub Desktop.
Getting geographic locations with JavaScript in iOS webapps
// Get the current location
navigator.geolocation.getCurrentPosition(showMap);
// your callback function—the showMap function in this example—should take a position object as the parameter as follows:
function showMap(position) {
// Show a map centered at position
}
// use the coords instance variable of the passed-in position object to obtain the latitude and longitude coordinates as follows:
latitude = position.coords.latitude;
longitude = position.coords.longitude;
// tracking current location - if your web application displays the current location on a map, you can register for location changes and continually scroll the map as the current location changes. When you register for location changes, you receive a callback every time the location changes. The callbacks are continual until you unregister for location changes.
// Register for location changes
var watchId = navigator.geolocation.watchPosition(scrollMap);
function scrollMap(position) {
// Scroll the map to center position
}
// similar to “Getting the Current Location,” use the coords instance variable of the passed in position object to obtain the latitude and longitude coordinates.
// use the clearWatch method in the Geolocation class to unregister for location changes. For example, unregister when the user clicks a button or taps a finger on the map as follows:
function buttonClickHandler() {
// Unregister when the user clicks a button
navigator.geolocation.clearWatch(watchId);
}
// src: https://developer.apple.com/library/safari/documentation/AppleApplications/Reference/SafariWebContent/GettingGeographicalLocations/GettingGeographicalLocations.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment