如何让geolocation返回到jquery中的变量?

如何让geolocation返回到jquery中的变量?,jquery,google-maps-api-3,geolocation,Jquery,Google Maps Api 3,Geolocation,下面是我正在编写的一些粗略代码。我现在要做的就是返回var geoOutput,这样我就可以存储它并在以后使用它。在本例中,它应提醒城市和州。如果我在success中发出警报,但在geolocation函数之外返回未定义,则我可以工作 $(function(){ var geoOutput; var geoGet; function getGeoLocation(geoType,displayType){ if(navigator.geolocation)

下面是我正在编写的一些粗略代码。我现在要做的就是返回var geoOutput,这样我就可以存储它并在以后使用它。在本例中,它应提醒城市和州。如果我在success中发出警报,但在geolocation函数之外返回未定义,则我可以工作

$(function(){
    var geoOutput;
    var geoGet;
    function getGeoLocation(geoType,displayType){
        if(navigator.geolocation){
                navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
            }
            else {
                $('.your-location').remove();
            }
        function geoSuccess(position) {
            console.log(position.coords.latitude+', '+position.coords.longitude);
            var geoCoder = new google.maps.Geocoder();
            var userLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
            geoCoder.geocode({'latLng':userLocation}, function(results,status){
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results) {
                        if(geoType == "zip") {
                            geoOutput = results[2].address_components[0].long_name;
                        }
                        if(geoType == "state") {
                            geoOutput = results[6].address_components[0].long_name;
                        }
                        if(geoType == "city") {
                            geoOutput = results[3].address_components[0].long_name;
                        }
                        if(geoType == "cityState") {
                            geoOutput = results[3].address_components[0].long_name+", "+results[6].address_components[0].long_name;
                        }
                        if(geoType == "coords") {
                            geoOutput = position.coords.latitude+","+position.coords.longitude;
                        }
                        if(geoType == "longitude") {
                            geoOutput = position.coords.longitude;
                        }
                        if(geoType == "latitude") {
                            geoOutput = position.coords.latitude;
                        }
                        if(displayType)
                            $('.your-location').text(geoOutput);
                        return geoOutput;
                    } else {
                        console.log('Google did not return any results.');
                        $('.your-location').remove();
                    }
                } else {
                    console.log("Reverse Geocoding failed due to: " + status);
                    $('.your-location').remove();
                }
            });
        }
        function geoError(err) {
             if (err.code == 1) {
                //console.log('The user denied the request for location information.');
                $('.your-location').text('State Not Found');
            } else if (err.code == 2) {
                //console.log('Your location information is unavailable.');
                $('.your-location').remove();
            } else if (err.code == 3) {
                //console.log('The request to get your location timed out.');
                $('.your-location').remove();
            } else {
                //console.log('An unknown error occurred while requesting your location.');
                $('.your-location').remove();
             }
        }
    }

    alert(getGeoLocation("cityState",false));
});

您还应该返回success函数本身。有关从嵌套函数返回的更多详细信息,请参见此答案: