user2637015
user2637015

Reputation: 723

Googlemap v3 reverse geocoding

using googlemap v3 reverse geocoding samplesource make this source

var map;
        var geocoder;
        var marker;
        function initialize() {
            geocoder = new google.maps.Geocoder();
            var mapOptions = {
                zoom : 14,
                center : new google.maps.LatLng(30, 30)
            };
            map = new google.maps.Map(document.getElementById('map-canvas'),
                    mapOptions);
        }

        function codeLatLng() {
              var latlng = new google.maps.LatLng(30, 30);
              alert("call codeLatLng() 1");
              geocoder.geocode({'latLng': latlng}, function(results, status) {
                  alert("call codeLatLng() 2");
                if (status == google.maps.GeocoderStatus.OK) {
                  if (results[1]) {
                    map.setZoom(11);
                    marker = new google.maps.Marker({
                        position: latlng,
                        map: map
                    });
                    infowindow.setContent(results[1].formatted_address);
                    infowindow.open(map, marker);
                  } else {
                    alert('No results found');
                  }
                } else {
                  alert('Geocoder failed due to: ' + status);
                }
              });
            }


        google.maps.event.addDomListener(window, 'load', initialize);
        codeLatLng();

i call function codeLatLng(); last line on code

so call function codeLatLng() and alert message "call codeLatLng() 1

but doesn't call "call codeLatLng() 2" and code doesn't work

what is wrong in my code?

Upvotes: 0

Views: 140

Answers (1)

geocodezip
geocodezip

Reputation: 161384

what is wrong in my code?

Your are executing codeLatLng before your map and the geocoder variable are initialized (initialize runs when the DOM has finished loading, codeLatLng runs immediately).

This would work better:

    var map;
    var geocoder;
    var marker;
    function initialize() {
        geocoder = new google.maps.Geocoder();
        var mapOptions = {
            zoom : 14,
            center : new google.maps.LatLng(30, 30)
        };
        map = new google.maps.Map(document.getElementById('map-canvas'),
                mapOptions);

        // map and geocoder initialized
        codeLatLng();
    }

    function codeLatLng() {
          var latlng = new google.maps.LatLng(30, 30);
          alert("call codeLatLng() 1");
          geocoder.geocode({'latLng': latlng}, function(results, status) {
              alert("call codeLatLng() 2");
            if (status == google.maps.GeocoderStatus.OK) {
              if (results[1]) {
                map.setZoom(11);
                marker = new google.maps.Marker({
                    position: latlng,
                    map: map
                });
                infowindow.setContent(results[1].formatted_address);
                infowindow.open(map, marker);
              } else {
                alert('No results found');
              }
            } else {
              alert('Geocoder failed due to: ' + status);
            }
          });
        }


    google.maps.event.addDomListener(window, 'load', initialize);

Upvotes: 2

Related Questions