Titanium中的Android Geolocation Null

Don*_*nie 1 android geolocation titanium

我无法在模拟器或物理手机上获得地理位置.

我正在使用Titanium SDK 1.6.2,ADK 2.2.

我按照这里使用方法无济于事.

我错过了什么?提前致谢.

错误:

e.coords在执行此分配时表示为null.f_lng = e.coords.longitude;

码:

function get_geolocation() {

    try {

        Ti.Geolocation.preferredProvider = 'gps';
        Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
        Titanium.Geolocation.distanceFilter = 10;

        if( Titanium.Geolocation.locationServicesEnabled === false ) {
            throw('Your device has GPS turned off. Please turn it on.');
        }

        var f_lat, f_lng;

        Titanium.Geolocation.getCurrentPosition(function(e) {

            if( ! e.success || e.error ) {
                alert("Unable to get your location.");
            }

            f_lng = e.coords.longitude;
            f_lat = e.coords.latitude;
        });

        return {
            's_status': 'success',
            'f_lat': f_lat,
            'f_lng': f_lng
        };

    } catch( s_error ) {

        return {
            's_status': 'error',
            's_message': s_error
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

Aar*_*ers 7

Ti.App.GeoApp = {};

Ti.Geolocation.preferredProvider = Titanium.Geolocation.PROVIDER_GPS;
Ti.Geolocation.purpose = "testing";
Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
Titanium.Geolocation.distanceFilter = 10;

if( Titanium.Geolocation.locationServicesEnabled === false ) {
    Ti.API.debug('Your device has GPS turned off. Please turn it on.');
}


function updatePosition(e) {

    if( ! e.success || e.error ) {
        alert("Unable to get your location.");
        Ti.API.debug(JSON.stringify(e));
        Ti.API.debug(e);
        return;
    }

    Ti.App.fireEvent("app:got.location", {
        "coords" : e.coords
    });
};

Ti.App.addEventListener("app:got.location", function(d) {
    Ti.App.GeoApp.f_lng = d.longitude;
    Ti.App.GeoApp.f_lat = d.latitude;
    Ti.API.debug(JSON.stringify(d));
    Ti.Geolocation.removeEventListener('location', updatePosition);

    alert(JSON.stringify(d));

});

var tabGroup = Titanium.UI.createTabGroup();


//
// create base UI tab and root window
//
var window = Titanium.UI.createWindow({
    backgroundColor:'#fff',
    barColor:'#003333',
});
var tab1 = Titanium.UI.createTab({
    icon:'KS_nav_views.png',
    title:'Tab 1',
    window:window
});

tabGroup.open();

Titanium.Geolocation.getCurrentPosition( updatePosition );    
Titanium.Geolocation.addEventListener( 'location', updatePosition );    
Run Code Online (Sandbox Code Playgroud)

在这里查看更多详情http://blog.clearlyinnovative.com/post/5384374513/titanium-appcelerator-quickie-get-location-android