Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a problem with geocoder.Geocode function. When i put it into a loop like in below method, it can't even be debugged into the geocoder.Geocode even if i have placed a debugger in side the function. what happens there is the loop just continues without even calling that geocoder.Geocode()method (as i think) and after loop ends then only debugger comes into the geocode method. what i want to do is geocode a address and get lat and long from that. and there are multiple addresses i want to loop through. Here is a part of my code... once i saw that there are options given for this type of questions with call back functions like calling geocode function from inside a separate method and call that method in a loop. i did that too. still the same happens. the code i have provided here is not that one.

JavaScript
function drawMarkers(markerList) {
            debugger;

            var mapOptions = {
                center: new google.maps.LatLng(markerList[0].Latitude, markerList[0].Longitude),
                zoom: 8,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var currentLocation;
            var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
            var infoWindow = new google.maps.InfoWindow();


            var lat_lng = new Array();
            var latlngbounds = new google.maps.LatLngBounds();
            for (i = 0; i < markerList.length; i++) {
                var data = markerList[i]


                var address = data.DeliveryAddress;


                var myLatlng = new google.maps.LatLng(data.Latitude, data.Longitude);
                lat_lng.push(myLatlng);


                var marker = new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title: data.DriverName,

                });
                var geocoder = new google.maps.Geocoder();
                if (geocoder) {
                    geocoder.geocode({ 'address': address }, function (results, status) {
                        debugger;
                        if (status == google.maps.GeocoderStatus.OK) {
                            var latitude = results[0].geometry.location.lat();
                            var longitude = results[0].geometry.location.lng();


                                }

                            });
                        }


                    }

                    map.setCenter(latlngbounds.getCenter());
                    map.fitBounds(latlngbounds);


                }
Posted
Updated 26-Mar-15 19:27pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900