Geolocation CodenameOne getCurrentLocation方法之间有什么区别?

Geolocation CodenameOne getCurrentLocation方法之间有什么区别?,geolocation,codenameone,Geolocation,Codenameone,我正在为Android和iOS构建一个应用程序,它可以拍照、检索设备位置并将包发送到服务器进行处理 要获取设备位置,我知道我需要使用LocationManager的getCurrentLocation方法之一。但它们在我看来都很像,所以我选了一个,但我不确定我选对了,因为它在Android(KitKat)上运行得不太好(我稍后会解释) 事实上,我经历了与最近报道的(这里)相同的怪事,甚至更糟。例如,连接到家庭/办公室Wifi,位置(尽管是室内的)是准确的。据我所知,该设备的结果是基于网络的。但是

我正在为Android和iOS构建一个应用程序,它可以拍照、检索设备位置并将包发送到服务器进行处理

要获取设备位置,我知道我需要使用LocationManager的getCurrentLocation方法之一。但它们在我看来都很像,所以我选了一个,但我不确定我选对了,因为它在Android(KitKat)上运行得不太好(我稍后会解释)

事实上,我经历了与最近报道的(这里)相同的怪事,甚至更糟。例如,连接到家庭/办公室Wifi,位置(尽管是室内的)是准确的。据我所知,该设备的结果是基于网络的。但是,当我在5公里(40分钟)外的开阔天空中,使用getCurrentLocation或getCurrentLocationAsync执行位置测试,甚至超时时,设备会以50米的精度输出我的家庭/办公室过去的位置

我还注意到,通常出现在Android状态栏靠近时钟的位置图标没有出现。为了让它看起来更像,我注意到,从谷歌启动地图应用程序时,位置图标出现了,然后我的应用程序就能够定位设备了

以下是我用来获取位置的最后一种方法:

`public static final void updateGeolocation (){
    Location location = null;
    try {
        location = LocationManager.getLocationManager().getCurrentLocation();
        setLocation(location);

        setGeolocationAccuracy(location.getAccuracy() > 0.0f ? location.getAccuracy() : DEFAULT_GEOLOCATION_ACCURACY);
    } catch (IOException e) {
        setLocation(null);
        setGeolocationAccuracy(DEFAULT_GEOLOCATION_ACCURACY);
    } 
}`
现在,我将通过timerTask更新位置:

// On lance la mise à jour périodique de la position de l'appareil
    // la tache se lance en dehors de l'EDT

    ParametresGeneraux.setCheckTimer(new Timer());
    ParametresGeneraux.setCheckTask(new TimerTask(){

        @Override
        public void run() {
            ParametresGeneraux.updateGeolocation();

        }
    });
    ParametresGeneraux.getCheckTimer().schedule(ParametresGeneraux.getCheckTask(), 0, ParametresGeneraux.GEOLOCATION_CHECK_INTERVAL);
注意:关于构建提示,我用提示ios.locationUsageDescription解释了我对GPS的需求 我禁用了android.captureRecord提示,因为我确实需要它,并且不想让用户怀疑我为什么需要捕获记录

因此,我的问题是:

  • 我是否以正确的方式使用getCurrentLocation,从而可以责怪我的手机硬件,还是我用错了

  • 为什么屏幕上半部分的位置图标只有在我启动谷歌地图而不是我的应用程序时才会出现(好像我的应用程序没有触发位置)

    • 如果我不使用超时,而位置需要10分钟才能到达,那该怎么办?会发生什么?如果我将超时设置为10秒,而位置在10分钟后到达(例如,我在隧道中),会有什么区别

    • 尽管LocationListener可能仅在设备位置更改时触发,但是否首选使用它

提前感谢任何能让我更清楚的人

编辑:以下@ShaiAlmog建议使所有工作顺利进行,我必须:

  • 请勿使用上述updateGeolocation()方法
  • 创建实现LocationListener的我的GeolocationListener,并在重写的updateLocation方法中执行我的工作(请参见下文)
  • 在主类的init方法中将LocationListener设置为my GeolocationListener
现在更新的位置可用,位置图标按预期显示

My GeolocationListener与以下代码一样简单:

public class GeolocationListener implements LocationListener{

@Override
public void providerStateChanged(int newState) {


}

/**
 * Met à jour les valeur de la position et la précision de la géolocalisation si le service de géoloc est dispo. Sinon met à jour les valeurs avec null pour la position
 * et DEFAULT_GEOLOCATION_ACCURACY pour la précision
 */
@Override
public void locationUpdated(Location location) {
    // Par défaut
    ParametresGeneraux.setLocation(null);
ParametresGeneraux.setGeolocationAccuracy(ParametresGeneraux.DEFAULT_GEOLOCATION_ACCURACY);

/*On met à jour la position et la précision
*/
if (location != null && (location.getStatus() == LocationManager.AVAILABLE)){

            ParametresGeneraux.setLocation(location);
            if ( location.getAccuracy() > 0.0f ) {
                ParametresGeneraux.setGeolocationAccuracy(location.getAccuracy());
            } 

        } // fin de la mise à jour de la position
}
}


如果您需要经常监视您需要使用的位置,则会在位置发生变化时通知您。我们在Google Play中使用了混合定位API,它试图在GPS使用方面更高效,并且只在需要时启动

由于获取GPS信号可能需要几分钟,并且可能不准确/不可用,因此这是一种更简单的临时请求方式。但是如果你使用监听程序并保持其打开状态,那么当你移动时,这个位置应该会慢慢地自我完善,多亏了Google Play服务,它也不需要那么多电池


使用此方法时,不应使用
getCurrentLocation
getCurrentLocationSync

谢谢@ShaiAlmog这绝对是正确的方法。事实上,位置图标现在确实出现了,而且位置会随着我的移动而自动更新!