www.webdeveloper.com
Results 1 to 5 of 5

Thread: Prblem with variables

Hybrid View

  1. #1
    Join Date
    Apr 2010
    Location
    Portugal
    Posts
    3

    Prblem with variables

    Sorry guys I'm a rookie on JS and I have the problem explained below: how do I pass the values of vars "lat1" and "long1" to outside the function and assign them to wp[0] as shown?

    (No problem with wp[1] that I get from another file).

    Thanks,

    Miguel

    Code:
    <script type="text/javascript">
    	
    	navigator.geolocation.getCurrentPosition(
     	function (pos){
            lat1 = pos.coords.longitude;
    	long1 = pos.coords.latitude;
    });
    	var lat2 ="<?php echo $_SESSION['lat']; ?>";
    	var long2 ="<?php echo $_SESSION['long']; ?>";	
    	
             var wp = new Array(2);
    	 wp[0] = lat1 + "," +long1;
    	wp[1] = lat2 + "," +long2;
    	
    	inicio = (wp[0]);
    	destino = (wp[1]);

  2. #2
    Join Date
    Oct 2009
    Posts
    46

    make global variables

    You probably just need to define lat1 and long1 as global variables. Then you can assign their values from within the geo function, and still retrieve the values to populate your array.

    Try declaring the vars directly after your opening script tag to make them global:

    Code:
      <script type="text/javascript">
        var lat1;
        var long1;
        navigator.geolocation.getCurrentPosition(
    // ... resume your code

  3. #3
    Join Date
    Apr 2010
    Location
    Portugal
    Posts
    3

    Not working

    That solution you gave to me I tried before but no value passes to the variable.

    If I try to echo it outside the function like:

    document.write(lat1);

    the output I get is "undifined"; although I get the correct value if I put the same statement inside the function


  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    are you sure that getCurrentPosition() has completed and called the callback by the time you look at lat1?

    it looks like you check lat1 immediately, quite possibly before the call back sets the values you are looking for.

    that's why it works in the callback, where it waits for the value.

    whatever you wanted to do with wp[], do it in the callback.

  5. #5
    Join Date
    Apr 2010
    Location
    Portugal
    Posts
    3
    Yes. I think that may be the problem...

    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles