【问题标题】:How can I get GPS ONLY location in Ionic?如何在 Ionic 中获得 GPS ONLY 位置?
【发布时间】:2018-01-31 10:55:28
【问题描述】:

我正在创建一个应用程序,用户(我工作的公司的成员)只需按下按钮并写下名称即可按需注册兴趣点,然后将它们发送并存储到服务器。

但我对位置的准确性有疑问。我正在使用@ionic-native/geolocation,即使我将enableHighAccuracy 设置为true,我收到的位置也不准确。其中一些比拍摄地点更远。

有没有办法仅通过 GPS 传感器获取地理位置?

我已经在谷歌上搜索了几个小时,但没有运气。也许我做错了什么。

这是我在客户端获取位置的代码:

obtenirPosicio(): Promise<Geoposicio> {
return new Promise<Geoposicio>((resolve, reject) => {

	const OPCIONS_GPS = {} as GeolocationOptions;
	OPCIONS_GPS.enableHighAccuracy = true;
	OPCIONS_GPS.maximumAge = 0;
	OPCIONS_GPS.timeout = 10000; // 10 segons de timeout per obtenir posicio GPS

	this.gps.getCurrentPosition(OPCIONS_GPS).then(res => {
		let nova_posicio = {} as Geoposicio;
		nova_posicio.lat = res.coords.latitude;
		nova_posicio.lng = res.coords.longitude;
		resolve(nova_posicio);
	}).catch(err => {
		reject(err);
	});

});
}

PS:我只针对安卓

【问题讨论】:

    标签: javascript android ionic-framework gps location


    【解决方案1】:

    除非用户将位置模式设置为“仅限设备”(即 GPS),否则当位置模式为“高精度”时,Android 位置管理器将通过 GPS 和非 GPS 来源的位置数据发送,因此有些会Wifi/蓝牙/小区三角测量位置不准确。

    但是,您可以通过查看结果的 coords.accuracy 属性来过滤掉这些,该属性表示位置的估计准确度。 确定您准备接受的最低准确度,然后拒绝任何低于此准确度的。

    例如:

    obtenirPosicio(): Promise<Geoposicio> {
        return new Promise<Geoposicio>((resolve, reject) => {
    
            const MIN_ACCURACY = 20; // metres
            const OPCIONS_GPS = {} as GeolocationOptions;
            OPCIONS_GPS.enableHighAccuracy = true;
            OPCIONS_GPS.maximumAge = 0;
            OPCIONS_GPS.timeout = 10000; // 10 segons de timeout per obtenir posicio GPS
    
            this.gps.getCurrentPosition(OPCIONS_GPS).then(res => {
                // Reject udpate if accuracy is not sufficient
                if(!res.coords.accuracy || res.coords.accuracy > MIN_ACCURACY){
                  console.warn("Position update rejected because accuracy of"+res.coords.accuracy+"m is less than required "+MIN_ACCURACY+"m");
                  return; // and reject() if you want
                }
    
                let nova_posicio = {} as Geoposicio;
                nova_posicio.lat = res.coords.latitude;
                nova_posicio.lng = res.coords.longitude;
                resolve(nova_posicio);
            }).catch(err => {
                reject(err);
            });
    
        });
    }
    

    【讨论】:

    • 非常感谢!我从来没有读过有关准确度属性的信息,它非常有帮助。
    • 尽管如此,用户永远无法通过此解决方案获得好的位置。所以我需要显示一条消息,建议设置“仅 gps”模式。再次感谢!
    猜你喜欢
    • 2011-05-04
    • 2013-03-04
    • 2020-12-17
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2021-06-10
    相关资源
    最近更新 更多