【问题标题】:Android: how does Google Map app retrieve current location?Android:谷歌地图应用程序如何检索当前位置?
【发布时间】:2014-07-21 18:08:36
【问题描述】:

我正在开发一个需要位置感知的应用 (<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />)。它在我的设备(运行 Android 4.4.2)上运行良好,但在 GT-S5830(运行 Android 2.2.1)上运行时遇到问题。

我同时使用android.location.LocationManager 和com.google.android.gms.location.LocationClient。两者都可以在 4.4.2 上正常工作(位置由 NETWORK、GPS 和 FUSED 提供商提供),但在 2.2.1 上,LocationManager 从不调用 onLocationChanged(...) 和 LocationClient 不能使用,因为检查 GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS 返回假的。

反正GT-S5830自带谷歌地图应用,能正确检索当前位置。

关于我做错了什么有什么想法吗?

更新 1: 我构建了一个新应用程序,它只检查 GooglePlayServices 是否可用,以防万一检索当前位置。 GooglePlayServices 实际可用并获取当前位置。现在的问题是:完全相同的方法如何在不同的应用程序上返回两个不同的结果?也许是因为争论?我总是通过this 参数:

1) 当我没有得到 ConnectionResult.SUCCESS 我传递了public class BaseActionBarActivity extends ActionBarActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {的实例

2) 当我 get ConnectionResult.SUCCESS 我传递了public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

的实例

更多更新如下。

Update2:问题似乎与清单中的<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />标签或导入的google-play-services_lib有关。

我刚刚在一台新 PC 上导入了项目,如果标签 <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 不存在,它会编译(这种方式不会在我的另一台 PC 上编译)并且应用程序在我遇到问题的设备上运行良好(如果我留下那个标签并使用随机值,它也可以工作,例如<meta-data android:name="com.google.android.gms.version" android:value="1" />)。

【问题讨论】:

  • 你为什么同时使用旧的 Location API 和新的 Location API,这可能不是一个好主意
  • 我只是想试试这两种方法中的任何一种是否适用于我遇到问题的设备。

标签: android google-maps location google-play-services locationmanager


【解决方案1】:

您可以像这样检查 googlePlayServicesUtil 是否可用:

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        Log.e("PLAY SERVE", "status:" + status);
        try {
            if (status != ConnectionResult.SUCCESS) {
                GooglePlayServicesUtil.getErrorDialog(status, this, 1).show();
            } else {
              //good to go
           }

【讨论】:

  • 我已经试过了,好像服务不可用
  • 我的应用程序没有检索旧设备上的位置,因为需要更新 google-play-services(现在我正在使用 GooglePlayServicesUtil.getErrorDialog(...) 出现一个弹出窗口说需要更新才能使应用程序正常工作)。我猜谷歌地图应用程序无论如何都可以工作,因为它是用旧版本的 google-play-services 构建的,不需要更新。谢谢你,埃里克。
猜你喜欢
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多