简体   繁体   English

如何获取设备位置

[英]How to get device location

So, I'm trying to build a weather app for practice and I've been running into the problem of trying to get the location. 所以,我正在尝试为练习构建一个天气应用程序,我一直在试图找到位置的问题。 I've read people suggest to use getLastLocation through the fused location API, problem with that is if they don't already have a location registered on the device it comes up null. 我读过人们建议通过融合位置API使用getLastLocation,问题是如果他们还没有在设备上注册的位置它出现null。 I've noticed using the emulator times that this come up is rare, but I'd still like my app to handle it properly. 我注意到使用模拟器的时间很少见,但我仍然喜欢我的应用程序来正确处理它。 One instance where you might run into this is if they just turned GPS off and back on, or if the phone was just turned on. 您可能碰到的一个例子是,他们只是关闭并重新打开GPS,或者手机刚打开。 One thing I did was if getLastLocation does come back null, is to request an update, but then you still have to wait for the device to register an updated location, which with a weather app all of the data is based off of and you're still kind of running into the same problem. 我做的一件事是如果getLastLocation确实返回null,是请求更新,但是你仍然需要等待设备注册更新的位置,这与天气应用程序的所有数据都是基于你的'你'仍然有点遇到同样的问题。 I've noticed with other apps this isn't a problem, like sometimes I actually have to load up Google Maps to get it to register a location. 我注意到其他应用程序这不是问题,有时我实际上必须加载谷歌地图才能让它注册一个位置。 How does Google Maps force it to update the location? Google地图如何强制它更新位置? Here's the example from my getLocation method: 这是我的getLocation方法的示例:

public void getLocation() throws SecurityException {
    boolean gps_enabled;
    LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (gps_enabled) {

        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location != null) {
            startGetForecast(location);
        } else {
            LocationRequest request = LocationRequest.create();
            request.setNumUpdates(1);
            request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, request, this);
        }
    } 
    else {
        AlertDialogFragment alertDialogFragment = new AlertDialogFragment();
        alertDialogFragment.setErrorTexts("Location Unavailable", "Could not retrieve location information.", "Exit");
        alertDialogFragment.show(getSupportFragmentManager(), "Location Unavailable");
    }
}

Note: This answer could sound like not answering to the question directly but after chatting (comments in question) with OP as his code was ok in general but the condition around the problem, this answer has satisfied OP. 注意:这个答案可能听起来像没有直接回答问题,但在与OP聊天(有问题的评论)后,因为他的代码一般都没问题,但问题是这个问题,这个答案已经满足了OP。

Fortunately, the standard library Is not the only way Google can get code into your hands. 幸运的是,标准库不是谷歌可以获取代码的唯一方式。 In addition to the standard library, Google provider Play services. 除了标准库,Google提供商Play服务。 This is a set of common services that are installed alongside the Google Play store application. 这是一组与Google Play商店应用程序一起安装的常用服务。 To fix this location mess, Google shipped a new locations services in Play Services called the Fused Location Provider. 为了解决这个位置问题,Google在Play服务中发布了一项名为Fused Location Provider的新位置服务。

Since these libraries live in another application, you must actually have that application installed. 由于这些库存在于另一个应用程序中,因此您必须实际安装该应用程序。 This means that only devices with the Play Store app installed and up to date will be able to use your application. 这意味着只有安装了Play Store应用程序并且最新的设备才能使用您的应用程序。

So the conclusion is: 所以结论是:

  1. You need to test the app on your device as mentioned above. 您需要如上所述在设备上测试应用。
  2. Since you are using Fused Location Provider Api, that means the Api will automatically determine last location from one of the following sources: 由于您使用的是融合位置提供程序Api,这意味着Api将自动确定以下某个来源的最后位置:

    1. GPS radio GPS收音机
    2. Coarse points from cell towers 细胞塔的粗糙点
    3. WiFi connections WiFi连接
  3. So you could easily remove the GPS if condition from your code 因此,您可以轻松地从代码中移除GPS条件

  4. Be a ware of that if you must use GPS signal, you need to be out side the building, lab, home or office. 如果你必须使用GPS信号,你需要在建筑物,实验室,家庭或办公室外面。

If you want to dig more find more in the mentioned resources and there is a lot of online resources. 如果你想在上述资源中挖掘更多,并且有很多在线资源。

Resources: The first part of the answer, the source of it is from: 资源:答案的第一部分,它的来源是:

Android Programming: The Big Nerd Ranch Guide Android编程:大书呆子牧场指南
2nd edition 第2版
chapter 31 page 552 under Google Play Services Google Play服务下的第31章第552页

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

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