【问题标题】:Unable to find user location using GPS无法使用 GPS 找到用户位置
【发布时间】:2012-01-09 05:30:19
【问题描述】:
    public class LocationtesterActivity extends Activity {
        /** Called when the activity is first created. */
        LocationManager locManager;
        // LocationListener locListener;
        Button b;
        String provider;
        TextView lat, alt, longi;


// Here i am first checking if both GPS and network options are enabled in Lovation and Security Settings or not.

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            b = (Button) findViewById(R.id.button1);
            lat = (TextView) findViewById(R.id.lattitude);
            alt = (TextView) findViewById(R.id.altitude);
            longi = (TextView) findViewById(R.id.longitude);

            b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    showCurrentLocation();

                }

            });

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

            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);

            criteria.setPowerRequirement(Criteria.POWER_LOW);

            provider = locManager.getBestProvider(criteria, true);

            System.out.println("best provider is :" + provider);

            if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
                    || !locManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                System.out.println("in provider enablement");
                createGpsDisabledAlert();

            }

            else {
                System.out.println("in location update request");
                locManager.requestLocationUpdates(provider, 0, 0,
                        new MyLocationListener());

            }

        }

        // for displaying the Dialogue Box
        // if GPS or network not enabled
        // and also taking to location and security screen

        private void createGpsDisabledAlert() {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(
            "Your GPS or network provider is disabled! Would you like to enable it?")
            .setCancelable(false)
            .setPositiveButton("Enable provider",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    showGpsOptions();
                }
            });
            builder.setNegativeButton("Do nothing",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }

        private void showGpsOptions() {
            Intent gpsOptionsIntent = new Intent(
                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(gpsOptionsIntent, 5);
        }

        // //////ends/////

        // Code to check whether user enabled GPS and Network provider in settings
        // if not then show the dialogue box again

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == 5 && resultCode == 0) {
                if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    createGpsDisabledAlert();
                    // Toast.makeText(this,
                    // "provider not enabled. Click the button for settings",
                    // 2000).show();
                } else {

                    Intent i=new Intent(this, LocationtesterActivity.class);
                    startActivity(i);
                    Toast.makeText(this, "User has enabled the provider", 1000)
                    .show();

                }
            }
        }

        // Method to display the current location
        protected void showCurrentLocation() {

            Location location = locManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (location != null) {

                String message = String.format(

                        "Current Location \n Longitude: %1$s \n Latitude: %2$s",

                        location.getLongitude(), location.getLatitude()

                );

                lat.setText("" + location.getLatitude());
                longi.setText("" + location.getLongitude());

                Toast.makeText(LocationtesterActivity.this, message,

                        Toast.LENGTH_LONG).show();

            }
            else
            {
                Toast.makeText(LocationtesterActivity.this, "no last known location", 1000).show();
            }

        }

        // Inner class for LocationListener
        private class MyLocationListener implements LocationListener {

            public void onLocationChanged(Location location) {


                System.out.println("in location changed");

                String message = String.format(

                        "New Location \n Longitude: %1$s \n Latitude: %2$s",

                        location.getLongitude(), location.getLatitude()

                );
                lat.setText("" + location.getLatitude());
                longi.setText("" + location.getLongitude());
                // alt.setText("" + location.getAltitude());
                Toast.makeText(LocationtesterActivity.this, message,
                        Toast.LENGTH_LONG).show();

            }

            public void onStatusChanged(String s, int i, Bundle b) {

                Toast.makeText(LocationtesterActivity.this,
                        "Provider status changed",

                        Toast.LENGTH_LONG).show();

            }

            public void onProviderDisabled(String s) {

                Toast.makeText(LocationtesterActivity.this,

                        "Provider disabled by the user. GPS turned off",

                        Toast.LENGTH_LONG).show();

            }

            public void onProviderEnabled(String s) {

                Toast.makeText(LocationtesterActivity.this,

                        "Provider enabled by the user. GPS turned on",

                        Toast.LENGTH_LONG).show();

            }

        }

    }

我正在使用上面的代码来查找用户位置。 1.根据我的标准,我总是将 GPS 作为最佳提供商,但这对我来说很好。 2.事情是为什么我无法获得任何位置值? 即使使用 Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);我没有得到任何东西。 请帮助我获取位置值。

【问题讨论】:

  • 如果您正在检查移动设备(设备)位置 location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) 尝试使用此方法进行初始加载,然后在开启 gps 的情况下获取更新的值!
  • 使用网络提供商它可以工作,但使用 GPS 它即使我等待 10 分钟也无法工作。
  • 将此更改为 locManager.requestLocationUpdates(provider, 0, 0, new MyLocationListener()); to ........ locManager.requestLocationUpdates(provider, 8000, 2, new MyLocationListener());... 8000(time[millisec] req for update..) 和 2 指定米...

标签: android


【解决方案1】:

GPS 需要一些时间,因为速度慢且在建筑物内不可用。您只需在建筑物外检查即可。
和 位置 location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);仅当 GPS 先前获得任何位置时才有效。

【讨论】:

  • :- 感谢您的信息。但问题是,在 5 分钟或 10 分钟后,我也调用了相同的方法,但我无法显示任何值。
  • stackoverflow.com/questions/5124525/… 访问此站点可能会找到您的解决方案
【解决方案2】:

两件事,

  1. 您是否在清单文件中设置了“android.permission.ACCESS_FINE_LOCATION”权限??

  2. 清除谷歌地图应用程序的所有数据,然后尝试加载地图以获取地图上的当前位置,如果谷歌地图提供您的当前位置,那么您的应用程序可以获取当前位置。

使用最佳标准获取位置是一个好习惯。

有时设备无法读取当前位置。我也面临这个问题,但当时我检查了谷歌地图/方向应用程序并重置了我的 GPS。

【讨论】:

  • 我已添加权限:-
  • 我已经添加了权限:- "android.permission.ACCESS_COARSE_LOCATION" "android.permission.ACCESS_FINE_LOCATION" "android.permission.ACCESS_MOCK_LOCATION" "android.permission.INTERNET" "android.permission.CONTROL_LOCATION_UPDATES" “android.permission.ACCESS_WIFI_STATE”“android.permission.ACCESS_NETWORK_STATE”我还加载了谷歌地图。它显示了我的位置。我正在使用 android 2.2。我已经尝试过使用 Android 2.2 API 和 Google Inc API 2.2 的项目。我需要别的东西吗。
  • 你必须使用谷歌API。为什么你坚持使用 GPS 提供商,如果你可以在网络提供商的帮助下获取位置,那么它有什么问题呢??
  • 其实是要求,我必须使用 GPS。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 2023-03-22
相关资源
最近更新 更多