0

My problem is this .I am new to javascript. I have a function which makes an asynchronous call to a Google map API (which returns location based on latlng) .This function in my code is MarkerCreaterPlusLocation. This call returns a value which I need in another function which is named MarkerCreater in my code. But the problem is that MarkerCreater does not stop for the MarkerCreaterPlusLocation to return the value .

To overcome this problem I tried using the callback for the MarkerCreater to get executed when the asynchronous function returns the value

The structure is as follows :

google.maps.event.addListener(map, 'click',addLatLng); //This code attaches the function to Listener



function addLatLng(event) {
        path = poly.getPath();
        path.push(event.latLng);
        MarkerCreaterPlusLocation(event.latLng,MarkerCreater);//MarkerCreater is the callback function
}




function MarkerCreaterPlusLocation(input,callback){
    location="l";
    geocoder.geocode({'latLng': input}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (results[1]) {
        location=results[1].formatted_address;
        callback(location,input);//I call the callback function upon the success of the result or otherwise
      } else {
        location="l"; 
        callback(location,input);
      }
    } else {
      location="l";
      callback(location,input);
    }
    });
}




function MarkerCreater(l,x){

    var marker = new google.maps.Marker({
        position: x,
        title: '#' + path.getLength()+l,
        icon: 'images/beachflag.png',
        map: map
});
    ///Some more javascript code 
}

I guess I am making mistake here as this does not seem to work . Rather it gives a 404 error which makes it still more difficult for me to understand it . Please help

2
  • You have a syntax error ... but maybe it's just a copy/paste issue. There is an additional "s" after location="l" in MarkerCreaterPlusLocation() Commented Nov 6, 2013 at 14:35
  • Thanks for pointing the error . I have corrected it . Yeah it was a copy paste error Commented Nov 6, 2013 at 14:37

1 Answer 1

3

Your location variable is not declared with var, which means it's in the global scope (i.e. window). So, setting location is actually setting window.location, which is causing the redirect to a 404.

To fix this, change the first line of your MarkerCreaterPlusLocation function to:

var location="l";

This will create it in the scope of the function only, and not the window.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks !! s was copy-paste error . The error was with the declaration of location variable .
Great. Always a good idea to declare your variables with var to avoid problems like this, unless you really want to create a global variable.
FYI - That doesn't seem to work in Chrome. Better not to use "location" as a variable.
Works in latest Chrome/Mac for me, when the var declaration is inside a function. But you're right, better to not use it at all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.