Android 无法使用GPS找到用户位置

Android 无法使用GPS找到用户位置,android,Android,我正在使用上述代码查找用户位置。 1.根据我的标准,我一直将GPS作为最佳供应商,但这对我来说很好。 2.问题是为什么我不能得到任何位置值? 即使使用Location Location=LocationManager.getlastnownlocation(LocationManager.GPS\u提供程序);我什么也没得到。 请帮助我获取位置值。由于速度慢,建筑物内不可用,GPS需要一些时间。您只需在建筑物外进行检查。 及 Location Location=LocationManager.g

我正在使用上述代码查找用户位置。 1.根据我的标准,我一直将GPS作为最佳供应商,但这对我来说很好。 2.问题是为什么我不能得到任何位置值? 即使使用Location Location=LocationManager.getlastnownlocation(LocationManager.GPS\u提供程序);我什么也没得到。
请帮助我获取位置值。

由于速度慢,建筑物内不可用,GPS需要一些时间。您只需在建筑物外进行检查。
及 Location Location=LocationManager.getLastKnownLocation(LocationManager.GPS\U提供程序);只有在GPS之前获得任何位置时才有效。

两件事

  • 在清单文件中设置“android.permission.ACCESS\u FINE\u LOCATION”权限

  • 清除Google地图应用程序的所有数据,然后尝试加载地图以获取地图上的当前位置,如果Google地图提供您的当前位置,则您的应用程序可以获取当前位置

  • 使用最佳条件获取位置是一个好习惯


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

    如果您正在检查移动(设备)位置=Location Manager.getLastKnownLocation(Location Manager.NETWORK_PROVIDER),请尝试此方法进行初始加载,并在GPS打开的情况下移动以获取更新值!!使用网络提供商,它可以工作,但使用GPS,即使我等待10分钟,它也不能工作。将此更改为locManager.RequestLocationUpdate(提供商,0,0,new MyLocationListener());到RequestLocationUpdate(提供程序,8000,2,新的MyLocationListener());。。。8000(更新所需的时间[毫秒])和2指定米…:-感谢提供信息。但问题是,在5分钟或10分钟之后,我也调用了相同的方法,但我无法显示任何值。访问此网站可能会发现您的解决方案我已添加权限:-我已添加权限:-“android.permission.ACCESS\u Rough\u LOCATION”“android.permission.ACCESS\u FINE\u LOCATION”“android.permission.ACCESS\u MOCK\u LOCATION”“android.permission.INTERNET”“android.permission.CONTROL\u LOCATION\u Update”“android.permission.ACCESS\u WIFI\u状态”“android.permission.ACCESS\u网络\u状态”“我还加载了谷歌地图。它显示了我在里面的位置。我用的是安卓2.2。我已经用Android 2.2 API和Google Inc API 2.2尝试了这个项目。我需要其他东西吗?你必须使用谷歌API。为什么你坚持要找GPS提供商,如果你能在网络提供商的帮助下获取位置,那么其中有什么问题?实际上,这是我必须使用GPS的要求。
        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();
    
                }
    
            }
    
        }