Android - 获取位置(Geocoder / Google Play服务)?

问题描述 投票:0回答:2

我希望以city, address - latitude longitude的形式获得用户的位置。我已经看到有可能使用Google Play服务和地理编码器这样做。是对的吗?您更喜欢哪种方法?为什么?

使用Google Play服务,它看起来更容易实现。有什么我需要考虑的吗?

android geolocation google-play-services geocoding
2个回答
1
投票

您可以阅读包含纬度和经度坐标的how to display a location address in android的完整文档。

使用Android框架位置API中的Geocoder类,您可以将地址转换为相应的地理坐标。您可以使用getFromLocation()方法将地理位置转换为地址。该方法返回对应于给定纬度和经度的估计街道地址。


0
投票

您可以使用以下方法获取用户的当前位置坐标和位置详细信息。

 //Get last known location coordinates
    private void getLastLocationNewMethod() {
        FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
        if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            //session.saveCurrentLocation("Everywhere");
            return;
        }
        mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                // GPS location can be null if GPS is switched off
                if (location != null) {
                    double myLat = location.getLatitude();
                    double myLon = location.getLongitude();

                    getAddress(myLat, myLon);


                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d("MapDemoActivity", "Error trying to get last GPS location");
                e.printStackTrace();

            }
        });
    }

    //get location name from coordinates
    public void getAddress(double lat, double lng) {
        String currentLocation = "";

        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.ENGLISH);
        try {
            List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
            Address obj = addresses.get(0);
            String add = obj.getAddressLine(0);
            add = add + "\n" + obj.getCountryName();
            add = add + "\n" + obj.getCountryCode();
            add = add + "\n" + obj.getAdminArea();
            add = add + "\n" + obj.getPostalCode();
            add = add + "\n" + obj.getSubAdminArea();
            add = add + "\n" + obj.getLocality();
            add = add + "\n" + obj.getSubThoroughfare();

            Log.v("IGA", "Address" + add);


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.