- Home /
Reverse Geolocation not returning correct country name
I am using the following code that takes latitude and longitude and returns the name of the country:
private string GoogleAPIKey = "MY KEY";
public string latitude;
public string longitude;
private string countryLocation;
public Text console;
IEnumerator Start()
{
if (!Input.location.isEnabledByUser){
yield break;
}
Input.location.Start();
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
if (maxWait < 1)
{
yield break;
}
if (Input.location.status == LocationServiceStatus.Failed)
{
yield break;
}
else
{
longitude = Input.location.lastData.longitude.ToString();
latitude = Input.location.lastData.latitude.ToString();
console.text += (latitude + " " + longitude);
}
Input.location.Stop();
using (WWW www = new WWW("https://maps.googleapis.com/maps/api/geocode/json?latlng=" +latitude +","+ longitude + "&key=" + GoogleAPIKey)){
yield return www;
if(www.error == null)
{
var location = Json.Deserialize(www.text) as Dictionary<string, object>;
var locationList = location["results"] as List<object>;
var locationListing = locationList[0] as Dictionary<string, object>;
countryLocation = locationListing["formatted_address"].ToString().Substring(locationListing["formatted_address"].ToString().LastIndexOf(",")+2);
console.text += (" LOCATION IS: " + countryLocation.ToString());
}else{
console.text += www.error;
}
};
}
I've tested this with several locations around the world by assigning the lat and long in the Inspector. The one country that is giving me issues is Russia; any location I input, it returns a number, which I worked out is the postal code of the location.
Why is this location returning numbers when all the other ones (such as USA, UK, France, Australia) return the name of the country correctly?
Has anyone else had this issue?
Comment
Looks to me like this is a question about the google maps API, nothing to do with Unity. Have you tried asking google, or on forums etc about that?