【问题标题】:How to get a current location?如何获取当前位置?
【发布时间】:2020-05-04 11:55:26
【问题描述】:

我尝试了一些方法来获取当前用户位置(纬度、经度、..),但没有任何结果。你能帮助我吗? 我最后一次尝试使用空位置:

    private void getLocation() throws IOException {
        if (ActivityCompat.checkSelfPermission(MainWindow.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(MainWindow.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
        } else {
            Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Location locationNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            Location locationPassive = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

            if (locationGPS != null)
                setCoordinate(locationGPS.getLatitude(), locationGPS.getLongitude());
            else if (locationNetwork != null)
                setCoordinate(locationNetwork.getLatitude(), locationNetwork.getLongitude());
            else if (locationPassive != null)
                setCoordinate(locationPassive.getLatitude(), locationPassive.getLongitude());
            else
                Toast.makeText(this, "Ошибка", Toast.LENGTH_SHORT).show();
        }
    }

    private void setCoordinate(double Latitude, double Longitude) throws IOException {
        latitude = String.valueOf(Latitude);
        longitude = String.valueOf(Longitude);
        coordinate = "Широта: " + latitude + ", долгота: " + longitude;

        geocoder = new Geocoder(this, Locale.getDefault());
        addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
        String state = addresses.get(0).getAdminArea();
    }

【问题讨论】:

标签: android location


【解决方案1】:

尝试使用融合位置提供程序 - https://developers.google.com/location-context/fused-location-provider

效果真的更好。

【讨论】:

  • developer.android.com/training/location/retrieve-current.html 的代码没有帮助。我粘贴了这个 fusedLocationClient.getLastLocation() .addOnSuccessListener(this, new OnSuccessListener() { @Override public void onSuccess(Location location) { // 得到最后一个已知位置。在极少数情况下,这可能为空。 if (location != null) { // 处理位置对象的逻辑 } } });但仍有问题。位置=空
【解决方案2】:

该代码对我有用:

 public void showOurLocation() {

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED && ActivityCompat.
                    checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions((Activity) this,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                                Manifest.permission.ACCESS_FINE_LOCATION}, 0);
                return;
            }
                   fusedLocationClient.getLastLocation()
                    .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                        @Override
                        public void onSuccess(Location location) {

                            if (location != null) {
                           lastLocation = location;
                                String timestamp = new java.util.Date().toString();
                                String address = "";
                                if (Geocoder.isPresent()) {
                                    try {

                                        adr = (ArrayList<Address>) geocoder
                                                .getFromLocation(location.getLatitude(), location.getLongitude(), 2);
                                        address = adr.get(0).toString().split("\"")[1];
                                        lastAddress = "Текущий адрес:\n" + address;

                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }

                                LatLng weAreHere = new LatLng(location.getLatitude(), location.getLongitude());
                                lastLocationRecord=new LocationRecord(Calendar.
                                        getInstance().getTime(),currentUserClearEmail,location,currentUser.getUid());

                            }
                        }
                    });
        }

有时我们仍然可以得到空值,这很正常,我想,只要我们稍后能得到位置就可以了

哦,你应该在你的代码中使用监听器。

【讨论】:

  • 因为我还有空位置(
  • 我请朋友运行这段代码。他也有空位置
  • 清单中有 ACCESS_FINE_LOCATION 吗?自 Android 10 以来还有 ACCESS_BACKGROUND_LOCATION
  • 添加 FusedLocationClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);在那个代码之前。有时还需要 ACCESS_BACKGROUND_LOCATION 权限
  • 什么都没有。 if (location != null) {...} 位置为空。我可以写信给你谈谈我的问题吗?
猜你喜欢
  • 2016-03-06
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 2010-09-07
相关资源
最近更新 更多