6. Geolocation API
• Compatibilidad:
if (Modernizr.geolocation) {
// esta al dia!, ver si quiere usarlo?
}
else
{
// salado el usuario , no tiene un mejor
browser..
}
7. Geolocation API
• Permiso se pide automático:
if (Modernizr.geolocation) {
// esta al dia!, ver si quiere usarlo?
navigator.geolocation.getCurrentPosition(show_map);
}
else
{
// salado el usuario, no tiene un mejor browser.. Reirse del mae.
}
Esto muestra una alerta en el browser, y el usuario DEBE dar permiso.
8. Geolocation API
• Si el mae no dio Permiso L:
if (Modernizr.geolocation) {
// esta al dia!, ver si quiere usarlo?
navigator.geolocation.getCurrentPosition( show_map, handle_error);
}
else
{
// salado el usuario , no tiene un mejor browser..
}
Ni modo hay que manejar errores.
9. Geolocation API
• Si el mae no dio Permiso L:
function handle_error(err)
{
if (err.code == 1)
{
// user said no!
}
if (err.code == 2)
{
// No se pudo ubicar por algun error de red u otro…
}
if (err.code == 3)
{
// el famoso TimeOut …
}
}
10. Geolocation API
• Si tenemos permiso y ubicación J:
function show_map(position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// let's show a map or do something interesting!
}
Ademas, se puede tener dependiendo:
Altitutud, velocidad, vista al norte, nivel de exactitud..
12. Como?
• Chrome uses Google Location Services.
• Firefox on Windows uses Google Location
Services.
• Firefox on Linux uses GPSD http://catb.org/gpsd/. I’m not sure if this
includes Android. I haven’t had a chance to
test it yet.
• Internet Explorer 9+ uses the Microsoft
Location Service.
• Safari on iOS uses
Apple Location Services for iPhone OS 3.2+.