Toon Van Dooren
Toon Van Dooren

Reputation: 613

Google Geocoder + google maps adding marker

What i am trying to do is adding a marker when the geocoder is done parsing to latlng... This is my code:

function parseLocation() {
    var user1Location = "Mechelen, Belgium";
    var geocoder = new google.maps.Geocoder();
    //convert location into longitude and latitude
    geocoder.geocode({
        address: user1Location
    }, function (locResult) {
        lat1 = locResult[0].geometry.location.lat();
        lng1 = locResult[0].geometry.location.lng();
        if (status == google.maps.GeocoderStatus.OK) {
            $('#map_canvas').bind('init', function () {
                $('#map_canvas').gmap('addMarker', {
                    'position': lat1 + ',' + lng1
                }).click(function () {
                    $('#map_canvas').gmap('openInfoWindow', {
                        'content': lat1 + ',' + lng1
                    }, this);
                });
            });
        }
    });
}

When i alert the variables in the init function they are undefined, i know this geocoder is async but I am waiting for the status to be ok... Any idea what ive done wrong?

Upvotes: 0

Views: 442

Answers (1)

turankonan
turankonan

Reputation: 1009

Try it. Geocoder is async but multiple times call callback function.

geocoder.geocode({"address":user1Location}, function(locResult, status) {
     if (status == google.maps.GeocoderStatus.OK) {
        lat1 = locResult[0].geometry.location.lat();
        lng1 = locResult[0].geometry.location.lng();
     }
});

Thats working on link in the same code.

http://jsfiddle.net/2jbTX/2/

Upvotes: 2

Related Questions