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

时间:2018-10-23 21:11:47

标签: android geolocation google-play-services geocoding

我想根据city, address - latitude longitude获得用户的位置。 我已经看到有可能使用Google Play服务和Geocoder进行此操作。那正确吗? 您首选哪种方法,为什么?

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

2 个答案:

答案 0 :(得分:1)

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

  

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

答案 1 :(得分: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();
        }
    }