2017-11-16 6 views
0

Ich möchte meine Karte nach Norden drehen, aber sie dreht sich weiter und hält nicht nach Norden. Ich möchte eine Ausgabe wie Google Map, wo die Rotation der Karte nach Norden festgelegt ist. Bitte überprüfen Sie meine Update() Methode. Schaut euch das Video hier: https://www.youtube.com/watch?v=liXmxdI3Adg&feature=youtu.beGameObject nach Norden drehen

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
using System; 

public class GoogleAPI : MonoBehaviour { 
    private string url; 
    public RawImage img; 
    public float latitude; 
    public float longitude; 
    public Text text; 
    public Text northtext; 

    private IEnumerator Map(float lat, float lon){ 

     url = "https://maps.googleapis.com/maps/api/staticmap?key=AIzaSyAG0hDd5mZNla3GFCi0cs9wWTkWu-eBBWA&center="+lat+","+lon+"&zoom=19&format=png&maptype=roadmap&style=element:geometry%7Ccolor:0x1d2c4d&style=element:labels.text.fill%7Ccolor:0x8ec3b9&style=element:labels.text.stroke%7Ccolor:0x1a3646&style=feature:administrative.country%7Celement:geometry.stroke%7Ccolor:0x4b6878&style=feature:administrative.land_parcel%7Celement:labels.text.fill%7Ccolor:0x64779e&style=feature:administrative.province%7Celement:geometry.stroke%7Ccolor:0x4b6878&style=feature:landscape.man_made%7Celement:geometry.stroke%7Ccolor:0x334e87&style=feature:landscape.natural%7Celement:geometry%7Ccolor:0x023e58&style=feature:poi%7Celement:geometry%7Ccolor:0x283d6a&style=feature:poi%7Celement:labels.text%7Cvisibility:off&style=feature:poi%7Celement:labels.text.fill%7Ccolor:0x6f9ba5&style=feature:poi%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:poi.business%7Cvisibility:off&style=feature:poi.park%7Celement:geometry.fill%7Ccolor:0x023e58&style=feature:poi.park%7Celement:labels.text.fill%7Ccolor:0x3C7680&style=feature:road%7Celement:geometry%7Ccolor:0x304a7d&style=feature:road%7Celement:labels.icon%7Cvisibility:off&style=feature:road%7Celement:labels.text.fill%7Ccolor:0x98a5be&style=feature:road%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:road.highway%7Celement:geometry%7Ccolor:0x2c6675&style=feature:road.highway%7Celement:geometry.stroke%7Ccolor:0x255763&style=feature:road.highway%7Celement:labels.text.fill%7Ccolor:0xb0d5ce&style=feature:road.highway%7Celement:labels.text.stroke%7Ccolor:0x023e58&style=feature:transit%7Cvisibility:off&style=feature:transit%7Celement:labels.text.fill%7Ccolor:0x98a5be&style=feature:transit%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:transit.line%7Celement:geometry.fill%7Ccolor:0x283d6a&style=feature:transit.station%7Celement:geometry%7Ccolor:0x3a4762&style=feature:water%7Celement:geometry%7Ccolor:0x0e1626&style=feature:water%7Celement:labels.text.fill%7Ccolor:0x4e6d70&size=360x640&scale=2"; 
     WWW www = new WWW (url); 
     yield return www; 
     img.texture = www.texture; 
     img.SetNativeSize(); 
    } 

    private IEnumerator StartLocationService(){ 
     if (!Input.location.isEnabledByUser) { 
      Debug.Log ("enable gps pls"); 
      yield break; 
     } 

     Input.location.Start(); 

     int maxWait = 20; 
     while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0){ 
      yield return new WaitForSeconds (1); 
      maxWait--; 
     } 

     if(maxWait <=0){ 
      Debug.Log ("Expired"); 
      yield break; 
     } 

     if (Input.location.status == LocationServiceStatus.Failed) { 
      Debug.Log ("Unable to determine device location"); 
      yield break; 
     } 



     latitude = Input.location.lastData.latitude; 
     longitude = Input.location.lastData.longitude; 

     yield break; 
    } 
    // Use this for initialization 
    void Start() { 
     img = img.GetComponent<RawImage>(); 
     StartCoroutine (StartLocationService()); 
     StartCoroutine (LoadData()); 
     Input.gyro.enabled = true; 
     Input.compass.enabled = true; 
    } 

    private IEnumerator LoadData(){ 
     while(true){ 
      latitude = Input.location.lastData.latitude; 
      longitude = Input.location.lastData.longitude; 
      text.GetComponent<Text>().text = "Lat: "+latitude+"\nLon: "+longitude; 
      StartCoroutine (Map(latitude, longitude)); 
      yield return new WaitForSeconds(1); 
     } 
    } 

    void Update(){ 
     img.GetComponent<RawImage>().transform.Rotate (0,0,Convert.ToInt16(Input.compass.trueHeading)); 
     northtext.GetComponent<Text>().text = Convert.ToInt16(Input.compass.trueHeading).ToString(); 
    } 
} 

Antwort

0

Sie drehen sich unendlich statt Drehung der Einstellung.

Sie können etwas tun:

void Update() 
{ 
    var targetEuler = new Vector3(0,0,Input.compass.trueHeading); 
    img.transform.rotation = Quaternion.Lerp(image.transform.rotation, targetEuler, Time.deltaTime); 
}