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

Google maps 在appcelerator中使用纬度和经度获取城市名称、地址,google-maps,appcelerator,titanium-mobile,appcelerator-mobile,reverse-geocoding,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');

我对提到的标题有疑问。我可以得到当前的纬度和经度。但当我使用反向地理编码转换为相应的城市名称或地址时,什么也看不出来。有人知道这件事吗?。这是我的密码

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

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

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

然后查看您的呼叫返回的内容。

除了Larrie应答之外,您还可以使用第三方提供商获取lat long的地址。