Javascript 如何从lat和lng获取地址?

Javascript 如何从lat和lng获取地址?,javascript,google-maps,Javascript,Google Maps,我有两套lat和lng 我希望地址和地址都存储在某个变量中: var geocoder = new google.maps.Geocoder(); for(var i=0; i<json_devices.length; i++) { var lat = json_devices[i].latitude; var lng = json_devices[i].longitude; conso

我有两套lat和lng

我希望地址和地址都存储在某个变量中:

var geocoder = new google.maps.Geocoder();
        for(var i=0; i<json_devices.length; i++)
        {
            var lat = json_devices[i].latitude;
            var lng = json_devices[i].longitude;
            console.log(lat);
            console.log(lng);
            var latlng =  new google.maps.LatLng(lat,lng);
            geocoder.geocode({'latLng': latlng}, function(results, status) {
             if (status == google.maps.GeocoderStatus.OK) {
              if (results[1]) {
                address=results[1].formatted_address;
                   } else {
                    alert('No results found');
                  }
                } else {
                  alert('Geocoder failed due to: ' + status);
                }
            });
            console.log(address);
        }
var geocoder=new google.maps.geocoder();

对于(var i=0;i我正在使用这个方法,它非常适合我。 请看一下

public String getAddressFromLatLong(GeoPoint point) { 
        String address = "Address Not Found";
        Geocoder geoCoder = new Geocoder(
                getBaseContext(), Locale.getDefault());
        try {
            List<Address> addresses = geoCoder.getFromLocation(
                    point.getLatitudeE6()  / 1E6, 
                    point.getLongitudeE6() / 1E6, 1);

            if (addresses.size() > 0) {
                            address =addresses.get(0).getAddressLine(0);
                if(address.length()<=0)
                address =addresses.get(0).getSubLocality();
                 }
        }
        catch (Exception e) {                
            e.printStackTrace();
             }   
        return address;
    }
公共字符串getAddressFromLatLong(地质点){
String address=“未找到地址”;
地理编码器地理编码器=新地理编码器(
getBaseContext(),Locale.getDefault();
试一试{
列表地址=geoCoder.getFromLocation(
point.getLatitudeE6()/1E6,
point.getLongitudeE6()/1E6,1);
如果(地址.size()>0){
地址=地址.get(0).getAddressLine(0);

if(address.length()这里的谷歌地理编码是异步的函数调用类型

发件人:

访问地理编码服务是异步的,因为谷歌地图 API需要调用外部服务器 需要传递回调方法,以便在完成 此回调方法处理结果。请注意 地理编码器可能返回多个结果

因此,您不能像那样获取地址,而是使用名为
回调的常用方法

这里我创建了一个示例代码来解释这个过程,您可以自己修改

var geocoder;

function codeLatLng(callback) {
    geocoder = new google.maps.Geocoder();
    var input = document.getElementById("latlng").value;
    var latlngStr = input.split(",", 2);
    var lat = parseFloat(latlngStr[0]);
    var lng = parseFloat(latlngStr[1]);
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({
        'latLng': latlng
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                address = results[1].formatted_address;
                callback(address);
            } else {
                alert("No results found");
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });
}

$('input[type="button"]').on('click', function () {
    codeLatLng(function (address) { //function call with a callback
        console.log(address);  // THE ADDRESS WILL BE OBTAINED
    })
});

你能创建JSFIDLE吗?@VinceLowe这里有粗略的数据,这里重现了OP的问题。