1 using System.Collections;
4 using UnityEngine.Networking;
20 [Tooltip(
"The SouthWest end of the bounding box.")]
23 [Tooltip(
"The NorthEast end of the bounding box.")]
30 [Tooltip(
"A XML with the results of a Overpass API query. You can use http://overpass-turbo.eu/ to generate one.")]
31 public TextAsset OsmXmlFile;
33 [Tooltip(
"If true, instead of the XML file above, we fetch the data directly from a Overpass API request to https://www.overpass-api.de/api/interpreter.")]
34 public bool FetchFromOverpassApi;
36 [Tooltip(
"The data configuration used for the Overpass API request. Basically a bounding box rectangle defined by two points.")]
42 [Tooltip(
"The height of the text mesh, relative to the device.")]
43 public float height = 1f;
45 [Tooltip(
"The TextMesh prefab.")]
46 public TextMesh textPrefab;
48 [Tooltip(
"The smoothing factor for movement due to GPS location adjustments; if set to zero it is disabled."), Range(0, 500)]
49 public float movementSmoothingFactor = 100.0f;
51 [Tooltip(
"Locations where the text will be displayed. The text will be the Label property. (Optional)")]
54 [Tooltip(
"Use this to either fetch OpenStreetMap data from a Overpass API request, or via a locally stored XML file. (Optional)")]
69 if (openStreetMapOptions.FetchFromOverpassApi && openStreetMapOptions.overPassRequestData !=
null)
71 StartCoroutine(nameof(LoadXMLFileFromOverpassRequest));
73 else if (openStreetMapOptions.OsmXmlFile !=
null)
75 LoadXMLFileFromTextAsset();
80 private void LoadXMLFileFromTextAsset()
82 CreateTextObjects(openStreetMapOptions.OsmXmlFile.text);
87 var lat1 = data.SouthWest.Latitude;
88 var lng1 = data.SouthWest.Longitude;
89 var lat2 = data.NorthEast.Latitude;
90 var lng2 = data.NorthEast.Longitude;
92 return "https://www.overpass-api.de/api/interpreter?data=[out:xml];node[amenity](" + lat1 +
"," + lng1 +
"," + lat2 +
"," + lng2 +
");out%20meta;";
95 IEnumerator LoadXMLFileFromOverpassRequest()
97 var www = UnityWebRequest.Get(GetOverpassRequestURL(openStreetMapOptions.overPassRequestData));
99 yield
return www.SendWebRequest();
101 if (www.isNetworkError || www.isHttpError)
103 Debug.Log(www.error);
104 Debug.Log(GetOverpassRequestURL(openStreetMapOptions.overPassRequestData));
109 Debug.Log(www.downloadHandler.text);
110 CreateTextObjects(www.downloadHandler.text);
115 public string GetNodeTagValue(XmlNode node,
string tagName)
117 var children = node.ChildNodes;
118 foreach (XmlNode nodeTag
in children)
120 if (nodeTag.Attributes !=
null && nodeTag.Attributes[
"k"].Value == tagName)
122 return nodeTag.Attributes[
"v"].Value;
129 public string GetNodeName(XmlNode node)
131 return (GetNodeTagValue(node,
"poiName") ?? GetNodeTagValue(node,
"amenity")) ??
"No Name";
135 void CreateTextObjects(
string text)
137 XmlDocument xmlDoc =
new XmlDocument();
138 xmlDoc.LoadXml(text);
140 var nodes = xmlDoc.GetElementsByTagName(
"node");
142 poiData =
new POIData[nodes.Count];
145 foreach (XmlNode node
in nodes)
147 if (node.Attributes !=
null)
149 float lat =
float.Parse(node.Attributes[
"lat"].Value);
150 float lng =
float.Parse(node.Attributes[
"lon"].Value);
152 var nodeName = GetNodeName(node);
162 AltitudeMode = AltitudeMode.GroundRelative,
173 for (var k = 0; k < poiData.Length; k++)
175 AddPOI(poiData[k].location, poiData[k].name);
180 void AddLocationsPOIs()
182 foreach (var location
in locations)
184 AddPOI(location, location.Label);
190 void AddPOI(
Location location,
string poiName)
192 var textInstance =
PlaceAtLocation.CreatePlacedInstance(textPrefab.gameObject, location,
195 MovementSmoothing = 0.1f,
196 HideObjectUntilItIsPlaced = true,
197 MaxNumberOfLocationUpdates = 10,
198 UseMovingAverage = false
201 textInstance.GetComponent<TextMesh>().text = poiName;