【问题标题】:Android: how to find my locationAndroid:如何找到我的位置
【发布时间】:2012-06-15 12:43:03
【问题描述】:

我已经阅读了很多关于 stackoverflow 的帖子,但我没有找到我需要的答案。使用 gps 或使用网络提供商查找我的位置非常简单,但是有一个问题。我的应用程序首先检查 gps 是否处于活动状态,如果没有,我将用户发送到 gps 选项页面。之后,用户可以返回第一个活动。我在此活动和第二个活动中使用当前位置。我只将监听器放在第一个活动中,并在函数“onLocationUpdate()”中将我的纬度和经度保存在我的全局应用程序变量中。如果我启动应用程序,然后我设置启用 gps,然后我打开我的第二个活动,纬度和经度等于 0.0,但如果我启动活动,然后我设置启用 gps,然后我等待 30 秒,它工作。 是否存在一种消除这种延迟的方法? 使用 Network_Provider 更快,但它不像 gps 那样精确!

代码片段是:

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    boolean gpsStatus = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (!gpsStatus) {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);


        alertDialogBuilder.setTitle("The GPS is off!");


        alertDialogBuilder
                .setMessage("Turn on the GPS?")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                Intent intent1 = new Intent();
                                intent1.setClassName(
                                        "com.android.settings",
                                        "com.android.settings.SecuritySettings");
                                startActivity(intent1);
                                dialog.cancel();
                            }
                        })
                .setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

    }
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, locationListener);

此部分用于检查 gps 是否启用。有没有不用进入设置页面就可以开启GPS的方法?

    LocationListener locationListener = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) { // update
        if (location != null) {

            msg_r.setLatitude(location.getLatitude());
            msg_r.setLongitude(location.getLongitude());
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};

这是我的听众。

【问题讨论】:

  • 使用 GPS 时总会有时间延迟,因为它需要先获得与至少 4 颗卫星的连接才能提供位置

标签: android gps


【解决方案1】:

您应该阅读 Reto Meier 关于构建基于位置的应用程序的文章:

  1. How to build location based apps that don't suck
  2. Deep dive into location
  3. Deep dive into location part 2

【讨论】:

    【解决方案2】:

    GPS 需要时间来获取您的当前位置。没有办法解决它。如果您希望它更快,或者像您说的那样您可以使用网络提供商,您可以使用方法getLastKnownLocation()。但是,最后一个已知位置可能根本不准确。

    直到第一次调用onLocationUpdate(),GPS 才准备好。因此,您可以让用户停留在第一个活动上,直到此事件发生,然后允许用户转到第二个活动。或者让第二个活动获取位置更新。你可以做很多不同的事情。

    至于在不进入设置页面的情况下启用 GPS,你不能。我找不到问题,但 CommonsWare 几个小时前刚刚回答了这个问题,指出这不可能发生,因为这是一个隐私问题。

    【讨论】:

    • 感谢您的回答!我尝试使用 getLastKnownLocation(),但我总是得到一个空值。 =(
    • 这两个可能是问题吗?如果手机从未有过 GPS 更新,那么它就没有最后已知的位置。你确定你的LocationManager 已经初始化了吗?
    • 但这很奇怪,因为在第一个活动中我使用地图并显示 myOverlay,所以地图知道我的位置并创建覆盖!另外,如果gps关闭,我的位置就会显示出来,所以我认为位置已经知道了! :S
    • 如果你还没弄明白,你能把代码/错误贴在你认为发生这种情况的地方吗?
    • 我使用了这个解决方案:一个“while”,如果我收到不同于 0.0 的纬度和经度,我会在其中检查每个循环。 15 秒后,如果我没有收到任何信息,我会尝试使用最后一个已知位置,如果它为空,我会发送回用户的第一个活动。这不是最好的解决方案,但它有效......我希望!
    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多