advertise on nct forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting Current Location With Javascript
#1
I originally thought grabbing a user's current location was going to be difficult, but it's actually very simple.
There's quick way on w3schools that shows us how to use HTML5

Geolocation API:

http://www.w3schools.com/html/html5_geolocation.asp

You also have to include the Google Maps API:

http://maps.googleapis.com/maps/api/js?sensor=false

Then you write your function to grab the current location:


Code:
function getLocation() {
    if(navigator.geolocation {
navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
        } else {
            alert("Geolocation is not supported by this browser.");
        }
    }

You'll also need to write your success function:


Code:
function geoSuccess(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    alert("lat:" + lat + " lng:" + lng);
}


And then an error function:


Code:
function geoError() {
    alert("Geocoder failed.");
}


You can stop here if you just want the lat and lng coordinates.
But if you want to convert these coordinates to a full address, you can call another function that passes your lat and lng:
Add a function in your success function:


Code:
function geoSuccess(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    alert("lat:" + lat + " lng:" + lng);
    codeLatLng(lat, lng);
}

And here's the code for that function:


Code:
var geocoder;
function initialize() {
    geocoder = new google.maps.Geocoder();
}
function codeLatLng(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
          console.log(results)
          if(results[1]) {
              //formatted address
              var address = results[0].formatted_address;
              alert("address = " + address);
          } else {
              alert("No results found");
          }
      } else {
          alert("Geocoder failed due to: " + status);
      }
    });
}


Answer found from:

http://stackoverflow.com/questions/67975...-city-name
It takes so long to learn simplicity.


Possibly Related Threads...
Thread Author Replies Views Last Post
  SHARED: Introducing Hrouter.js Javascript Routing Library Horlahcoded 7 84 10-16-2017, 10:13 PM
Last Post: iamdannyfc
  SHARED: Introducing Hkwest.js Javascript Ajax Library Horlahcoded 0 37 10-15-2017, 09:46 AM
Last Post: Horlahcoded
  HELP: Javascript Vs Jquery. beme 10 296 08-21-2017, 07:20 AM
Last Post: beme
  Test Your Javascript Knowledge Dealwap 10 563 08-16-2017, 07:41 PM
Last Post: Thepreditor
  How Can I Submit A Form Using Javascript iamdannyfc 11 235 06-22-2017, 10:13 PM
Last Post: bfaschat
  TUTORIAL: Take A Selfie With Javascript SamBlingxZ 0 82 06-05-2017, 11:41 PM
Last Post: SamBlingxZ
  3 Types Of Javascript Animations That I So Much Admire! Laive 5 213 05-20-2017, 09:40 AM
Last Post: Laive
  HELP: How Can I Create A Javascript Ads Rotator For My Site Eddykul 5 219 03-30-2017, 09:51 AM
Last Post: Harsh
  Beginner Javascript Coding Mistakes To Avoid smartlord 2 258 02-08-2017, 04:04 PM
Last Post: smartlord
  Concatenate Many Arrays Uniquely With Javascript Dealwap 0 207 01-03-2017, 04:15 PM
Last Post: Dealwap



Users browsing this thread: 1 Guest(s)

About Nigeria's Creative Talents

Nigeria's Creative Talents is an online community where every creative Nigerians can share ideas with themselves.

For any more information, please use our contact form.

              User Links

              Advertise