Returning values from Callback

前端 未结 4 440
一整个雨季
一整个雨季 2021-01-28 16:24

I don\'t have any previous javascript experience.

I\'m trying to implement the following function which I wish to use to return the values lat and lng:

f         


        
4条回答
  •  误落风尘
    2021-01-28 17:10

    you could use global variables or return the values in an array for instance:

    global variables method:

    lat = "";
    lng = "";
    
    function get_address() {
        var geocoder = new google.maps.Geocoder()
    
        geocoder.geocode({ address: "SE-17270 Sverige"}, 
        function(locResult) {
    
        lat = locResult[0].geometry.location.lat();
        lng = locResult[0].geometry.location.lng();
        alert(lat);
        alert(lng);
        })
    }
    

    array method:

    function get_address() {
        var geocoder = new google.maps.Geocoder()
    
        geocoder.geocode({ address: "SE-17270 Sverige"}, 
        function(locResult) {
    
        var lat = locResult[0].geometry.location.lat();
        var lng = locResult[0].geometry.location.lng();
    
        thearray = [];
        thearray.push(lat);
        thearray.push(lng);
        return thearray;
        })
    }
    

提交回复
热议问题