1

I am curently working with google maps and I have a method that creates markers on google maps. The method gives the info for the markers. one part of this info is the longtitude and lattitude for the marker. The longtitude and lattitude are retreived from a zip code via a function in javascript. however when I return the longtitude and Lattitude it says it contains nothing.

It gets multiple zip codes and should return multiple longtitudes and lattitudes

My code:

This adds the markers.

void AddMarkerTimer(object sender, EventArgs e)
        {

            try
            {
                this.Document.InvokeScript("addMarker", new string[] { this.Document.InvokeScript("getLatFromPost", new string[] { delictMarkers[MarkerIndex].PostalCode }) + "", this.Document.InvokeScript("getLonFromPost", new string[] { delictMarkers[MarkerIndex].PostalCode }) + "", "marker" + MarkerIndex, MarkerIndex + "" });
                MarkerIndex++;
            }
            catch(ArgumentOutOfRangeException ax){
                MessageBox.Show(ax.ToString());
            }

            if (MarkerIndex == delictMarkers.Count - 1)
            {
                MarkerTimer.Stop();
            }
        }

This should return a longtitude and lattitude (javascript).

 function getLatFromPost(post) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({ address: post },
                function (results_array, status) {
                    var s;
                    s = results_array[0].geometry.location.lat();
                    //alert(s);
                    // Check status and do whatever you want with what you get back
                    // in the results_array variable if it is OK.
                    return s;
                });
        }
        function getLonFromPost(post) {
            var geocoder = new google.maps.Geocoder();

                geocoder.geocode({ address: post },
                    function (results_array, status) {
                        var s;
                        s = results_array[0].geometry.location.lng();
                        alert(s);
                        // Check status and do whatever you want with what you get back
                        // in the results_array variable if it is OK.
                         return s;
                    });

            }

And this part adds it on the map(javascript).

function addMarker(x, y, titleMarker, index) {
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(x, y),
                map: map,
                title: titleMarker,
                icon: delict,
                index: index
            });
            google.maps.event.addListener(marker, "click", function (event) {
                showMarkerContextMenu(event.latLng, index, titleMarker);
            });
            marker.set("index", index);
            markerList[index] = marker;
            //marker.setMap(map);
        }

I hope it is clear what my problem is. Thank you in advance for your attention.

Arnout
  • 157
  • 1
  • 13
  • it seems something works asynchronously, so you can look at this post http://stackoverflow.com/questions/2993563/how-do-i-return-a-variable-from-google-maps-javascript-geocoder-callback – AOZ Nov 25 '13 at 22:26

1 Answers1

0

geocoder.geocode works asynchronously. Meaning that you pass the parameters and a callback function. When the answer is ready, the callback function will be called. So basically it doesn't make sense for your callback function to return a value. Because even if the value were to be returned, it would be returned to the caller which is google in this case.

kaptan
  • 3,060
  • 5
  • 34
  • 46