Exemple #1
0
 public animal(int no_leg_joints, int no_body_joints) : base()
 {
     hind_leg = new leg(no_leg_joints);
     torso    = new body(no_body_joints);
     skull    = new head();
     subject  = hind_leg;        // the default view is of the leg (this should be changed for the final draft)
 }
    IEnumerator GetJSON()
    {
        string baseURL = "https://maps.googleapis.com/maps/api/directions/json?";
        //string origin = "origin="+ "43.428887,-80.476235";
        string origin = "origin=" + "33.890130,130.708588";
        string dest   = "destination=" + query;
        string mode   = "mode=" + "walking";
        string apiKey = "key=" + "AIzaSyBA24VtZCdo5G0dh8dR5_xiJpUkmIAAVzw";
        //string apiKey = "key=" + "AIzaSyDS5jKpIO_-UVi835Ncapx0Zykkty-QFEE";
        string          api = baseURL + origin + "&" + dest + "&" + mode + "&" + apiKey;
        UnityWebRequest www = UnityWebRequest.Get(api);

        yield return(www.Send());

        if (www.isNetworkError)
        {
            Debug.Log(www.error);
        }
        else
        {
            string result = www.downloadHandler.text;
            Debug.Log(result);
            geoCoded g = JsonUtility.FromJson <geoCoded>(result);
            leg      l = g.routes [0].legs [0];
            Debug.Log(l.end_location.lat);
            Debug.Log(l.end_location.lng);
            // countText.text = Jsonwww.downloadHandler.text;
            steps = new List <step>(l.steps);

            Debug.Log(steps [0].end_location.lng);
            Debug.Log(steps [0].end_location.lat);
        }

        // Initialize directions array
        GameObject[] panels = new GameObject[1];
        panels[0] = Instantiate(panelPrefab, directionsPanel);
        var texts = panels[count].GetComponentsInChildren <Text>();

        texts[0].text = "Distance here";
        texts[1].text = steps[count].maneuver; // description
        texts[2].text = "Step " + (count + 1) + " / " + steps.Count;
        texts[3].text = steps[count].end_location.lat + ", " + steps[count].end_location.lng;

        //panels[1] = Instantiate(panelPrefab, directionsPanel);
        //var texts2 = panels[count+1].GetComponentsInChildren<Text>();


        //while true so this function keeps running once started.
        while (true)
        {
            Debug.Log("in the loop");
            //check if user has location service enabled
            if (!Input.location.isEnabledByUser)
            {
                yield break;
            }

            Debug.Log("after first break");
            // Start service before querying location
            Input.location.Start();

            // Wait until service initializes
            int maxWait = 4;
            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
            {
                lat = Input.location.lastData.latitude;
                lon = Input.location.lastData.longitude;
                //locationText.text = "Location: " + lat + ", " + lon;

                if (steps != null)
                {
                    destLat = steps[count].end_location.lat;
                    destLon = steps[count].end_location.lng;
                    float λ = destLon - lon;

                    var y = Mathf.Sin(λ) * Mathf.Cos(destLat);
                    var x = Mathf.Cos(lat) * Mathf.Sin(destLat) -
                            Mathf.Sin(lat) * Mathf.Cos(destLat) * Mathf.Cos(λ);
                    brng = Mathf.Rad2Deg * Mathf.Atan2(y, x);
                    if (brng < 0)
                    {
                        brng = 360 + brng;
                    }
                    compassBrng = Input.compass.trueHeading;

                    int distance = Mathf.RoundToInt(distance_metres(lat, lon, destLat, destLon));

                    // constantly update distance shown
                    texts[0].text = distance.ToString() + "m";



                    /*distanceText.text = distance.ToString();
                     *                  bearingText.text = brng.ToString();
                     * headingText.text = count.ToString() + "  " + steps.Count;*/

                    if (isCollide())
                    {
                        Destroy(panels[0]);
                        //panels count = panels[count+1]?
                        count++;
                        if (count < steps.Count)
                        {
                            panels[0]     = Instantiate(panelPrefab, directionsPanel);
                            texts         = panels[count].GetComponentsInChildren <Text>();
                            texts[1].text = steps[count].maneuver; // description
                            texts[2].text = "Step " + (count + 1) + " / " + steps.Count;
                            texts[3].text = steps[count].end_location.lat + ", " + steps[count].end_location.lng;
                        }
                    }
                    if (count == steps.Count)
                    {
                    }
                }
                Input.location.Stop();
            }
        }
    }