tags:

views:

81

answers:

2

I need to access javascript variable:

var geocoder = new google.maps.Geocoder();

function geocodePosition(pos) {    
  geocoder.geocode({    
    latLng: pos    
  }, function(responses) {  
    if (responses && responses.length > 0) {  
      updateMarkerAddress(responses[0].formatted_address);  
    } else {  
      updateMarkerAddress('Cannot determine address at this location.');  
    }  
  });  
}  

function updateMarkerStatus(str) {  
  document.getElementById('markerStatus').innerHTML = str;  
}  

function updateMarkerPosition(latLng) {  
  document.getElementById('info').innerHTML = [  
    latLng.lat(),  
    latLng.lng()  
  ].join(', ');  
}  

function updateMarkerAddress(str) {  
  document.getElementById('address').innerHTML = str;  
} 



function initialize() {  

  geocoder.geocode( { 'address': 'london'}, function(results, status) {
   rr = results[0].geometry.location;
});

 var latLng = new google.maps.LatLng(rr);  

  var map = new google.maps.Map(document.getElementById('mapCanvas'), {  
    zoom: 8,  
    center: latLng,  
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });
  var marker = new google.maps.Marker({
    position: latLng,
    title: 'Point A',
    map: map,
    draggable: true
  });  

  // Update current position info.  
  updateMarkerPosition(latLng);  
  geocodePosition(latLng);  

  // Add dragging event listeners.  
  google.maps.event.addListener(marker, 'dragstart', function() {  
    updateMarkerAddress('Dragging...');  
  });  

  google.maps.event.addListener(marker, 'drag', function() {  
    updateMarkerStatus('Dragging...');    
    updateMarkerPosition(marker.getPosition());  
  });  

  google.maps.event.addListener(marker, 'dragend', function() {  
    updateMarkerStatus('Drag ended');  
    geocodePosition(marker.getPosition());  
  });  


// Onload handler to fire off the app.  
google.maps.event.addDomListener(window, 'load', initialize);    

This is whole script I need to get near address first so pass somehow address to convert to ltlng

+4  A: 

You have to access it or pass it somewhere in the callback, like this:

var rr;      
geocoder.geocode( { 'address': 'london'}, function(results, status) {
  rr = results[0].geometry.location;
  alert(rr);    
});

Or pass it to another function to do additional work with the data:

geocoder.geocode( { 'address': 'london'}, function(results, status) {
  anotherFunction(results[0].geometry.location);
});

That anonymous function is the callback for the geocode() function, meaning it runs later, so rr is getting set after your alert() currently.

Nick Craver
so when passed to anotoher function how can I retrieve variable inside first function?
miojamo
@user438755 - In the second example above, you would have another function, `function anotherFunction(loc) { alert(loc); }` for example, you can do whatever you want with that parameter, since that function will be called with it...when the server response comes back and the data is ready.
Nick Craver
but i need to access this variable in function where geocoder.geocode is just outside: var latLng = new google.maps.LatLng(rr);
miojamo
You have to understand that these functions run asynchronously, so you have to code to handle that; no other option.
jvenema
@user438755 - The data simply isn't available, it's an asynchronous operation, you need to run the rest of your code *when the data is available*, and that means from the callback.
Nick Craver
I've edited the post with whole script the idea is i need to pass address first for starting position and than adjust
miojamo
@user438755 - you'd simply call any update code inside you callback function, that includes the map updates, etc...I don't see you using a callback in the map at all though.
Nick Craver
yes i've added this inside initialize function [edited again] to get first coordinates from address so later can adjust it. Please tell me how to do it.
miojamo
@user438755 - You need to put all of that code in your callback, or at least trigger it from there, like this: http://jsfiddle.net/nick_craver/mPmAn/
Nick Craver
i've tried that already but map won't load than
miojamo
also tried to write it as cookie but don't work either cookie reading is ok however doesn't work var rr = readCookie('jeaddress').substr(1,22).toString(); alert(rr);
miojamo
A: 

The call is asynchronous meaning that the geocoder.geocode will return immediately and only inside the anonymous callback function you are able to access it.

geocoder.geocode( { 'address': 'london'}, function(results, status) {
   alert(results[0].geometry.location);
});
Darin Dimitrov
but I need to get result outside how to do it?
miojamo
You might need many things, the question here is that you cannot have them until they are available, which in your case is the anonymous callback function. So if you want to use AJAX you will have to adapt the way you think and structure your application in an event-oriented manner.
Darin Dimitrov