- Home /
How to get address from latitude and longitude using geocoder ?
private void GetAddress()
{
#if UNITY_ANDROID
AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject context = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass cls = new AndroidJavaClass("java.util.Locale");
AndroidJavaObject locale = cls.CallStatic<AndroidJavaObject>("getDefault");
AndroidJavaObject Address = new AndroidJavaObject("android.location.Address",locale);
AndroidJavaObject geoLocation = new AndroidJavaObject("android.location.Geocoder",context,locale);
object[] toastParams = new object[3];
toastParams[0] = 19.21426;
toastParams[1] = 72.8369;
toastParams[2] = 1;
List<AndroidJavaObject> AddList = geoLocation.Call<List<AndroidJavaObject>>("getFromLocation", toastParams );
string add = AddList[0].Call<string>("getCountryName");
Debug.Log(add);
#endif
}
Here the return type for getFromLocation is list of Address i'm not sure how to achieve this. Geocoder class Here I know this is achievable through geocoder api but that requires api key. so i've decided to go with this method but got stuck here. Please help me with this issue.
Answer by Llama_w_2Ls · Jul 28, 2020 at 12:19 PM
private LocationService locationService;
private void FixedUpdate()
{
float Latitude = locationService.lastData.latitude;
float Longitude = locationService.lastData.longitude;
Debug.Log("Latitude: " + Latitude + ", Longitude: " + Longitude);
}
I dont know what geocoder is, but if you want the latitude and longitude, unity has their own built-in function, Input.Location.
Thanks for replying. But I've already implemented location service using this. Here I've trying to fetch users address details like locality, country name , state name using android.location.Geocoder. Any help would be appreciated. Thanks.
Sorry, as i said before, i dont know how GeoCoder works. Good luck though
Your answer
![](https://koobas.hobune.stream/wayback/20220613004244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Child location problem 1 Answer
Where can i find readymade AI scrips for free? 0 Answers
How do I move the Asset Folder to another drive? 0 Answers
Instantiate a random prefab at an objects location 3 Answers
Length between GPS-locations 1 Answer