Android 钛合金中的地理定位为空

发布于 2024-11-06 09:24:59 字数 1330 浏览 1 评论 0原文

我无法在模拟器或实体手机上获取地理位置。

我正在使用 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
        };
    }
}

I can't get geolocations in the emulator or on a physical phone.

I'm using Titanium SDK 1.6.2, ADK 2.2.

I've followed the approaches used here to no avail.

What am I missing? Thanks in advance.

Error:

Says that e.coords is null when doing this assignment. f_lng = e.coords.longitude;

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

著墨染雨君画夕 2024-11-13 09:24:59
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 );    

在此处查看更多详细信息 http://blog.clearlyinnovative.com/post/5384374513/钛-appcelerator-quickie-get-location-android

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

see more details here http://blog.clearlyinnovative.com/post/5384374513/titanium-appcelerator-quickie-get-location-android

早茶月光 2024-11-13 09:24:59

顺着亚伦的回答,这是对我来说在 iPhone 模拟器、iPhone 和 Android 手机(不是 Android 模拟器)上有用的方法。请记住,我使用 redux 因此代码会略有不同。

var path = Ti.Platform.name == 'android' ? Ti.Filesystem.resourcesDirectory : "../../";

var map = {

    top: 0,
    bottom: 0,
    latitude: 0,
    longitude: 0,
    latitudeDelta: 0.1,
    longitudeDelta: 0.1,
    display: "map",

    init: function (annotations, latitude, longitude, top, bottom, delta) {

        if (top)
            map.top = top;
        if (bottom)
            map.bottom = bottom;
        if (delta) {
            map.latitudeDelta = delta;
            map.longitudeDelta = delta;
        }

        map.createMap(annotations, latitude, longitude);
        map.createOptions();
        map.getLocation();

    },

    createMap: function (annotations, latitude, longitude) {

        map.mapView = Ti.Map.createView({
            mapType: Ti.Map.STANDARD_TYPE, animate: true, regionFit: false, userLocation: true,
            region: { latitude: latitude, longitude: longitude, latitudeDelta: map.latitudeDelta, longitudeDelta: map.longitudeDelta },
            annotations: annotations, bottom: map.bottom, top: map.top, borderWidth: 1
        });
        if (!isAndroid) {
            map.mapView.addAnnotation(annotations[0]);
        }
        map.mapView.selectAnnotation(annotations[0]);
        win.add(map.mapView);

    },

    createOptions: function () {

        //map/satellite displays.
        var mapDisplay = new ImageView({ image: path + 'images/map/satellite-view.png', width: 70, height: 49, zIndex: 2, top: map.top + 5, right: 5 });
        mapDisplay.addEventListener('click', function () {
            if (map.display == "map") {
                map.mapView.setMapType(Titanium.Map.SATELLITE_TYPE);
                mapDisplay.image = path + "images/map/map-view.png";
                map.display = "satellite";
            }
            else {
                map.mapView.setMapType(Titanium.Map.STANDARD_TYPE);
                mapDisplay.image = path + "images/map/satellite-view.png";
                map.display = "map";
            }
        });
        win.add(mapDisplay);

        //crosshairs.
        if(Ti.Geolocation.locationServicesEnabled) {
            var centerDisplay = new ImageView({ image: path + 'images/map/crosshairs.png', width: 49, height: 49, zIndex: 2, top: map.top + 5, right: 80 });
            centerDisplay.addEventListener('click', function () {
                if(map.latitude != 0 && map.longitude != 0) {
                    info("setting user location to " + map.latitude + " / " + map.longitude);
                    //center map.
                    var userLocation = {
                        latitude: map.latitude,
                        longitude: map.longitude,
                        latitudeDelta: map.latitudeDelta,
                        longitudeDelta: map.longitudeDelta,
                        animate: true
                    };
                    map.mapView.setLocation(userLocation);
                }
            });
            win.add(centerDisplay);
        }

    },

    createAnnotation: function (title, subtitle, latitude, longitude, isLocation, addToMap) {

        var mapAnnotation = Ti.Map.createAnnotation({
            latitude: latitude, longitude: longitude,
            title: title,
            subtitle: subtitle,
            animate: true
        });
        if (isAndroid) {
            mapAnnotation.pinImage = path + (isLocation ? "images/map/blue-pin.png" : "images/map/purple-pin.png");
        }
        else {
            mapAnnotation.pincolor = isLocation ? Ti.Map.ANNOTATION_PURPLE : Ti.Map.ANNOTATION_RED;
        }

        if (addToMap)
            map.mapView.addAnnotation(mapAnnotation);

        return mapAnnotation;

    },

    updateAnnotation: function (mapAnnotation, title, subtitle, latitude, longitude, isLocation) {

        if (mapAnnotation) {
            map.mapView.removeAnnotation(mapAnnotation);
            mapAnnotation = map.createAnnotation(title, subtitle, latitude, longitude, isLocation);
            map.mapView.addAnnotation(mapAnnotation);
            map.mapView.selectAnnotation(mapAnnotation);
        }

    },

    addAnnotation: function (mapAnnotation) {

        map.mapView.addAnnotation(mapAnnotation);

    },

    removeAnnotation: function (mapAnnotation) {

        map.mapView.removeAnnotation(mapAnnotation);

    },

    selectAnnotation: function (mapAnnotation) {

        map.mapView.selectAnnotation(mapAnnotation);

    },

    createRoute: function (name, points) {

        var route = {
            name: name, points: points, color: "#7c74d4", width: 4
        };
        map.mapView.addRoute(route);
        setTimeout(function () { map.mapView.regionFit = true; }, 700);

    },

    getLocation: function() {

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

        if(!Ti.Geolocation.locationServicesEnabled) {
            //alert('Your device has GPS turned off. Please turn it on.');
            return;
        }

        function updatePosition(e) {
            if(!e.success || e.error) {
                info("Unable to get your location - " + e.error);
                return;
            }
            info(JSON.stringify(e.coords));
            map.latitude = e.coords.latitude;
            map.longitude = e.coords.longitude;
            Ti.Geolocation.removeEventListener('location', updatePosition);
        };

        Ti.Geolocation.getCurrentPosition(updatePosition);    
        Ti.Geolocation.addEventListener('location', updatePosition);

    }

};

