70 likes | 97 Views
Geo Location. RIA. 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. Using DOM to Find Location.
E N D
Geo Location RIA
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
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
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; }
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+"'>";
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)
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