user325558
user325558

Reputation: 1433

GeoLocation HTML5 getCurrentLocation callback

How to add a callback function to getCurrentLocation method passing coords or position as args to a global variable?. The object is allow to pass the coord to a global variable outside getCurrentLocation.

//Global scope
var Location= {coords:null;
} 

function getLocation(position)
{
  position.coords.latitude;
  Location.coords=coords.clone();
}

somewhere
navigator.geolocation.getCurrentLocation(getLocation, error)
Location.coords is NULL !

thanks.

Upvotes: 0

Views: 2605

Answers (5)

stimms
stimms

Reputation: 44064

You can read all about how to use the geoloction API at http://diveintohtml5.ep.io/geolocation.html which is a fantastic site. By default getCurrentLocation takes a callback as an argument. The callback receives a position object.

Upvotes: 0

ChristopheCVB
ChristopheCVB

Reputation: 7315

Shoudn't be :

getCurrentPosition

Upvotes: 1

Martin Algesten
Martin Algesten

Reputation: 13620

In your code this row:

Location.coords=coords.clone();

uses a variable coords that isn't declared. The parameter to the function with the coordinate you have called position. Is it a typo?

Upvotes: 0

Ian Devlin
Ian Devlin

Reputation: 18870

Finding your position with Geolocation | HTML5 Doctor should also help you out.

Upvotes: 0

Mark Holland
Mark Holland

Reputation: 846

navigator.geolocation.getCurrentLocation(function(pos){
  console.log("I'm located at ",pos.coords.latitude,' and ',pos.coords.longitude);
});

Suggest using Paul Irish's shim as a fallback for older browsers too: https://gist.github.com/366184

Upvotes: 0

Related Questions