0
<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Geocoding service</title>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
        <script>
            function initialize() {

                geocoder = new google.maps.Geocoder();

                address = "toronto, canada";

                geocoder.geocode({
                    'address': address
                }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var lat = results[0].geometry.location.lat();
                        var lng = results[0].geometry.location.lng();
                    } else {
                        var lat = 123;
                        var lng = 456;
                    }
                    document.getElementById('my_place1').value = lat + " - " + lng;
                });

                document.getElementById('my_place').value = lat + " - " + lng;

            }
        </script>
    </head>

    <body onLoad="initialize()">
        <input name="" id="my_place" style="width:300px; margin-left:20px;" type="text">
        <br>
        <input name="" id="my_place1" style="width:300px; margin-left:20px;" type="text">
    </body>
</html>

In the above code for my_place1 value is working but the same value not working with my_place. Can anybody help to solve this issue? I want to work something outside geocoder.geocode with lat and lng values.

2
  • 4
    The operation is asynchronous. Commented Feb 24, 2014 at 15:10
  • Not only that the variables lat and lng are out of scope. Commented Feb 24, 2014 at 15:14

2 Answers 2

1

Your document.getElementById('my_place').value = lat + " - " + lng ; is executed before lat and lang are made, which is concept of asynchronous.

When you want to use that outside of geocoder.geocode(, then you will have to invoke the callback function updatdateMyPlace() after made lat and lang

UPDATE

 function initialize() {
      geocoder = new google.maps.Geocoder();
      address = "toronto, canada";
      geocoder.geocode({ 'address': address}, function(results, status) {

      if (status == google.maps.GeocoderStatus.OK) {
        var lat = results[0].geometry.location.lat();
        var lng = results[0].geometry.location.lng();
        }
      else {
         var lat = 123;
          var lng = 456;
       }
        document.getElementById('my_place1').value = lat + " - " + lng  ; 
        updatdateMyPlace(lat, lng);
     });

     function updatdateMyPlace(lat, lng){
        document.getElementById('my_place').value = lat + " - " + lng ; 
     }
 }
Sign up to request clarification or add additional context in comments.

4 Comments

yea. but i want lat and lng values out side geocoder.geocode(). that is the issue here
for that you will have invoke callback function after made the lat and lng values ar made
you are correct. But the real issue is i want to do something with lat, lng value outside geocoder.geoode(). i want to send this lat, lng values to ouside geocoder.geocode()
updatdateMyPlace() is outside geocoder.geocode() function So lat and lang are. please have a look answer.
0

You are locally scoping these values, which means you can only access them from within the same block.

if (status == google.maps.GeocoderStatus.OK) {

    var lat = results[0].geometry.location.lat();
    var lng = results[0].geometry.location.lng();

    document.getElementById('my_place1').value = lat + " - " + lng;

}  // <--- once you exit this block, the lat/lng no longer exist

1 Comment

to use lat and lng values outside the block means, how can i paas them to outside.

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.