简体   繁体   English

Android GPS更新位置

[英]Android GPS update location

I am making simple application which based on phone's location. 我正在根据手机的位置制作一个简单的应用程序。 I am using this code for GPS location. 将此代码用于GPS定位。 In the main() function I am requesting the GPS location and then I am working with it. main()函数中,我请求GPS位置,然后使用它。 (I am calculating the sunrise and sunset) (我正在计算日出和日落)

I can get the location, but when I start the application I get the LastKnown location. 我可以获得位置,但是当我启动应用程序时,我得到了LastKnown位置。 So, if I want real "new one" location I must wait for GPS lock on and then restart the application. 因此,如果我想要真正的“新位置”,则必须等待GPS锁定,然后重新启动应用程序。

So, my question is: How can I get a "fresh" location? 因此,我的问题是:如何获得“新鲜”位置?

Ok. 好。 I have an idea. 我有个主意。

Can I call MainActivity refresh from another class? 我可以叫MainActivity从另一个班级刷新吗?

in GPS class I have this 在GPS课上,我有这个

private final Context mContext;

@Override
public void onLocationChanged(Location location) {
    startActivity(new Intent(mContext, MainActivity.class));
}

But this doesn't work. 但这是行不通的。 Can anyone help? 有人可以帮忙吗?

Here is the overview of location services in Android. 是Android中定位服务的概述。 Here is the LocationManager class at the heart of location services in Android. 是Android中位置服务核心的LocationManager类。

Here is an Android developer tutorial on "Location Strategies". 是有关“位置策略”的Android开发人员教程。 Here is another tutorial that might be usable. 是另一个可能有用的教程。

Also, you cannot just ask Android for your current position, since GPS may take quite some time to get a fix. 另外,您不能仅向Android请求您的当前位置,因为GPS可能需要花费一些时间才能解决。 Instead, you need to request location updates and use the first update you get, or similar patterns. 相反,您需要请求位置更新,并使用获得的第一个更新或类似的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM