Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does getCurrentPosition return?

This is what I have:

   var position;
  navigator.geolocation.getCurrentPosition(onSuccess, onError);

  function onSuccess(pos) {
position = { latitude: pos.coords.latitude, longitude: pos.coords.longitude} ;
//position = { latitude:43.465099,longitude:-80.520344};
  }

  function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }


  var myOptions = {
      center: new google.maps.LatLng(
        position.latitude,position.longitude
      ),
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      disableDefaultUI: true
  };

When I use the commented out line, the program works fine and it zooms to the hardcoded location. When I use the pos.coords line, nothing happens. Am I calling it wrong? Am I not able to just put it in my variable like that or does it return something else?

like image 470
VicVu Avatar asked Sep 01 '26 15:09

VicVu


1 Answers

You appear to be using the result properly but I would encode the JSON a bit differently:

position = { "latitude": pos.coords.latitude, 
             "longitude": pos.coords.longitude};
like image 195
Simon MacDonald Avatar answered Sep 03 '26 04:09

Simon MacDonald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!