如何获取街道地址作为标记的标题

cgvd09ve  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(254)

下面是我的密码。。
fetchlastlocation()是在单击gps图标(imageview)时调用的方法。这段代码可以工作,但我想获得/获取街道地址并将其附加到标记标题。我该怎么做才能得到这个结果?

private void fetchlastlocation() {
        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]
                    {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);

            return;
        }
        Task<Location> task=fusedLocationProviderClient.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location1) {
                if(location1!=null){
                    currentlocation=location1;

                    LatLng latLng=new LatLng(currentlocation.getLatitude(),currentlocation.getLongitude());
                    MarkerOptions markerOptions=new MarkerOptions().position(latLng);
                    nMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
                    nMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,50));
                }
            }
        });
    }
x4shl7ld

x4shl7ld1#

使用地理编码器从latlong获取地址

public static String getCountryName(Context context, double latitude, double longitude) {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
try {
    addresses = geocoder.getFromLocation(latitude, longitude, 1);
    Address result;

    if (addresses != null && !addresses.isEmpty()) {
        return addresses.get(0).getAddressLine(0);
       //     String city = addresses[0].locality;
       //     String state = addresses[0].adminArea;
      //      String country = addresses[0].countryName;
      //      String postalCode = addresses[0].postalCode;
      //      String knownName = addresses[0].featureName;
    }
    return null;
} catch (IOException ignored) {
   //do something
}

}
jum4pzuy

jum4pzuy2#

你要做的就是逆向地理编码。检查文档
因为您已经有了地理坐标(纬度和经度),所以可以在geocoder类中使用它们来查找所需的可读地址

相关问题