Android:如何找到我的位置

时间:2012-06-12 15:34:21

标签: android gps

我已经在stackoverflow上阅读了很多关于此的帖子,但我没有找到我需要的答案。使用gps或使用网络提供商查找我的位置非常简单,但是存在问题。我的应用程序首先检查gps是否处于活动状态,如果没有,我将用户发送到gps选项页面。之后,用户可以返回第一个活动。我使用此活动中的当前位置和第二个活动。我只将侦听器放在第一个活动中,并在函数“onLocationUpdate()”中将我的纬度和经度保存在我的全局应用程序变量中。如果我启动应用程序然后我设置启用gps然后我打开我的第二个活动,lat和lon等于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) {
    }
};

这是我的倾听者。

2 个答案:

答案 0 :(得分:2)

您应该阅读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

答案 1 :(得分:1)

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

在第一次拨打onLocationUpdate()之前,GPS尚未就绪。因此,您可以让用户保持第一个活动,直到发生此事件,然后允许用户转到第二个活动。或者让第二个活动获得位置更新。你可以做不同的事情。

至于启用GPS而不进入设置页面,你不能。我找不到这个问题,但几个小时前CommonsWare刚刚回答了这个问题,说这不会发生,因为这是一个隐私问题。

相关问题