谷歌地图API地理编码 - 返回GPS坐标



我有这个代码:

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();
});

您是否知道为什么该函数控制台正确的值但不返回任何内容?

对于 GMAP API,我使用此脚本:http://maps.google.com/maps/api/js?sensor=false&libraries=geometry

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

此外,geocoder.geocode将异步调用您的匿名函数。这意味着get_lat/get_lng没有机会掌握return值并将其return到调用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();
    }
});

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

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);
    }

你会在地图上得到标记

最新更新