Piggy-backing off of Aaron's answer, here is what worked for me on IPhone Simulator, IPhone, and Android phone (not Android simulator). Keep in mind that I use redux so the code will be a little different.

var path = Ti.Platform.name == 'android' ? Ti.Filesystem.resourcesDirectory : "../../";

var map = {

    top: 0,
    bottom: 0,
    latitude: 0,
    longitude: 0,
    latitudeDelta: 0.1,
    longitudeDelta: 0.1,
    display: "map",

    init: function (annotations, latitude, longitude, top, bottom, delta) {

        if (top)
            map.top = top;
        if (bottom)
            map.bottom = bottom;
        if (delta) {
            map.latitudeDelta = delta;
            map.longitudeDelta = delta;
        }

        map.createMap(annotations, latitude, longitude);
        map.createOptions();
        map.getLocation();

    },

    createMap: function (annotations, latitude, longitude) {

        map.mapView = Ti.Map.createView({
            mapType: Ti.Map.STANDARD_TYPE, animate: true, regionFit: false, userLocation: true,
            region: { latitude: latitude, longitude: longitude, latitudeDelta: map.latitudeDelta, longitudeDelta: map.longitudeDelta },
            annotations: annotations, bottom: map.bottom, top: map.top, borderWidth: 1
        });
        if (!isAndroid) {
            map.mapView.addAnnotation(annotations[0]);
        }
        map.mapView.selectAnnotation(annotations[0]);
        win.add(map.mapView);

    },

    createOptions: function () {

        //map/satellite displays.
        var mapDisplay = new ImageView({ image: path + 'images/map/satellite-view.png', width: 70, height: 49, zIndex: 2, top: map.top + 5, right: 5 });
        mapDisplay.addEventListener('click', function () {
            if (map.display == "map") {
                map.mapView.setMapType(Titanium.Map.SATELLITE_TYPE);
                mapDisplay.image = path + "images/map/map-view.png";
                map.display = "satellite";
            }
            else {
                map.mapView.setMapType(Titanium.Map.STANDARD_TYPE);
                mapDisplay.image = path + "images/map/satellite-view.png";
                map.display = "map";
            }
        });
        win.add(mapDisplay);

        //crosshairs.
        if(Ti.Geolocation.locationServicesEnabled) {
            var centerDisplay = new ImageView({ image: path + 'images/map/crosshairs.png', width: 49, height: 49, zIndex: 2, top: map.top + 5, right: 80 });
            centerDisplay.addEventListener('click', function () {
                if(map.latitude != 0 && map.longitude != 0) {
                    info("setting user location to " + map.latitude + " / " + map.longitude);
                    //center map.
                    var userLocation = {
                        latitude: map.latitude,
                        longitude: map.longitude,
                        latitudeDelta: map.latitudeDelta,
                        longitudeDelta: map.longitudeDelta,
                        animate: true
                    };
                    map.mapView.setLocation(userLocation);
                }
            });
            win.add(centerDisplay);
        }

    },

    createAnnotation: function (title, subtitle, latitude, longitude, isLocation, addToMap) {

        var mapAnnotation = Ti.Map.createAnnotation({
            latitude: latitude, longitude: longitude,
            title: title,
            subtitle: subtitle,
            animate: true
        });
        if (isAndroid) {
            mapAnnotation.pinImage = path + (isLocation ? "images/map/blue-pin.png" : "images/map/purple-pin.png");
        }
        else {
            mapAnnotation.pincolor = isLocation ? Ti.Map.ANNOTATION_PURPLE : Ti.Map.ANNOTATION_RED;
        }

        if (addToMap)
            map.mapView.addAnnotation(mapAnnotation);

        return mapAnnotation;

    },

    updateAnnotation: function (mapAnnotation, title, subtitle, latitude, longitude, isLocation) {

        if (mapAnnotation) {
            map.mapView.removeAnnotation(mapAnnotation);
            mapAnnotation = map.createAnnotation(title, subtitle, latitude, longitude, isLocation);
            map.mapView.addAnnotation(mapAnnotation);
            map.mapView.selectAnnotation(mapAnnotation);
        }

    },

    addAnnotation: function (mapAnnotation) {

        map.mapView.addAnnotation(mapAnnotation);

    },

    removeAnnotation: function (mapAnnotation) {

        map.mapView.removeAnnotation(mapAnnotation);

    },

    selectAnnotation: function (mapAnnotation) {

        map.mapView.selectAnnotation(mapAnnotation);

    },

    createRoute: function (name, points) {

        var route = {
            name: name, points: points, color: "#7c74d4", width: 4
        };
        map.mapView.addRoute(route);
        setTimeout(function () { map.mapView.regionFit = true; }, 700);

    },

    getLocation: function() {

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

        if(!Ti.Geolocation.locationServicesEnabled) {
            //alert('Your device has GPS turned off. Please turn it on.');
            return;
        }

        function updatePosition(e) {
            if(!e.success || e.error) {
                info("Unable to get your location - " + e.error);
                return;
            }
            info(JSON.stringify(e.coords));
            map.latitude = e.coords.latitude;
            map.longitude = e.coords.longitude;
            Ti.Geolocation.removeEventListener('location', updatePosition);
        };

        Ti.Geolocation.getCurrentPosition(updatePosition);    
        Ti.Geolocation.addEventListener('location', updatePosition);

    }

};
迷乱花海 2024-11-13 09:24:59

您是否尝试过设置超时以确保在使用之前设置e.coords,有人建议将其作为临时修复?

setTimeout(function() {
    return e.coords
}, 1000);

Have you tried setting a timeout to make sure the e.coords is set before you use it, it has been suggested as a temporary fix?

setTimeout(function() {
    return e.coords
}, 1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文