在代号1中使用LocationManager的正确方法 [英] Proper way of using LocationManager in Codename one

查看:95
本文介绍了在代号1中使用LocationManager的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LocationManager来跟踪用户的位置,并在GoogleMaps上显示相同的内容。
我正在使用以下方法,并且有相同的问题。



要第一次获取用户的位置,我正在使用:

  locationManager = LocationManager.getLocationManager(); 
location = locationManager.getCurrentLocation();

我知道这是一个阻止呼叫。因此,我显示一个无限的进度条,直到这个方法返回。

一旦我有了位置,我就调用WebBrowser组件中的服务器上托管的jsp页面, lat和lng到这个页面。



我重写了 onLoad()方法,该页面将用户的位置作为标记返回一个Google地图。 WebBrowser在页面加载时获取事件。
一旦页面被加载,我将一个监听器分配给我的 locationManager 来获取位置更新。
当我在侦听器中接收到更新时,我使用 BrowserComponent - > execute()

问题:



这个方法工作得很好。 b
$ b

当我第一次为用户的位置查询LocationManager时,我没有明确分配侦听器。这个方法如何在内部执行?

这是否会启动它自己的侦听器?如果是,那么当我在 onLoad()方法中分配一个侦听器时,初始侦听器是否被删除?



或者当我分配侦听器时,是否分配给侦听器?



我最终启动了一个监听器?



我最终会让两个监听器做同样的工作吗?



BR, / p>

Sanket

解决方案

您正在使用API​​正确调用getCurrentLocation )或getLastKnownLocation()将返回一个Location对象(它可能会在内部启动一个侦听器,但会在内部停止)。
当你打电话给setListener时,你应该清除你的监听器,当你不想再获取更新时。
(无论哪种方式,您最终都不会得到2个侦听器)注意,getCurrentLocation()可能会返回Android上较不准确的位置对象,因为如果gps不可用和/或不工作,它将默认为网络提供商。


I am using the LocationManager to trace a user's location and show the same on GoogleMaps. I am using the following approach and have some questions regarding the same.

To get the user's location for the first time, I am using:

locationManager = LocationManager.getLocationManager();
location = locationManager.getCurrentLocation();

What I understand that this is a blocking call. Hence, I show an infinite progress bar till this method returns.

Once I have the location, I call a jsp page hosted on my server in a WebBrowser component and pass the lat and lng to this page. This page return a google map with the user's location as a marker.

I override the onLoad() method of the WebBrowser to get an event when the page is loaded. As soon as the page is loaded I assign a listener to my locationManager to get location updates. When I receive updates in the listener, I execute a javascript on the page using the BrowserComponent -> execute() method.

This approach is working very well.

The question:

When I query the LocationManager for the user's position for the first time, I am not explicitly assigning a listener. How does this method get executed internally ?

Does this start a listener of its own ?

If yes, then when I assign a listener in the onLoad() method, does the initial listener get removed ?

Shall I explicitly clear the listeners to my locationManager ?

Or when I assign the listener, does it assign the earlier initiated listener ?

Will I end up having two listeners doing the same job ?

BR,

Sanket

解决方案

You are using the API correctly call to getCurrentLocation() or getLastKnownLocation() will return a Location object.(it may start a listener internally, but it will be stopped internally). When you call to setListener you should clear your listener when you don't want to get updates any more. (In either way you won't end up with 2 listeners)

Notice the getCurrentLocation() may return a less accurate Location object on Android, because if the gps is not available and/or not working it will default to the Network provider.

这篇关于在代号1中使用LocationManager的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