Html 5 geolocation api

ehtishamsiddiqui 335 views 12 slides May 28, 2018
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

HTML 5 Next Generation Websites by Muhammad Ehtisham Siddiqui (Follow me to complete course)


Slide Content

HTML 5 Geolocation API Muhammad Ehtisham Siddiqui Aptech Computer Education

What is Geolocation The HTML Geolocation API is used to locate a user's position . he HTML Geolocation API is used to get the geographical position of a user . Geolocation is most accurate for devices with GPS, like iPhone.

Browsers Support API Chrome Internet Explorer Firefox Safari Opera Geolocation 5.0 - 49.0 (http) 50.0 (https) 9.0 3.5 5.0 16.0

How to use API Check if Geolocation is supported If supported, run the getCurrentPosition () method. If not, display a message to the user If the getCurrentPosition () method is successful, it returns a coordinates object to the function specified in the parameter ( showPosition ) The showPosition () function outputs the Latitude and Longitude if  ( navigator.geolocation )  if ( navigator.geolocation ) {         navigator.geolocation.getCurrentPosition ( showPosition );   }  function  showPosition (position) {     x.innerHTML  = "Latitude: " + position.coords.latitude  +      "< br >Longitude: " + position.coords.longitude ;  }

Steps

Example Code

Finding Longitude & Latitude <!DOCTYPE html >< html >< body > <p>Click the button to get your coordinates.</p > < button onclick =" getLocation ()">Try It</button >< p id="demo"></p ><script> var x = document.getElementById ("demo "); function getLocation () { if ( navigator.geolocation ) { navigator.geolocation.getCurrentPosition ( showPosition );} else { x.innerHTML = "Geolocation is not supported by this browser .";}} function showPosition (position) { x.innerHTML = "Latitude: " + position.coords.latitude + "< br >Longitude: " + position.coords.longitude ;} </script ></ body > </ html>

Displaying the result in MAP <!DOCTYPE html >< html >< body><p id="demo">Click the button to get your position.</p><button onclick =" getLocation ()">Try It</button><div id=" mapholder "></div>< script> var x = document.getElementById ("demo "); function getLocation () { if ( navigator.geolocation ) { navigator.geolocation.getCurrentPosition ( showPosition , showError );} else { x.innerHTML = "Geolocation is not supported by this browser ."; }}function showPosition (position) { var latlon = position.coords.latitude + "," + position.coords.longitude ; var img_url = "https://maps.googleapis.com/maps/ api / staticmap?center =" + latlon +"&zoom=14&size=400x300&key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU "; document.getElementById (" mapholder "). innerHTML = "< img src ='"+ img_url +"'>";} }</ script></body></html>

OutPut

Video

Video