Search code examples
javascriptgeolocationreturn

Javascript Geolocation


I'm trying to do some things with geolocation. I want to get back the geolocation latitude and longitude in one variable as array. It doesn't work. I think the first line in the code is the problem. Is it possible to get the value like that (with return)?

startingPoint = navigator.geolocation.getCurrentPosition(onGeoSuccess);

function onGeoSuccess(position) {
    start['lat'] = position.coords.latitude;
    start['lng'] = position.coords.longitude;
    return start;
}

Is there a better solution? I don't want to execute other code in the onGeoSuccess, I want to return the value back.


Solution

  • You can try something like this:

    function geolocate() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          var geolocation = new google.maps.LatLng(
            position.coords.latitude, position.coords.longitude);
          var circle = new google.maps.Circle({
            center: geolocation,
            radius: position.coords.accuracy
          });
          autocomplete.setBounds(circle.getBounds());
        });
      }
    }