7
RIA Geo Location

RIA Geo Location. Finding Location modern devices can get location coordinates phones and tablets can use GPS desktops and laptops –often cannot use GPS

Embed Size (px)

DESCRIPTION

Using DOM to Find Location navigator.geolocation.getCurrentPosition – asynchronous method that uses callbacks – getCurrentPosition(locFound, locError) – locFound is called if it is successful – locError is called if it is not successful handle errors if location is not supported

Citation preview

Page 1: RIA Geo Location. Finding Location modern devices can get location coordinates phones and tablets can use GPS desktops and laptops –often cannot use GPS

RIA

Geo Location

Page 2: RIA Geo Location. Finding Location modern devices can get location coordinates phones and tablets can use GPS desktops and laptops –often cannot use GPS

Finding Location

• modern devices can get location coordinates

• phones and tablets can use GPS• desktops and laptops

–often cannot use GPS –can get the location of the ISP–not as accurate but better than nothing

Page 3: RIA Geo Location. Finding Location modern devices can get location coordinates phones and tablets can use GPS desktops and laptops –often cannot use GPS

Using DOM to Find Location

navigator.geolocation.getCurrentPosition– asynchronous method that uses callbacks– getCurrentPosition(locFound, locError)– locFound is called if it is successful– locError is called if it is not successful

handle errors if location is not supported

Page 4: RIA Geo Location. Finding Location modern devices can get location coordinates phones and tablets can use GPS desktops and laptops –often cannot use GPS

Example

if (navigator.geolocation) {navigator.geolocation.getCurrentPosition(locOK,locErr);

} else {document.getElementById("loc").innerHTML=

"GEO location is NOT supported";}function locOK(currPos){

document.getElementById("lat").innerHTML="Latitude="+currPos.coords.latitude;

document.getElementById("lng").innerHTML="Longitude="+currPos.coords.longitude;

}function locErr(e){

document.getElementById("loc").innerHTML="Error getting current location: "+e.message;

}

Page 5: RIA Geo Location. Finding Location modern devices can get location coordinates phones and tablets can use GPS desktops and laptops –often cannot use GPS

Using Google Map API

assign the source of an img tag to the Google URL for making a map

just a static image so it does not allow for interaction

example:var latlon = position.coords.latitude + "," + position.coords.longitude;var img_url = "http://maps.googleapis.com/maps/api/staticmap?center="+latlon+"&zoom=14&size=400x300&sensor=false";document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";

Page 6: RIA Geo Location. Finding Location modern devices can get location coordinates phones and tablets can use GPS desktops and laptops –often cannot use GPS

Distance Calculation

there are sites that can help with calculating the distance between two points using latitude and longitude

for example: http://www.movable-type.co.uk/scripts/latlong.html

you may need to adjust the units (km vs meters)

Page 7: RIA Geo Location. Finding Location modern devices can get location coordinates phones and tablets can use GPS desktops and laptops –often cannot use GPS

Sorting Arrays of Objects

create a compare function like:function compare(x,y){ if(x.id < y.id) return -1; if(x.id > y.id) return 1; return 0;}

then in your code you can use:objs.sort(compare);

as long as each object in objs has an id field