简体   繁体   中英

get all the information from a marker google maps-android studio

I'm making an app that when it opens ,it shows the user's current location.The thing is that I want also to put all the details(home address,postal code,country) by pressing on the marker just like this application Application Photo

Code:

private GoogleMap mMap;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private Location lastLocation;
private Marker currentUserLocationMarker;

here is the method that contains the marker

 @Override
public void onLocationChanged(Location location) {

    lastLocation=location;
    if(currentUserLocationMarker!=null)
    {
        currentUserLocationMarker.remove();
    }

    LatLng latLng= new LatLng(location.getLatitude(),location.getLongitude());
    MarkerOptions markerOptions= new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Test");
    markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_mini));


    currentUserLocationMarker = mMap.addMarker(markerOptions);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomBy(15));

    if(googleApiClient != null)
    {
        LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
    }

}

You can use the Geocoding API and use reverse geocoding to get the details from the latlng. Here is an example reverse geocoding query,

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY

This will return a json response with multiple address results with first address as the most prominent result.

You can also use Geocoder in android as follows, (This example gets the list of addresses and logs the formatted adress line from the returned results)

        Geocoder geocoder=new Geocoder(this);

        try {
            List<Address> addressList=geocoder.getFromLocation(40.714232,-73.9612889,10);

            for(Address address:addressList){
                Log.d("TAG",address.getAddressLine(0));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

The parms of getFromLocation(..) are lat,lng and maximum number of results you want to retrieve with first againg being the prominent one.

Checkout the documentation of Geocoding API to know what else you can do,

https://developers.google.com/maps/documentation/geocoding/intro#reverse-example

Also checkout the documentation of Geocoder,

https://developer.android.com/reference/android/location/Geocoder#Geocoder(android.content.Context,%20java.util.Locale)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM