在appcelerator中使用纬度和经度获取城市名称,地址

get city name, address using latitude and longitude in appcelerator

我对上述标题有疑问。我可以获得当前的纬度和经度。但是当我使用反向地理编码转换为相应的城市名称或地址时,没有任何显示。有人对此有任何想法吗?这是我的代码

 Titanium.Geolocation.getCurrentPosition(function(e) {

            if (!e.success || e.error) {
                alert('Could not find the device location');
                return;
            } else {
                longitude = e.coords.longitude;
                latitude = e.coords.latitude;

                Titanium.Geolocation.reverseGeocoder(latitude, longitude, function(e) {
                    if (e.success) {
                        var places = e.places;
                        if (places && places.length) {
                            driverCity = places[0].city;
                            // Current city
                            driverState = places[0].address;
                            // Current State
                            annotation.title = e.places[0].displayAddress;
                            // Whole address
                            // Ti.API.info("\nReverse Geocode address == " + JSON.stringify(places));
                        } else {
                            // address = "No address found";
                        }
                    }
                });
            }
        });

我建议首先在函数中为您的 return 参数使用不同的变量,并使用否定条件来减少缩进:

Titanium.Geolocation.getCurrentPosition(function(position) {

    if (!position.success || position.error) {
        alert('Could not find the device location');
        return;
    }
    longitude = position.coords.longitude;  //  -88.0747875
    latitude = position.coords.latitude;    //  41.801141

    Titanium.Geolocation.reverseGeocoder(latitude, longitude, function(result) {
       if (!result.success || !result.places || result.places.length == 0) {
           alert('Could not find any places.');
           return;
       }
       var places = result.places;
       Ti.API.info("\nReverse Geocode address == " + JSON.stringify(places));
       driverCity = places[0].city;
       // Current city
       driverState = places[0].address;
       // Current State
       annotation.title = places[0].displayAddress;
       // Whole address
       // Ti.API.info("\nReverse Geocode address == " + JSON.stringify(places));
    });
});

然后查看从您的通话中return收到的内容。