我正忙着尝试对从Json格式的Google Maps API收到的信息进行反序列化。
我知道json是写的,我也使用JsonToC#来创建数据类型,但由于某种原因,我的类没有填充json信息。
JSON - https://pastebin.com/gVQ75sfv
C# -
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using AUP;
using System.Collections.Generic;
//-- John Esslemont
public class rCade_GPS : MonoBehaviour
{
public Action OnComplete;
private GPSPlugin gpsPlugin;
public Text gpsStatusText;
public Text latitudeText;
public Text longitudeText;
public Text speedText;
public Text altitudeText;
public Text bearingText;
// new
public Text accuracyText;
public Text distanceInMetersText;
//nmea
public Text timeStampText;
public Text nmeaText;
public Text Json;
private Dispatcher dispatcher;
public GEO resultFromServer;
public float Longitude, Latitude;
public string URL;
public string FromServer;
IEnumerator Start()
{
OnComplete += Deserialize;
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
yield break;
// Start service before querying location
Input.location.Start();
// Wait until service initializes
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
{
yield return new WaitForSeconds(1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1)
{
print("Timed out");
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed)
{
print("Unable to determine device location");
yield break;
}
else
{
// Access granted and location value could be retrieved
print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
Longitude = Input.location.lastData.longitude;
Latitude = Input.location.lastData.latitude;
}
SA_StatusBar.text = Longitude + " ;;;; " + Latitude;
// Stop service if there is no need to query location updates continuously
Input.location.Stop();
}
public void RequestGEOLocation()
{
SA_StatusBar.text = "GPS : Longitude is :: " + Longitude + "Latitude :: " + Latitude;
GetHumanReadableVersion(Latitude, Longitude, "API KEY");
}
public void GetHumanReadableVersion(float longitude, float lat, string apiKey)
{
#if (!UNITY_EDITOR)
{
URL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + longitude + ',' + lat + "&key=" + apiKey;
}
#elif(UNITY_EDITOR)
{
URL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=55.1301,-1.517228&key=API KEY";
}
#endif
SA_StatusBar.text = "Result URL IS " + URL;
StartCoroutine(WebServiceRequest());
}
public virtual IEnumerator WebServiceRequest()
{
WWWForm form = new WWWForm();
WWW wwwRequest = new WWW(URL);
yield return wwwRequest;
if (!String.IsNullOrEmpty(wwwRequest.error))
{
FromServer = wwwRequest.text;
Debug.LogError("Error On Page : " + wwwRequest.error);
}
else if (wwwRequest.text.Length > 0)
{
FromServer = wwwRequest.text;
if (OnComplete != null)
OnComplete();
}
else
{
FromServer = "Null";
Debug.LogError("We got no responce from the server");
}
}
public void Deserialize()
{
Json.text = FromServer;
resultFromServer = JsonUtility.FromJson<GEO>(FromServer);
Debug.Log(resultFromServer.results[0]);
}
// Create Json encoder to take the long and lat for conversion
}
[System.Serializable]
public class address_components
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
[System.Serializable]
public class location
{
public double lat { get; set; }
public double lng { get; set; }
}
[System.Serializable]
public class northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
[System.Serializable]
public class southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
[System.Serializable]
public class viewport
{
public northeast northeast { get; set; }
public southwest southwest { get; set; }
}
[System.Serializable]
public class northeast2
{
public double lat { get; set; }
public double lng { get; set; }
}
[System.Serializable]
public class southwest2
{
public double lat { get; set; }
public double lng { get; set; }
}
[System.Serializable]
public class bounds
{
public northeast2 northeast { get; set; }
public southwest2 southwest { get; set; }
}
[System.Serializable]
public class geometry
{
public location location { get; set; }
public string location_type { get; set; }
public viewport viewport { get; set; }
public Bounds bounds { get; set; }
}
[System.Serializable]
public class results
{
public List<address_components> address_components { get; set; }
public string formatted_address { get; set; }
public geometry geometry { get; set; }
public string place_id { get; set; }
public List<string> types { get; set; }
}
[System.Serializable]
public class GEO
{
public List<results> results { get; set; }
public string status { get; set; }
}