如何获取要在Android Studio:Google地图的标记代码片段中显示的特定街道地址?

时间:2019-01-27 19:41:32

标签: java google-maps android-studio geolocation

我正在尝试获取标记代码片段以显示用户的当前地址,但目前仅显示GPS坐标。它能够收集位置并在烤面包中显示当前地址,但是我可以用一种方式显示在标记片段中吗?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

LocationManager locationManager;

LocationListener locationListener;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 1) {

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                {

                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

                }

            }

        }

    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@SuppressLint("MissingPermission")
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());

    //        mMap.clear();
    //        mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
    //        mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));

            Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());

            try {

                List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);


                if (listAddresses != null && listAddresses.size() > 0) {

                    Log.i("PlaceInfo", listAddresses.get(0).toString());

                    String address = "";

                    if (listAddresses.get(0).getSubThoroughfare() != null) {

                        address += listAddresses.get(0).getSubThoroughfare() + " ";

                    }

                    if (listAddresses.get(0).getThoroughfare() != null) {

                        address += listAddresses.get(0).getThoroughfare() + ", ";

                    }

                    if (listAddresses.get(0).getLocality() != null) {

                        address += listAddresses.get(0).getLocality() + ", ";

                    }

                    if (listAddresses.get(0).getPostalCode() != null) {

                        address += listAddresses.get(0).getPostalCode() + ", ";

                    }

                    if (listAddresses.get(0).getCountryName() != null) {

                        address += listAddresses.get(0).getCountryName();

                    }

              //      Toast.makeText( MapsActivity.this, address, Toast.LENGTH_SHORT).show();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };

    if (Build.VERSION.SDK_INT < 23) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);




    } else {

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

        } else {

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
            mMap.clear();


            mMap.addMarker(new MarkerOptions()
                    .position(userLocation)
                    .title("Uw Locatie")
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_marker))
                    .snippet(String.valueOf(lastKnownLocation)))
                    .showInfoWindow();
            mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));

        }

标记当前显示-UW Locatie:位置[gps纬度/经度]

我需要显示标记-UW Locatie:特定地址(地址,邮政编码,街道等)

0 个答案:

没有答案