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

时间:2016-07-14 08:32:25

标签: google-maps appcelerator titanium-mobile appcelerator-mobile reverse-geocoding

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

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

1 个答案:

答案 0 :(得分:2)

我建议首先在函数中使用不同的变量作为返回参数,并使用负面条件来减少缩进:

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

然后查看通话中返回的内容。