【发布时间】:2019-08-01 14:59:47
【问题描述】:
我在 Unity 中使用 Google Maps Reverse Geolocation 来查找输入的纬度和经度的国家/地区名称。
我已经通过 Inspector 输入来自世界各地的位置来测试代码。 每次输出国家名称(如英国、美国、法国、新加坡等)。 但是,当我输入俄语经纬度时,它只返回一个数字。 这个数字原来是该地区的邮政编码。
这是我正在使用的代码:
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;
}
};
}
我已经厌倦了俄罗斯境内的几个地点,它只输出数字。 我不知道其他国家是否会发生这种情况,但我尝试过的其他国家都可以正常工作并输出国家名称。
【问题讨论】:
-
你能提供你尝试过的纬度、经度值吗?
标签: c# android google-maps unity3d reverse-geocoding