How to get complete address from latitude and longitude?
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
For more info of available details, Look at Android-Location-Address
Get the particular address using latitude and longitude
From a Geocoder object, you can call the getFromLocation(double, double, int) method.
like :-
private String getAddress(double latitude, double longitude) {
StringBuilder result = new StringBuilder();
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
result.append(address.getLocality()).append("\n");
result.append(address.getCountryName());
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
return result.toString();
}
Another best answer is here How to get city name from latitude and longitude coordinates in Google Maps?
Get full address details based on current location's latitude and longitude in Flutter
Using Geocoder plugin you can get address from the latitiude and longitude
import 'package:location/location.dart';
import 'package:geocoder/geocoder.dart';
import 'package:flutter/services.dart';
getUserLocation() async {//call this async method from whereever you need
LocationData myLocation;
String error;
Location location = new Location();
try {
myLocation = await location.getLocation();
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'please grant permission';
print(error);
}
if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
error = 'permission denied- please enable it from app settings';
print(error);
}
myLocation = null;
}
currentLocation = myLocation;
final coordinates = new Coordinates(
myLocation.latitude, myLocation.longitude);
var addresses = await Geocoder.local.findAddressesFromCoordinates(
coordinates);
var first = addresses.first;
print(' ${first.locality}, ${first.adminArea},${first.subLocality}, ${first.subAdminArea},${first.addressLine}, ${first.featureName},${first.thoroughfare}, ${first.subThoroughfare}');
return first;
}
EDIT
Please use Geocoding instead of Geocoder as Geocoding is maintained by baseflow.com agency.
How to get address from latitude and longitude android google map v2
Use the following code snippet:
try {
Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
yourtextfieldname.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
yourtextfieldname.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
//Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
}
}
}
catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
reference :
https://mobiforge.com/design-development/using-google-maps-android
How can I get city name from a latitude and longitude point?
This is called Reverse Geocoding
Documentation from Google:
http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding.
Sample Call to Google's geocode Web Service:
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true&key=YOUR_KEY
Related Topics
What's the Difference Between the Various Methods to Get an Android Context
Android Fragment Handle Back Button Press
Firebase Data Desc Sorting in Android
How to Implement My Very Own Uri Scheme on Android
Android Min Sdk Version Vs. Target Sdk Version
Ideal Way to Cancel an Executing Asynctask
Best Use of Handlerthread Over Other Similar Classes
Failed to Resolve: Com.Android.Support:Appcompat-V7:26.0.0
How to Remove All Debug Logging Calls Before Building the Release Version of an Android App
Integrating the Zxing Library Directly into My Android Application
How to Put Google Maps V2 on a Fragment Using Viewpager
How to Completely Uninstall Android Studio from Windows(V10)
Android Broadcastreceiver on Startup - Keep Running When Activity Is in Background
How to Listen For a Webview Finishing Loading a Url
How to Load an Imageview by Url in Android
Android 8.0: Java.Lang.Illegalstateexception: Not Allowed to Start Service Intent