dev1234
dev1234

Reputation: 5716

Google Map geocoder get long and lat

I am trying to get the long and lat of cities through a for loop.

Now i have done my coding but still due to geocoder being a asynchronous function something is not working (Map is not loading and markers are not shown). with the output of geocoder i am trying to display markers on the MAP.

function codeAddress(address, map, callback) {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var latLng = new google.maps.LatLng(results[0].geometry.location.lat().lat,results[0].geometry.location.lng());
                    var markerObj = new MarkerWithLabel({
                    position: latLng,
                    title:name,
                    labelContent: name,
                    labelClass:'marker-labels',
                    icon:markerImg
                });
        markerObj.setMap(map);
        console.log(latLng);
        return callback(map, latLng);
      } else {
        console.log('Geocode was not successful for the following reason: ' + status);
      }
    });
  }
    function initialize() {
        var mapOptions = {
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

    map = new google.maps.Map(document.getElementById('googleMap'),mapOptions);

    if ('' != markersAddress) {
    for (var x=0; x<markersAddress.length; x++) {
        var address = markersAddress[x].address;
        var name = markersAddress[x].name;
        codeAddress(address, map, function(map, latLng) {})
    }
    }
}

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

JS Fiddle http://jsfiddle.net/qe9soL4o/5/

Upvotes: 1

Views: 259

Answers (1)

Umair Ayub
Umair Ayub

Reputation: 21241

You are using MarkerWithLabel but you havent included it

Include both these files in head

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>

<script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerwithlabel/src/markerwithlabel.js"></script>

Note that order of these files is important

Upvotes: 3

Related Questions