Google maps 谷歌地图API地理代码-返回GPS坐标

Google maps 谷歌地图API地理代码-返回GPS坐标,google-maps,geocode,Google Maps,Geocode,我有这个密码: var get_lat = function(address) { geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { console.log('lat is: '+ results[0].geometry.location.lat());

我有这个密码:

var get_lat = function(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log('lat is: '+ results[0].geometry.location.lat());
            return results[0].geometry.location.lat();
        }
    });
}

var get_lng = function(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log('lng is: '+ results[0].geometry.location.lng());
            return results[0].geometry.location.lng();
        }
    });
}
在控制台中,它打印我需要的坐标,但返回值始终未定义:

我在谷歌地图的经典初始化中使用它,如下所示:

function createMarker(latlng) {
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
    });
}

function initialize() {
    var myOptions = {
        zoom: 8,
        center: new google.maps.LatLng(49.210366,15.989588)
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var address = "Correct address that console.logs the correct GPS coordinations in function definitions above";
    var gps_lat = get_lat(address);
    var gps_lng = get_lng(address);
    console.log('GPS lat: ' + gps_lat); // logs "undefined"
    console.log('GPS lng: ' + gps_lng); // logs "undefined"

    var marker = createMarker({lat: gps_lat}, lng: gps_lng});
}


$(window).on("load", function (e) {  
    initialize();
});
你知道为什么这个函数的值是正确的,但它不返回任何东西吗

对于GMAPAPI,我使用以下脚本:

您在一个匿名函数中有一个返回,该函数传递给geocoder.geocode,所有这些都在另一个函数get\u lat/get\u lng中。get_lat/get_lng本身没有return语句,因此返回undefined

此外,geocoder.geocode将异步调用匿名函数。这意味着get_lat/get_lng没有机会获得返回值并将其返回到调用get_lat/get_lng的位置

最简单的一种解决方案是将createMarker代码放入geocoder.geocode的回调中。此外,在这种情况下,您必须合并两个get_lat/get_lng函数。例如:

geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var gps_lat = results[0].geometry.location.lat()
        var gps_lng = results[0].geometry.location.lng();
        console.log('lat is ' + gps_lat + '. lng is ' + gps_lng);
        var marker = createMarker({lat: gps_lat, lng: gps_lng});
        return results[0].geometry.location.lat();
    }
});

您必须等到谷歌API给出响应。所以,编写一个逻辑,在GoogleAPI的回调函数中创建标记,如下所示

var getLatLng = function (address)
    {
        geocoder.geocode({ 'address': address }, function (results, status)
        {                
            if (status == google.maps.GeocoderStatus.OK)
            {
                var location = results[0].geometry.location;

                // Create marker once you get response from Google
                createMarker({ lat: location.lat(), lng: location.lng() });
            }
        });
    }
并调用该函数,如下所示

function initialize()
    {
        var myOptions =
        {
            zoom: 8,
            center: new google.maps.LatLng(49.210366, 15.989588)
        }

        map = new google.maps.Map(document.getElementById("map"), myOptions);
        var address = "Correct address that console.logs the correct GPS coordinations in function definitions above";            

        // Old code ------------------------------------------------
        //var gps_lat = get_lat(address);
        //var gps_lng = get_lng(address);

        //console.log('GPS lat: ' + gps_lat); // logs "undefined"
        //console.log('GPS lng: ' + gps_lng); // logs "undefined"

        //var marker = createMarker({ lat: gps_lat, lng: gps_lng });
        // ----------------------------------------------------------

        // New code
        getLatLng(address);
    }
你会在地图上找到标记