0
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript">
    var geocoder;
    var loc;
    geocoder = new google.maps.Geocoder();
    var input = "32.83036,34.974339"
    var latlngStr = input.split(",",2);
    var lat = parseFloat(latlngStr[0]);
    var lng = parseFloat(latlngStr[1]);
    var latlng = new google.maps.LatLng(lat, lng);

    geocoder.geocode({'latLng': latlng}, function(results, status) {
        document.write(results[1].formatted_address);
    });


</script>

It doesn't do anything. If I'll replace the "document.write" with "alert" it will work. What am I doing wrong?

Yuval
  • 1

2 Answers2

0

document.write is evil; if you're using an XHTML DTD it doesn't even work. For more reasons on why not to use document.write: Why is document.write considered a "bad practice"?

Community
  • 1
  • 1
Bjorn
  • 4,822
  • 1
  • 18
  • 34
  • it doesn't metter. it's just an example, in the actual code i need to put that data in a variable and use it outside this function. my point is, that for some reason i can only use that data inside this function. – Yuval Jun 27 '11 at 08:58
  • If you're storing this in a var and use it later on, make sure the function (callback) has fired. For example, trigger the function which uses the variable from within the callback function. – Bjorn Jun 27 '11 at 08:59
  • my javascript skills are very poor. can you please show me an example? – Yuval Jun 27 '11 at 09:02
  • To get a grasp of the concept of callbacks/closures I'd recommend reading this: http://jibbering.com/faq/notes/closures/ . Here's a little example: http://pastie.org/2128127 – Bjorn Jun 27 '11 at 09:09
  • what is "console.log" ? again. i'm not good in jp. can you show another example? – Yuval Jun 27 '11 at 09:21
0

Couldn't you just use something like;

document.getElementById('ObjectName').innerText = results[1].formatted_address;

Also results[0].formatted_address (zero not one) will give you a more detailed street name.

Jim