0

I am trying to get user current location and pass those latitude/longitude coordinates to a map. The map will be used as a website background image.

I was partially successful with this code:

  navigator.geolocation.getCurrentPosition(showPosition);

    function showPosition(position) {
        xss =position.coords.latitude + 
        "," + position.coords.longitude; 


  }
var position = [32.5590985,35.8415147];

Now I want the value of xss to be inside this variable:

var position = [32.5590985,35.8415147];

basically I was trying to get the values from the function but without success.

How should I go about getting a value from inside the function?

HTML

<div id="googlemaps"></div>

JS Code

    navigator.geolocation.getCurrentPosition(showPosition);

    function showPosition(position) {
        xss =position.coords.latitude + 
        "," + position.coords.longitude; 


  }
var position = [32.5590985,35.8415147];

function initialize() {

    var myOptions = {
        zoom: 17,
        streetViewControl: false,
        scaleControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('googlemaps'),
        myOptions);


    latLng = new google.maps.LatLng(position[0], position[1]);

    map.setCenter(latLng);

    marker = new google.maps.Marker({
        position: latLng,
        map: map,
        draggable: false,
        animation: google.maps.Animation.DROP
    });


}
google.maps.event.addDomListener(window, 'load', initialize);
2
  • yes but i want to get the xss as is it inside the position var @Igor Commented Jun 6, 2014 at 18:16
  • inside it it should be latitude and longitude of the user current position to show it on map, take another look at the question i added a code for testing @Igor Commented Jun 6, 2014 at 18:19

2 Answers 2

1

How about you set the value of position variable from inside the showPosition function?

var position;

navigator.geolocation.getCurrentPosition(showPosition);

function showPosition(pos) {
  position = [pos.coords.latitude, pos.coords.longitude];
}
Sign up to request clarification or add additional context in comments.

3 Comments

didn't work out i try to added an alert(position); outside the function and it gave me undefinded but inside the function it work good and that's keep the same problem please take a look at the question again i added a demo code
well, yeah , because getCurrentPosition is an asynchronous function and your alert statement gets executed before getCurrentPosition even finished :(
Worked Perfect like charm Thank You
0

Assuming navigator.geolocation.getCurrentPosition is asynchronous, this looks like a classic "Get results from an asynchronous request" problem. Please see https://stackoverflow.com/a/14220323/380487 for a detailed answer.

2 Comments

i was trying to do such a thing navigator.geolocation.getCurrentPosition(showPosition); function showPosition(position) { xss = "[" +position.coords.latitude + "," + position.coords.longitude +"]"; return xss; } var position = showPosition(position); but it didn't work also
Did you read the answer I linked to? If navigator.geolocation.getCurrentPosition is async, you will never be able to access values from inside the function in subsequent synchronous code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.