Results 1 to 1 of 1
  1. #1
    colonelx
    colonelx is offline
    Guest
    Join Date
    2014 Aug
    Posts
    1
    Thanks Thanks Given 
    0
    Thanks Thanks Received 
    0
    Thanked in
    0 Posts
    Rep Power
    0

    Geolocation using JS+HTML5

    hi
    my post is about using some html5 Technique

    html5 provide us alot of abilities one of it is Geolocation
    which allow us to get user location longitude and latitude
    using client side programming with some help of javascript
    Please notice that your browser will prompt you before allowing
    trusted sites to run this API.
    Privacy is always a concern, so you can't force anyone to use this API.

    to start using it first
    let's create some html code to create button to call JS function

    HTML Code:
    <p id="textgeo">Where are you in the world?</p>
    	<button onclick="getLocation()">Get Coordinates</button>
    in getLocation() function first we will check if the browser support geolocation using

    Code:
     if (navigator.geolocation)
    so if not exists it will return false so we tell user that his browser does not support geolocation

    so the code will be

    Code:
                  if (navigator.geolocation)
    	      {
    	          //do some stuff
    	      }
    	    else
    	     { 
    		  document.getElementById("textgeo").innerHTML="Geolocation is not supported by this browser.";
    	      }
    the the geolocation has a method called

    Code:
    getCurrentPosition(callback)
    this method automatically get current location then execute
    the callback function with parameter we can call it 'position'
    the parameter that the function sent it contains coordinates
    of user current location

    so the full code will be like this

    HTML Code:
    	<script>
    
    	  function getLocation()
    	    {
    	    if (navigator.geolocation)
    	      {
    	      navigator.geolocation.getCurrentPosition(showPosition);
    	      }
    	    else
    		  { 
    		  document.getElementById("textgeo").innerHTML="Geolocation is not supported by this browser.";
    		  }
    	    }
    	    
    	  function showPosition(position)
    	    {
    	    document.getElementById("textgeo").innerHTML="Latitude: " + position.coords.latitude +
    	    "<br>Longitude: " + position.coords.longitude;
    	    }
    
    	</script>
    also u can use Google Map API to show the map all u need is to edit showPosition function to use google map api
    i can't post links cuz it's my first post so i can't give u the full link of that ai but u can google it and find more information about that
    thats all i hope it could help someone

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •