public LonLatPoint lonlatToMercator(LonLatPoint llP, int level) { long size = mapSize(level); float tlon = getRange(llP.lon, MinLongitude, MaxLongitude); float x = (tlon + 180) / 360; x = getRange(x * size + 0.5f, 0f, size - 1); float tlat = getRange(llP.lat, MinLatitude, MaxLatitude); float sinLatitude = Mathf.Sin(tlat * Mathf.PI / 180); float y = 0.5f - Mathf.Log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Mathf.PI); y = getRange(y * size + 0.5f, 0f, size - 1); return new LonLatPoint((long) x, (long) y); }
public LonLatPoint lonlatToMercator(LonLatPoint llP, int level) { long size = mapSize(level); float tlon = getRange(llP.lon, MinLongitude, MaxLongitude); float x = (tlon + 180) / 360; x = getRange(x * size + 0.5f, 0f, size - 1); float tlat = getRange(llP.lat, MinLatitude, MaxLatitude); float sinLatitude = Mathf.Sin(tlat * Mathf.PI / 180); float y = 0.5f - Mathf.Log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Mathf.PI); y = getRange(y * size + 0.5f, 0f, size - 1); return(new LonLatPoint((long)x, (long)y)); }
// Use this for initialization void Start () { //test // Main.initById (43); zywx_setPoiDateSource (Main.testdataAll); // GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); // plane.transform.position = new Vector3 (0,0.031f,0); // Material yourMaterial = (Material)Resources.Load("moutain", typeof(Material)); // plane.GetComponent<Renderer> ().sharedMaterial = yourMaterial; // Main.initById (); string[] temp1 = Main.appear_camera.Split (new char[] { ',' }); string[] temp11 = temp1[0].Split(new char[] { '_' }); string[] temp12 = temp1[1].Split(new char[] { '_' }); float appear_position_x = float.Parse(temp11 [0]); float appear_position_y = float.Parse(temp11 [1]); float appear_position_z = float.Parse(temp11 [2]); float appear_rotation_x = float.Parse(temp12 [0]); float appear_rotation_y = float.Parse(temp12 [1]); float appear_rotation_z = float.Parse(temp12 [2]); transform.position = new Vector3 (appear_position_x,appear_position_y,appear_position_z); transform.eulerAngles = new Vector3 (appear_rotation_x,appear_rotation_y,appear_rotation_z); // print ("appear_position_x:"+appear_position_x+" appear_position_y:"+appear_position_y+" appear_position_z:"+appear_position_z); // print ("appear_rotation_x:"+appear_rotation_x+" appear_rotation_x:"+appear_rotation_y+" appear_rotation_z:"+appear_rotation_z); foreach (object obj in arr) { showLabelPoiTypes.Add (obj); } LonLatPoint lonlatpoint = new LonLatPoint(116.351856f,39.930587f); LonLatPoint lonlatMercator = gp.lonlatToMercator (lonlatpoint, 17); print("---------------------lonlatMercator is "+(long)lonlatMercator.lon+" "+(long)lonlatMercator.lat); setPcToPhoneScaleByPhoneType (); #if UNITY_EDITOR pcToPhoneScaleImage = 1; #endif road2 = GameObject.Find ("R2"); road3 = GameObject.Find ("R3"); road4 = GameObject.Find ("R4"); road6 = GameObject.Find ("R6"); road8 = GameObject.Find ("R8"); road10 = GameObject.Find ("R10"); road11 = GameObject.Find ("R11"); myCamera = GetComponent<Camera>(); for (int i = 1; i<=24; i++) { Texture2D image_big = (Texture2D)Resources.Load ("map_"+i+"@3x"); GUIContent content_big = new GUIContent (); content_big.image = image_big; texture_big.Add(content_big); Texture2D image_small = (Texture2D)Resources.Load ("map_icon_"+i); GUIContent content_small = new GUIContent (); content_small.image = image_small; texture_small.Add(content_small); } texture_howToPlayBottom = (Texture2D)Resources.Load ("map_11@3x"); texture_howToPlayTop = (Texture2D)Resources.Load ("position_pop"); for (int i=4; i<=Main.ImageCount; i++) { Texture2D image = (Texture2D)Resources.Load ("1.1-" + i); texture_image.Add (image); } texture_myLoc = (Texture2D)Resources.Load ("mylocation"); }
void zywx_moveToLocation(float lon,float lat,bool annimation,bool isWatch,float high,float speed){ step = speed; LonLatPoint lonlatpoint = new LonLatPoint(lon,lat); PixelPoint point = gp.lonlatToPixel (lonlatpoint,17); if (isWatch) { transform.eulerAngles = new Vector3 (60, 180, 0); transform.position = new Vector3(transform.position.x,1.5f,transform.position.z); float zoffsize = Mathf.Tan(DegreetoRadians(90-transform.eulerAngles.x))*transform.position.y; targetPosition = new Vector3 (-(float)point.pointX / 100f, high, (float)point.pointY / 100f+zoffsize); aroundPosition = new Vector3 (-(float)point.pointX / 100f, 0, (float)point.pointY / 100f); wantWatch = true; } else { transform.eulerAngles = new Vector3 (90, 180, 0); targetPosition = new Vector3 (-(float)point.pointX / 100f, high, (float)point.pointY / 100f); } if (annimation) { startMove = true; } else { transform.position = targetPosition; } }
void OnGUI () { #if UNITY_EDITOR if(GUI.Button(new Rect(10,10,100,50),"test")) { zywx_listenOnePoi("109.501732|18.225855|观海亭"); } #endif // GUI.Label (new Rect (Screen.width - 400, 10, 400, 50), ""+myCamera.transform.position.x.ToString("f2")+"_"+myCamera.transform.position.y.ToString("f2")+"_"+myCamera.transform.position.z.ToString("f2")); // GUI.Label (new Rect (Screen.width - 400, 70, 400, 50), ""+myCamera.transform.eulerAngles.x.ToString("f2")+"_"+myCamera.transform.eulerAngles.y.ToString("f2")+"_"+myCamera.transform.eulerAngles.z.ToString("f2")); // GUI.Label (new Rect (10, 10, Screen.width, 50),testLog); GUI.backgroundColor = Color.clear; GUIStyle centeredStyle = GUI.skin.GetStyle("Label"); centeredStyle.alignment = TextAnchor.UpperCenter; centeredStyle.fontSize = 29; // centeredStyle.font = myfont; centeredStyle.normal.textColor = Color.black; if (Main.imgString.Length > 0) { string[] images = Main.imgString.Split(new char[] { ';' }); for (int i=0; i<images.Length; i++) { string[] image = images[i].Split(new char[] { ',' }); float x = float.Parse(image[0]); float y = float.Parse(image[1]); float z = float.Parse(image[2]); int index = int.Parse(image[3]); float selfscale = float.Parse(image[4]); Vector3 screenpos = myCamera.WorldToScreenPoint(new Vector3 (x,y,z)); float poiDistanceFromCamera = Vector3.Distance(new Vector3 (x,y,z),transform.position); Texture2D texture = (Texture2D)texture_image[index]; float width = texture.width/poiDistanceFromCamera*pcToPhoneScaleImage*selfscale; float height = texture.height/poiDistanceFromCamera*pcToPhoneScaleImage*selfscale; if (index == 52 || index == 53) { GUI.DrawTexture (new Rect (screenpos.x - width / 2, Screen.height - screenpos.y - height/2, width, height), texture); } else { GUI.DrawTexture(new Rect (screenpos.x-width/2, Screen.height - screenpos.y-height, width, height), texture); } } } listPoisAlreadyInScreen = new ArrayList (); for (int i=0; i<list_display.Count; i++) { PoiClass poi = (PoiClass)list_display[i]; int level = poi.level; float lon = poi.lon; float lat = poi.lat; int type = poi.type; if (type != selectedType){ continue; } int indexInDataSource = poi.ishot; string name = poi.name; int isSelected = poi.isSelected; LonLatPoint lonlatpoint = new LonLatPoint(lon,lat); PixelPoint point = gp.lonlatToPixel (lonlatpoint,17); Vector3 screenpos = myCamera.WorldToScreenPoint(new Vector3 (-(float)point.pointX/100f,0f,(float)point.pointY/100f)); if(!isPositonInScreen(screenpos))continue;//不在屏幕内的不显示 // if(type == 100){//玩法 // if (GUI.Button (new Rect (screenpos.x - 31.5f, Screen.height - screenpos.y - 76f, 63f, 76f), texture_howToPlayBottom)) { // if(!isDraging){ // selectOnePoi(poi); // } // } // if (GUI.Button (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y-10, poi.labelLength, label_high), "")) { // if(!isDraging){ // selectOnePoi(poi); // } // } // centeredStyle.alignment = TextAnchor.UpperCenter; // centeredStyle.normal.textColor = new Color(1,1,1,1); // GUI.Label (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y-label_stroke_width-10, poi.labelLength, label_high), name,centeredStyle); // GUI.Label (new Rect (screenpos.x-poi.labelLength/2-label_stroke_width, Screen.height - screenpos.y-10, poi.labelLength, label_high), name,centeredStyle); // GUI.Label (new Rect (screenpos.x-poi.labelLength/2+label_stroke_width, Screen.height - screenpos.y-10, poi.labelLength, label_high), name,centeredStyle); // GUI.Label (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y+label_stroke_width-10, poi.labelLength, label_high), name,centeredStyle); // centeredStyle.normal.textColor = Color.black; // GUI.Label (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y-10, poi.labelLength, label_high), name,centeredStyle); // if(isSelected == 1){ // if (GUI.Button (new Rect (screenpos.x - 130f, Screen.height - screenpos.y - 76f-152, 259, 152), texture_howToPlayTop)) { // if(!isDraging){ // print ("indexInDataSource is "+indexInDataSource); //#if STANDALONE // if (Main.platform.Equals ("ios")) { // _unityCallIOS("go there|"+indexInDataSource); // }else{ // unityCallAndroid("unityCallAndroid",""); // } //#endif // } // } // centeredStyle.normal.textColor = Color.black; // centeredStyle.alignment = TextAnchor.MiddleCenter; // GUI.Label (new Rect (screenpos.x - 130f, Screen.height - screenpos.y - 76f-152, 259, 152), "到这去",centeredStyle); // } // continue; // } float poiDistanceFromCamera = Vector3.Distance(new Vector3 (-(float)point.pointX/100f,0f,(float)point.pointY/100f),transform.position); if(kav.gdLevel2UnityCameraHeight(level) > poiDistanceFromCamera){ poi.screenPosition = new Vector2(screenpos.x,Screen.height - screenpos.y); if(!calculateWhichPositionShouldPlace(poi)){ continue; } listPoisAlreadyInScreen.Add(poi); if(isSelected == 1){//big if (GUI.Button (new Rect (screenpos.x - 31.5f, Screen.height - screenpos.y - 76f, 63f, 76f), (GUIContent)texture_big[type])) { if(!isDraging){ #if STANDALONE if(type == 0){ if (Main.platform.Equals ("ios")) { _unityCallIOS("clickpoi|"+name); }else{ unityCallAndroid("unityCallAndroid",""+name); } } #endif } } if (GUI.Button (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y-10, poi.labelLength, label_high), "")) { if(!isDraging){ #if STANDALONE if(type == 0){ if (Main.platform.Equals ("ios")) { _unityCallIOS("clickpoi|"+name); }else{ unityCallAndroid("unityCallAndroid",""+name); } } #endif } } if (isPoiTypeHasLabel(type)) { centeredStyle.alignment = TextAnchor.UpperCenter; centeredStyle.normal.textColor = new Color(1,1,1,1); GUI.Label (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y-label_stroke_width-10, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (screenpos.x-poi.labelLength/2-label_stroke_width, Screen.height - screenpos.y-10, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (screenpos.x-poi.labelLength/2+label_stroke_width, Screen.height - screenpos.y-10, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y+label_stroke_width-10, poi.labelLength, label_high), name,centeredStyle); centeredStyle.normal.textColor = Color.black; GUI.Label (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y-10, poi.labelLength, label_high), name,centeredStyle); } }else{//small if (GUI.Button (new Rect (screenpos.x - 22, Screen.height - screenpos.y - 22, 44, 44), (GUIContent)texture_small[type])) { if (!isDraging) { selectOnePoi (poi); #if STANDALONE if(type == 0){ if (Main.platform.Equals ("ios")) { _unityCallIOS("clickpoi|"+name); }else{ unityCallAndroid("unityCallAndroid",""+name); } } #endif } } if(isPoiTypeHasLabel(type)){ float label_position_x = 0.0f; float label_position_y = 0.0f; switch(poi.textPosition){ case 1://right label_position_x = screenpos.x + 22f; label_position_y = Screen.height - screenpos.y - label_high/2; centeredStyle.alignment = TextAnchor.MiddleLeft; break; case 2://left label_position_x = screenpos.x - 22f - poi.labelLength; label_position_y = Screen.height - screenpos.y - label_high/2; centeredStyle.alignment = TextAnchor.MiddleRight; break; case 3://top label_position_x = screenpos.x - poi.labelLength/2; label_position_y = Screen.height - screenpos.y - 22 - label_high + 10; centeredStyle.alignment = TextAnchor.LowerCenter; break; case 4://bottom label_position_x = screenpos.x - poi.labelLength/2; label_position_y = Screen.height - screenpos.y + 22 - 10; centeredStyle.alignment = TextAnchor.UpperCenter; break; default://right label_position_x = screenpos.x + 22f; label_position_y = Screen.height - screenpos.y - label_high/2; centeredStyle.alignment = TextAnchor.MiddleLeft; break; } if (GUI.Button (new Rect (label_position_x, label_position_y, poi.labelLength, label_high), "")) { if(!isDraging){ selectOnePoi(poi); #if STANDALONE if(type == 0){ if (Main.platform.Equals ("ios")) { _unityCallIOS("clickpoi|"+name); }else{ unityCallAndroid("unityCallAndroid",""+name); } } #endif } } centeredStyle.normal.textColor = new Color(1,1,1,1); GUI.Label (new Rect (label_position_x, label_position_y-label_stroke_width, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (label_position_x-label_stroke_width, label_position_y, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (label_position_x+label_stroke_width, label_position_y, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (label_position_x, label_position_y+label_stroke_width, poi.labelLength, label_high), name,centeredStyle); centeredStyle.normal.textColor = new Color(55f/255f,55f/255f,55f/255f,1); GUI.Label (new Rect (label_position_x, label_position_y, poi.labelLength, label_high), name,centeredStyle); } } } } //location LonLatPoint myLoc = new LonLatPoint(myLocationLon,myLocationLat); PixelPoint myLocPoint = gp.lonlatToPixel (myLoc,17); Vector3 myLocScreenPoint = myCamera.WorldToScreenPoint(new Vector3 (-(float)myLocPoint.pointX/100f,0f,(float)myLocPoint.pointY/100f)); GUIUtility.RotateAroundPivot (90f-(transform.eulerAngles.y-180)+heading, new Vector2(myLocScreenPoint.x,Screen.height - myLocScreenPoint.y)); if(isPositonInScreen(myLocScreenPoint)){//不在屏幕内的不显示 GUI.DrawTexture(new Rect (myLocScreenPoint.x-40, Screen.height - myLocScreenPoint.y-40, 80, 80), texture_myLoc); } }
void zywx_setCameraPositionByBounds(string message){ print ("unity:zywx_setCameraPositionByBounds:" + message); string[] str = message.Split(new char[] { ',' }); float minlat = float.Parse(str[0]); float maxlat = float.Parse(str[1]); float minlon = float.Parse(str[2]); float maxlon = float.Parse(str[3]); LonLatPoint lonlatpoint1 = new LonLatPoint(minlon,minlat); PixelPoint point1 = gp.lonlatToPixel (lonlatpoint1,17); LonLatPoint lonlatpoint2 = new LonLatPoint(maxlon,minlat); PixelPoint point2 = gp.lonlatToPixel (lonlatpoint2,17); float yOffsize = Mathf.Abs ((float)point1.pointX / 100f - (float)point2.pointX / 100f); float cameraHigh = yOffsize / Mathf.Tan (DegreetoRadians(30)); print ("cameraHigh is "+cameraHigh); if (cameraHigh > Main.fitHigh) cameraHigh = Main.fitHigh; zywx_moveToLocation((minlon+maxlon)/2,(minlat+maxlat)/2,false,false,cameraHigh,10); print ("unity:zywx_setCameraPositionByBounds:success" ); }
// Update is called once per frame void Update() { gps_info = "手机实际位置: " + Input.location.lastData.longitude + "," + Input.location.lastData.latitude; testBallLoc = "小球位置: " + testLon + "," + testLat; if (baseLat > 1 && baseLon > 1) //start follow { testLon += Input.location.lastData.longitude - baseLon; testLat += Input.location.lastData.latitude - baseLat; baseLon = Input.location.lastData.longitude; baseLat = Input.location.lastData.latitude; } LonLatPoint lonlatpoint = new LonLatPoint(testLon, testLat); PixelPoint point = gp.lonlatToPixel(lonlatpoint, 17); controller.SimpleMove(controller.transform.InverseTransformPoint((new Vector3(-(float)point.pointX / 100f, 0.8f, (float)point.pointY / 100f)))); // LonLatPoint mylonlatpoint = new LonLatPoint(Input.location.lastData.longitude,Input.location.lastData.latitude); // PixelPoint mypoint = gp.lonlatToPixel (mylonlatpoint,17); // controller.SimpleMove (controller.transform.InverseTransformPoint((new Vector3 (-(float)mypoint.pointX/100f,0.8f,(float)mypoint.pointY/100f)))); // print ("x:"+(-(float)mypoint.pointX/100f)+" y:"+(float)mypoint.pointY/100f); if (isRotateBack) // { Vector3 centerPoint = new Vector3(Screen.width / 2, Screen.height / 2, transform.position.y / Mathf.Sin(DegreetoRadians(transform.eulerAngles.x))); float angleBefore = transform.eulerAngles.y; float rotateSpeed; if (angleBefore > 180) { rotateSpeed = -50.0f * Time.deltaTime; } else { rotateSpeed = 50.0f * Time.deltaTime; } transform.RotateAround(camera.ScreenToWorldPoint(centerPoint), new Vector3(0, 1, 0), rotateSpeed); for (int i = 0; i <= 12; i++) { texts[i].transform.Rotate(Vector3.forward * rotateSpeed); } float angleAfter = transform.eulerAngles.y; print("angleBefore is " + angleBefore + " angleAfter is " + angleAfter); if (angleBefore <= 180 && angleAfter > 180) { isRotateBack = false; hasRotated = false; // _unityCallIOS("back"); } print("transform.eulerAngles.y is " + transform.eulerAngles.y); } speed = transform.position.y / scale; if (Input.touchCount == 1) { if (Input.GetTouch(0).phase == TouchPhase.Began) //屏幕点击事件 { touchBefore = Input.GetTouch(0).position; Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { print("click:" + hit.transform.gameObject.name); clickMessage = "点击:" + kav.getValueFromkey(hit.transform.gameObject.name); } currentGesture = ""; } if (Input.GetTouch(0).phase == TouchPhase.Moved) { if (!currentGesture.Equals("")) { return; } Vector2 touchAfter = Input.GetTouch(0).position; Vector2 touchDeltaPosition = touchAfter - touchBefore; float lengthScreen = touchDeltaPosition.magnitude; Vector3 touchAfterToWorld = camera.ScreenToWorldPoint(new Vector3(touchAfter.x, touchAfter.y, transform.position.y / Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)))); Vector3 touchBeforeToWorld = camera.ScreenToWorldPoint(new Vector3(touchBefore.x, touchBefore.y, transform.position.y / Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)))); Vector3 deltaWorld = touchAfterToWorld - touchBeforeToWorld; float lengthWorld = deltaWorld.magnitude; float scaleFromSceenToWorld = lengthWorld / lengthScreen; float y_weight = -touchDeltaPosition.y * Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); float z_weight = -touchDeltaPosition.y * Mathf.Cos(DegreetoRadians(transform.eulerAngles.x)); transform.Translate(-touchDeltaPosition.x * scaleFromSceenToWorld, y_weight * scaleFromSceenToWorld, z_weight * scaleFromSceenToWorld); touchBefore = touchAfter; } } else if (Input.touchCount == 2) { Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1); if (touchZero.phase == TouchPhase.Began || touchOne.phase == TouchPhase.Began) { touch0before = Input.GetTouch(0).position; touch1before = Input.GetTouch(1).position; currentGesture = "begin"; gestureList.Clear(); // print("coor0:"+testCoorString0); // print("coor1:"+testCoorString1); testCoorString0 = ""; testCoorString1 = ""; } else if (touchZero.phase == TouchPhase.Moved && touchOne.phase == TouchPhase.Moved) { testCoorString0 += touchZero.position + "\n"; testCoorString1 += touchOne.position + "\n"; //判断之前和之后,两点间的距离的变化是否是巨大的 float pinchDistance = Vector2.Distance(touchZero.position, touchOne.position); float prevDistance = Vector2.Distance(touch0before, touch1before); pinchDistanceDelta = pinchDistance - prevDistance; print("pinchDistanceDelta is " + pinchDistanceDelta); if (Mathf.Abs(pinchDistanceDelta) > minPinchDistance) { isDistanceChangeHuge = true; } else { isDistanceChangeHuge = false; } // Vector2 touch0before = touchZero.position - touchZero.deltaPosition; // Vector2 touch1before = touchOne.position - touchOne.deltaPosition; Vector2 vectorbefore01 = new Vector2(touch1before.x - touch0before.x, touch1before.y - touch0before.y); float angleZero = VectorAngle(vectorbefore01, touchZero.position - touch0before); Vector2 vectorbefore10 = new Vector2(touch0before.x - touch1before.x, touch0before.y - touch1before.y); float angleOne = VectorAngle(vectorbefore10, touchOne.position - touch1before); if (angleZero * angleOne > 0 && Mathf.Abs(angleZero) > 90 - angleRangeOfRotate / 2 && Mathf.Abs(angleZero) < 90 + angleRangeOfRotate / 2 && Mathf.Abs(angleOne) > 90 - angleRangeOfRotate / 2 && Mathf.Abs(angleOne) < 90 + angleRangeOfRotate / 2) { isRotate = true; } else { isRotate = false; } if (isRotate) { if (currentGesture.Equals("begin")) { gestureList.Add("rotate"); if (isContinuousSameGesture("rotate")) //连续三个rotate { currentGesture = "rotate"; } else { touch0before = touchZero.position; touch1before = touchOne.position; return; } } else { if (!currentGesture.Equals("rotate")) //zoom or updown { touch0before = touchZero.position; touch1before = touchOne.position; return; } } print("---------rotate_camera"); Vector2 vectorAfter01 = new Vector2(touchOne.position.x - touchZero.position.x, touchOne.position.y - touchZero.position.y); float rotateAngle = VectorAngle(vectorbefore01, vectorAfter01); Vector3 centerPoint = new Vector3((touch0before.x + touch1before.x) / 2, (touch0before.y + touch1before.y) / 2, transform.position.y / Mathf.Sin(DegreetoRadians(transform.eulerAngles.x))); transform.RotateAround(camera.ScreenToWorldPoint(centerPoint), new Vector3(0, 1, 0), -rotateAngle); print("centerPoint is " + camera.ScreenToWorldPoint(centerPoint)); hasRotated = true; //text rotate // text.transform.Rotate(Vector3.forward*rotateAngle); for (int i = 0; i <= 12; i++) { texts[i].transform.Rotate(Vector3.forward * (-rotateAngle)); } } else { if (isDistanceChangeHuge) { if (currentGesture.Equals("begin")) { gestureList.Add("zoom"); if (isContinuousSameGesture("zoom")) //连续三个zoom { currentGesture = "zoom"; } else { touch0before = touchZero.position; touch1before = touchOne.position; return; } } else { if (!currentGesture.Equals("zoom")) //rotate or updown { touch0before = touchZero.position; touch1before = touchOne.position; return; } } print("------------zoom"); Vector2 touchZeroPrevPos = touchZero.position - (touchZero.position - touch0before); Vector2 touchOnePrevPos = touchOne.position - (touchOne.position - touch1before); // Find the magnitude of the vector (the distance) between the touches in each frame. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; // Find the difference in the distances between each frame. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; print("height:" + transform.position.y); if (deltaMagnitudeDiff > 0) //zoom out { if (transform.position.y > maxHigh) { print("too high"); hasUpDown = false; //call ios native function back touch0before = touchZero.position; touch1before = touchOne.position; return; } } else //zoom in { if (transform.position.y < minHigh) { print("too low"); touch0before = touchZero.position; touch1before = touchOne.position; return; } } float h1 = transform.position.y; transform.Translate(-Vector3.forward * deltaMagnitudeDiff * speed); if (transform.position.y > maxHigh * 9 / 10) { supportRotate = false; if (h1 <= maxHigh * 9 / 10) //转回来 { startRotateBack(); hasUpDown = false; } } else { supportRotate = true; } print("after height:" + transform.position.y); float h2 = transform.position.y; float angle1 = 90.0f - transform.eulerAngles.x; if (deltaMagnitudeDiff > 0) //zoom out { if (transform.eulerAngles.x < getMaxAngleByHeight()) { print("transform.eulerAngles.x is " + transform.eulerAngles.x + " getMaxAngleByHeight is " + getMaxAngleByHeight()); Vector3 cameraLeftWorldVector = transform.TransformDirection(Vector3.left); float anglecorret = getMaxAngleByHeight() - transform.eulerAngles.x; transform.RotateAround(transform.position, cameraLeftWorldVector, -anglecorret); float dis1 = Mathf.Tan(DegreetoRadians(angle1)) * h2; float dis2 = Mathf.Tan(DegreetoRadians(angle1 - anglecorret)) * h2; float dis = dis1 - dis2; float y_weight = dis * Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); float z_weight = dis * Mathf.Cos(DegreetoRadians(transform.eulerAngles.x)); transform.Translate(0, y_weight, z_weight); } } else //zoom in { if (!hasUpDown) { Vector3 cameraLeftWorldVector = transform.TransformDirection(Vector3.left); float anglecorret = transform.eulerAngles.x - getMaxAngleByHeight(); print("transform.eulerAngles.x is " + transform.eulerAngles.x); print("getMaxAngleByHeight() is " + getMaxAngleByHeight()); print("anglecorret is " + anglecorret); transform.RotateAround(transform.position, cameraLeftWorldVector, anglecorret); float dis1 = Mathf.Tan(DegreetoRadians(angle1)) * h2; float dis2 = Mathf.Tan(DegreetoRadians(angle1 + anglecorret)) * h2; float dis = dis2 - dis1; float y_weight = dis * Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); float z_weight = dis * Mathf.Cos(DegreetoRadians(transform.eulerAngles.x)); transform.Translate(0, -y_weight, -z_weight); } } } else { if (currentGesture.Equals("begin")) { gestureList.Add("updown"); if (isContinuousSameGesture("updown")) //连续三个updown { currentGesture = "updown"; } else { touch0before = touchZero.position; touch1before = touchOne.position; return; } } else { if (!currentGesture.Equals("updown")) //zoom or rotate { touch0before = touchZero.position; touch1before = touchOne.position; return; } } print("look up and down"); float angle = (touchZero.position - touch0before).y * 90.0f / Screen.height; if (angle > 0) //look up { if (transform.eulerAngles.x < getMaxAngleByHeight()) { print("can not look up anymore"); touch0before = touchZero.position; touch1before = touchOne.position; return; } } else { if (transform.eulerAngles.x > maxAngle) { print("can not look down anymore"); touch0before = touchZero.position; touch1before = touchOne.position; return; } } if (transform.eulerAngles.x - angle > maxAngle) //防止倒过来看 { angle = transform.eulerAngles.x - maxAngle; } Vector3 cameraLeftWorldVector = transform.TransformDirection(Vector3.left); Vector3 centerPoint = new Vector3(Screen.width / 2, Screen.height / 2, transform.position.y / Mathf.Sin(DegreetoRadians(transform.eulerAngles.x))); transform.RotateAround(camera.ScreenToWorldPoint(centerPoint), cameraLeftWorldVector, angle); hasUpDown = true; } } touch0before = touchZero.position; touch1before = touchOne.position; } } }
public PixelPoint lonlatToPixel(LonLatPoint llp, int level) { LonLatPoint mcP = lonlatToMercator(llp, level); return new PixelPoint((long) mcP.lon - offsetX, (long) mcP.lat - offsetY); }
void OnGUI() { GUIStyle centeredStyle = GUI.skin.GetStyle("Label"); centeredStyle.alignment = TextAnchor.MiddleCenter; centeredStyle.fontSize = 12; centeredStyle.font = myfont; centeredStyle.normal.textColor = Color.black; for (int i=0; i<listImage.Count; i++) { ImageClass image = (ImageClass)listImage[i]; float x = image.worldLocation.x; float y = 0; float z = image.worldLocation.z; Vector3 location = new Vector3(x,y,z); int index = image.imgIndex; Vector3 screenpos = myCamera.WorldToScreenPoint(location); // print("screenpos is "+screenpos); float poiDistanceFromCamera = Vector3.Distance(location,transform.position); Texture2D texture = (Texture2D)texture_image[index]; float selfscale = image.scale; float width = texture.width/poiDistanceFromCamera*imageScale*selfscale; float height = texture.height/poiDistanceFromCamera*imageScale*selfscale; if (index == 52 || index == 53) { GUI.DrawTexture (new Rect (screenpos.x - width / 2, Screen.height - screenpos.y - height / 2, width, height), texture); if (displayIndex) { GUI.Label (new Rect (screenpos.x - width / 2, Screen.height - screenpos.y - height / 2, width, height), "" + i); } } else { GUI.DrawTexture(new Rect (screenpos.x-width/2, Screen.height - screenpos.y-height, width, height), texture); if(displayIndex){ GUI.Label(new Rect (screenpos.x-width/2, Screen.height - screenpos.y-height/2, width, height),""+i); } } } int n = 0; int offsizex = 0; int offsizey = 0; for (n = 0; n<listImageContent.Count; n++) { if(n == selectedButton){ GUI.backgroundColor = Color.red; }else{ GUI.backgroundColor = Color.white; } if (GUI.Button (new Rect (10+offsizex, 10+offsizey, 50, 50), (GUIContent) listImageContent[n])) { selectedButton = n; } offsizex += 60; if(n > 1 && (n+1)%20 == 0){ offsizex = 0; offsizey += 60; } } // if (GUI.Button (new Rect (Screen.width - 90, 10, 80, 50), "删除上一个")) { // if(listImage.Count > 0){ // listImage.RemoveAt(listImage.Count-1); // } // } // if (GUI.Button (new Rect (Screen.width - 90 - 90, 10, 80, 50), statedes)) { // if(adding){ // adding = false; // statedes = "正常点击"; // }else{ // adding = true; // statedes = "正在增加"; // } // } centeredStyle.fontSize = 15; centeredStyle.alignment = TextAnchor.MiddleLeft; userScale = GUI.TextField(new Rect(Screen.width - 90 - 90, 10, 80, 50),userScale,15); if (GUI.Button (new Rect (Screen.width - 90, 10, 80, 50), "save")) { string output = ""; for(int i=0;i<listImage.Count;i++){ ImageClass image = (ImageClass)listImage[i]; output += image.worldLocation.x + ","+0+","+image.worldLocation.z+","+image.imgIndex+","+image.scale+";"; } print(output); } if (GUI.Button (new Rect (Screen.width - 90, 70, 80, 50), "指定删除")) { listImage.RemoveAt(int.Parse(deleteIndex)); GUI.SetNextControlName(""); GUI.FocusControl(""); } deleteIndex = GUI.TextField(new Rect(Screen.width - 90 - 90, 70, 80, 50),deleteIndex,15); centeredStyle.alignment = TextAnchor.UpperLeft; GUI.Label(new Rect(10,190,300,200),"使用说明:4个方向键,可以移动场景\nwsad:微调场景\nz:缩小场景,x:放大场景\nF1:影藏/显示 图片的编号\nF2:影藏/显示poi\nQ:删除上一个"); if (!displayPoi) return; //data GUI.backgroundColor = Color.clear; centeredStyle.alignment = TextAnchor.UpperCenter; centeredStyle.fontSize = 29; listPoisAlreadyInScreen = new ArrayList (); for (int i=0; i<list_display.Count; i++) { PoiClass poi = (PoiClass)list_display [i]; int level = poi.level; float lon = poi.lon; float lat = poi.lat; int type = poi.type; string name = poi.name; int isSelected = poi.isSelected; LonLatPoint lonlatpoint = new LonLatPoint (lon, lat); PixelPoint point = gp.lonlatToPixel (lonlatpoint, 17); Vector3 screenpos = myCamera.WorldToScreenPoint (new Vector3 (-(float)point.pointX / 100f, 0f, (float)point.pointY / 100f)); if (!isPositonInScreen (screenpos)) continue;//不在屏幕内的不显示 float poiDistanceFromCamera = Vector3.Distance (new Vector3 (-(float)point.pointX / 100f, 0f, (float)point.pointY / 100f), transform.position); if (kav.gdLevel2UnityCameraHeight (level) > poiDistanceFromCamera) { poi.screenPosition = new Vector2 (screenpos.x, Screen.height - screenpos.y); if (!calculateWhichPositionShouldPlace (poi)) { continue; } listPoisAlreadyInScreen.Add (poi); if (type == selectedType || isSelected == 1) {//big if (GUI.Button (new Rect (screenpos.x - 31.5f, Screen.height - screenpos.y - 76f, 63f, 76f), (GUIContent)texture_big [type])) { } if (GUI.Button (new Rect (screenpos.x - poi.labelLength / 2, Screen.height - screenpos.y - 10, poi.labelLength, label_high), "")) { } centeredStyle.alignment = TextAnchor.UpperCenter; centeredStyle.normal.textColor = new Color (1, 1, 1, 1); GUI.Label (new Rect (screenpos.x - poi.labelLength / 2, Screen.height - screenpos.y - label_stroke_width - 10, poi.labelLength, label_high), name, centeredStyle); GUI.Label (new Rect (screenpos.x - poi.labelLength / 2 - label_stroke_width, Screen.height - screenpos.y - 10, poi.labelLength, label_high), name, centeredStyle); GUI.Label (new Rect (screenpos.x - poi.labelLength / 2 + label_stroke_width, Screen.height - screenpos.y - 10, poi.labelLength, label_high), name, centeredStyle); GUI.Label (new Rect (screenpos.x - poi.labelLength / 2, Screen.height - screenpos.y + label_stroke_width - 10, poi.labelLength, label_high), name, centeredStyle); centeredStyle.normal.textColor = Color.black; GUI.Label (new Rect (screenpos.x - poi.labelLength / 2, Screen.height - screenpos.y - 10, poi.labelLength, label_high), name, centeredStyle); } else {//small if (GUI.Button (new Rect (screenpos.x - 22, Screen.height - screenpos.y - 22, 44, 44), (GUIContent)texture_small [type])) { selectOnePoi (poi); } if (type == 0 || type == 1 || type == 2) { float label_position_x = 0.0f; float label_position_y = 0.0f; switch (poi.textPosition) { case 1://right label_position_x = screenpos.x + 22f; label_position_y = Screen.height - screenpos.y - label_high / 2; centeredStyle.alignment = TextAnchor.MiddleLeft; break; case 2://left label_position_x = screenpos.x - 22f - poi.labelLength; label_position_y = Screen.height - screenpos.y - label_high / 2; centeredStyle.alignment = TextAnchor.MiddleRight; break; case 3://top label_position_x = screenpos.x - poi.labelLength / 2; label_position_y = Screen.height - screenpos.y - 22 - label_high + 10; centeredStyle.alignment = TextAnchor.LowerCenter; break; case 4://bottom label_position_x = screenpos.x - poi.labelLength / 2; label_position_y = Screen.height - screenpos.y + 22 - 10; centeredStyle.alignment = TextAnchor.UpperCenter; break; default://right label_position_x = screenpos.x + 22f; label_position_y = Screen.height - screenpos.y - label_high / 2; centeredStyle.alignment = TextAnchor.MiddleLeft; break; } if (GUI.Button (new Rect (label_position_x, label_position_y, poi.labelLength, label_high), "")) { selectOnePoi (poi); } centeredStyle.normal.textColor = new Color (1, 1, 1, 1); GUI.Label (new Rect (label_position_x, label_position_y - label_stroke_width, poi.labelLength, label_high), name, centeredStyle); GUI.Label (new Rect (label_position_x - label_stroke_width, label_position_y, poi.labelLength, label_high), name, centeredStyle); GUI.Label (new Rect (label_position_x + label_stroke_width, label_position_y, poi.labelLength, label_high), name, centeredStyle); GUI.Label (new Rect (label_position_x, label_position_y + label_stroke_width, poi.labelLength, label_high), name, centeredStyle); centeredStyle.normal.textColor = Color.black; GUI.Label (new Rect (label_position_x, label_position_y, poi.labelLength, label_high), name, centeredStyle); } } } } }
// Update is called once per frame void Update() { gps_info = "手机实际位置: " + Input.location.lastData.longitude + "," + Input.location.lastData.latitude; testBallLoc = "小球位置: " + testLon + "," + testLat; if (baseLat > 1 && baseLon > 1) {//start follow testLon += Input.location.lastData.longitude - baseLon; testLat += Input.location.lastData.latitude -baseLat; baseLon = Input.location.lastData.longitude; baseLat = Input.location.lastData.latitude; } LonLatPoint lonlatpoint = new LonLatPoint(testLon,testLat); PixelPoint point = gp.lonlatToPixel (lonlatpoint,17); controller.SimpleMove (controller.transform.InverseTransformPoint((new Vector3 (-(float)point.pointX/100f,0.8f,(float)point.pointY/100f)))); // LonLatPoint mylonlatpoint = new LonLatPoint(Input.location.lastData.longitude,Input.location.lastData.latitude); // PixelPoint mypoint = gp.lonlatToPixel (mylonlatpoint,17); // controller.SimpleMove (controller.transform.InverseTransformPoint((new Vector3 (-(float)mypoint.pointX/100f,0.8f,(float)mypoint.pointY/100f)))); // print ("x:"+(-(float)mypoint.pointX/100f)+" y:"+(float)mypoint.pointY/100f); if (isRotateBack) {// Vector3 centerPoint = new Vector3(Screen.width/2,Screen.height/2,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x))); float angleBefore = transform.eulerAngles.y; float rotateSpeed; if(angleBefore > 180){ rotateSpeed = -50.0f*Time.deltaTime; }else{ rotateSpeed = 50.0f*Time.deltaTime; } transform.RotateAround (camera.ScreenToWorldPoint(centerPoint), new Vector3(0,1,0), rotateSpeed); for (int i=0; i<=12; i++) { texts[i].transform.Rotate(Vector3.forward*rotateSpeed); } float angleAfter = transform.eulerAngles.y; print("angleBefore is "+angleBefore+" angleAfter is "+angleAfter); if(angleBefore <= 180 && angleAfter > 180){ isRotateBack = false; hasRotated = false; // _unityCallIOS("back"); } print("transform.eulerAngles.y is "+transform.eulerAngles.y); } speed = transform.position.y/scale; if (Input.touchCount == 1) { if (Input.GetTouch (0).phase == TouchPhase.Began) {//屏幕点击事件 touchBefore = Input.GetTouch (0).position; Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray,out hit)) { print("click:"+hit.transform.gameObject.name); clickMessage = "点击:" + kav.getValueFromkey(hit.transform.gameObject.name); } currentGesture = ""; } if (Input.GetTouch (0).phase == TouchPhase.Moved) { if(!currentGesture.Equals("")){ return; } Vector2 touchAfter = Input.GetTouch (0).position; Vector2 touchDeltaPosition = touchAfter - touchBefore; float lengthScreen = touchDeltaPosition.magnitude; Vector3 touchAfterToWorld = camera.ScreenToWorldPoint(new Vector3(touchAfter.x,touchAfter.y,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)))); Vector3 touchBeforeToWorld = camera.ScreenToWorldPoint(new Vector3(touchBefore.x,touchBefore.y,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)))); Vector3 deltaWorld = touchAfterToWorld - touchBeforeToWorld; float lengthWorld = deltaWorld.magnitude; float scaleFromSceenToWorld = lengthWorld/lengthScreen; float y_weight = -touchDeltaPosition.y*Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); float z_weight = -touchDeltaPosition.y*Mathf.Cos(DegreetoRadians(transform.eulerAngles.x)); transform.Translate (-touchDeltaPosition.x * scaleFromSceenToWorld, y_weight * scaleFromSceenToWorld, z_weight*scaleFromSceenToWorld ); touchBefore = touchAfter; } } else if (Input.touchCount == 2) { Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1); if(touchZero.phase == TouchPhase.Began || touchOne.phase == TouchPhase.Began){ touch0before = Input.GetTouch(0).position; touch1before = Input.GetTouch(1).position; currentGesture = "begin"; gestureList.Clear(); // print("coor0:"+testCoorString0); // print("coor1:"+testCoorString1); testCoorString0 = ""; testCoorString1 = ""; }else if (touchZero.phase == TouchPhase.Moved && touchOne.phase == TouchPhase.Moved) { testCoorString0 += touchZero.position+"\n"; testCoorString1 += touchOne.position+"\n"; //判断之前和之后,两点间的距离的变化是否是巨大的 float pinchDistance = Vector2.Distance(touchZero.position, touchOne.position); float prevDistance = Vector2.Distance(touch0before,touch1before); pinchDistanceDelta = pinchDistance - prevDistance; print("pinchDistanceDelta is "+pinchDistanceDelta); if (Mathf.Abs(pinchDistanceDelta) > minPinchDistance) { isDistanceChangeHuge = true; }else{ isDistanceChangeHuge = false; } // Vector2 touch0before = touchZero.position - touchZero.deltaPosition; // Vector2 touch1before = touchOne.position - touchOne.deltaPosition; Vector2 vectorbefore01 = new Vector2(touch1before.x-touch0before.x,touch1before.y-touch0before.y); float angleZero = VectorAngle(vectorbefore01, touchZero.position - touch0before); Vector2 vectorbefore10 = new Vector2(touch0before.x-touch1before.x,touch0before.y-touch1before.y); float angleOne = VectorAngle(vectorbefore10, touchOne.position - touch1before); if(angleZero * angleOne > 0 && Mathf.Abs(angleZero) > 90-angleRangeOfRotate/2 && Mathf.Abs(angleZero) < 90+angleRangeOfRotate/2 && Mathf.Abs(angleOne) > 90-angleRangeOfRotate/2 && Mathf.Abs(angleOne) < 90+angleRangeOfRotate/2){ isRotate = true; }else{ isRotate = false; } if(isRotate){ if(currentGesture.Equals("begin")){ gestureList.Add("rotate"); if(isContinuousSameGesture("rotate")){//连续三个rotate currentGesture = "rotate"; }else{ touch0before = touchZero.position; touch1before = touchOne.position; return; } }else{ if(!currentGesture.Equals("rotate")){//zoom or updown touch0before = touchZero.position; touch1before = touchOne.position; return; } } print("---------rotate_camera"); Vector2 vectorAfter01 = new Vector2(touchOne.position.x-touchZero.position.x,touchOne.position.y-touchZero.position.y); float rotateAngle = VectorAngle(vectorbefore01, vectorAfter01); Vector3 centerPoint = new Vector3((touch0before.x+touch1before.x)/2,(touch0before.y+touch1before.y)/2,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x))); transform.RotateAround (camera.ScreenToWorldPoint(centerPoint),new Vector3(0,1,0) , -rotateAngle); print("centerPoint is "+camera.ScreenToWorldPoint(centerPoint)); hasRotated = true; //text rotate // text.transform.Rotate(Vector3.forward*rotateAngle); for (int i=0; i<=12; i++) { texts[i].transform.Rotate(Vector3.forward*(-rotateAngle)); } }else{ if(isDistanceChangeHuge){ if(currentGesture.Equals("begin")){ gestureList.Add("zoom"); if(isContinuousSameGesture("zoom")){//连续三个zoom currentGesture = "zoom"; }else{ touch0before = touchZero.position; touch1before = touchOne.position; return; } }else{ if(!currentGesture.Equals("zoom")){//rotate or updown touch0before = touchZero.position; touch1before = touchOne.position; return; } } print("------------zoom"); Vector2 touchZeroPrevPos = touchZero.position - (touchZero.position - touch0before); Vector2 touchOnePrevPos = touchOne.position - (touchOne.position - touch1before); // Find the magnitude of the vector (the distance) between the touches in each frame. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; // Find the difference in the distances between each frame. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; print ("height:"+transform.position.y); if(deltaMagnitudeDiff > 0){//zoom out if(transform.position.y > maxHigh){ print("too high"); hasUpDown = false; //call ios native function back touch0before = touchZero.position; touch1before = touchOne.position; return; } }else{//zoom in if(transform.position.y < minHigh){ print("too low"); touch0before = touchZero.position; touch1before = touchOne.position; return; } } float h1 = transform.position.y; transform.Translate(-Vector3.forward*deltaMagnitudeDiff*speed); if(transform.position.y > maxHigh*9/10){ supportRotate = false; if(h1 <= maxHigh*9/10){//转回来 startRotateBack(); hasUpDown = false; } }else{ supportRotate = true; } print ("after height:"+transform.position.y); float h2 = transform.position.y; float angle1 = 90.0f-transform.eulerAngles.x; if(deltaMagnitudeDiff > 0){//zoom out if(transform.eulerAngles.x < getMaxAngleByHeight()){ print("transform.eulerAngles.x is "+transform.eulerAngles.x+" getMaxAngleByHeight is "+getMaxAngleByHeight()); Vector3 cameraLeftWorldVector = transform.TransformDirection (Vector3.left); float anglecorret = getMaxAngleByHeight()-transform.eulerAngles.x; transform.RotateAround (transform.position, cameraLeftWorldVector,-anglecorret); float dis1 = Mathf.Tan(DegreetoRadians(angle1))*h2; float dis2 = Mathf.Tan(DegreetoRadians(angle1-anglecorret))*h2; float dis = dis1 - dis2; float y_weight = dis*Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); float z_weight = dis*Mathf.Cos(DegreetoRadians(transform.eulerAngles.x)); transform.Translate (0, y_weight, z_weight ); } }else{//zoom in if(!hasUpDown){ Vector3 cameraLeftWorldVector = transform.TransformDirection (Vector3.left); float anglecorret = transform.eulerAngles.x - getMaxAngleByHeight(); print ("transform.eulerAngles.x is "+transform.eulerAngles.x); print ("getMaxAngleByHeight() is "+getMaxAngleByHeight()); print ("anglecorret is "+anglecorret); transform.RotateAround (transform.position, cameraLeftWorldVector,anglecorret); float dis1 = Mathf.Tan(DegreetoRadians(angle1))*h2; float dis2 = Mathf.Tan(DegreetoRadians(angle1+anglecorret))*h2; float dis = dis2 - dis1; float y_weight = dis*Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); float z_weight = dis*Mathf.Cos(DegreetoRadians(transform.eulerAngles.x)); transform.Translate (0, -y_weight, -z_weight ); } } }else{ if(currentGesture.Equals("begin")){ gestureList.Add("updown"); if(isContinuousSameGesture("updown")){//连续三个updown currentGesture = "updown"; }else{ touch0before = touchZero.position; touch1before = touchOne.position; return; } }else{ if(!currentGesture.Equals("updown")){//zoom or rotate touch0before = touchZero.position; touch1before = touchOne.position; return; } } print("look up and down"); float angle = (touchZero.position - touch0before).y*90.0f / Screen.height; if(angle > 0){//look up if(transform.eulerAngles.x < getMaxAngleByHeight()){ print("can not look up anymore"); touch0before = touchZero.position; touch1before = touchOne.position; return; } }else{ if(transform.eulerAngles.x > maxAngle){ print("can not look down anymore"); touch0before = touchZero.position; touch1before = touchOne.position; return; } } if(transform.eulerAngles.x - angle > maxAngle){//防止倒过来看 angle = transform.eulerAngles.x - maxAngle; } Vector3 cameraLeftWorldVector = transform.TransformDirection (Vector3.left); Vector3 centerPoint = new Vector3(Screen.width/2,Screen.height/2,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x))); transform.RotateAround (camera.ScreenToWorldPoint(centerPoint), cameraLeftWorldVector, angle); hasUpDown = true; } } touch0before = touchZero.position; touch1before = touchOne.position; } } }
public PixelPoint lonlatToPixel(LonLatPoint llp, int level) { LonLatPoint mcP = lonlatToMercator(llp, level); return(new PixelPoint((long)mcP.lon - offsetX, (long)mcP.lat - offsetY)); }
// Update is called once per frame void Update() { //相机正在移动 if (startMove) { transform.position = Vector3.MoveTowards(transform.position, targetPosition, step*Time.deltaTime); if(transform.position.Equals(targetPosition)){ startMove = false; if(wantWatch){//说明移动过去是为了观看 startWatch = true; wantWatch = false; } } } //相机正在旋转 if (startWatch) { float angleBefore = transform.eulerAngles.y; transform.RotateAround (aroundPosition,new Vector3(0,1,0) , 30 * Time.deltaTime); float angleAfter = transform.eulerAngles.y; if(angleBefore < 180 && angleAfter > 180){ startWatch = false; } } gps_info = "手机实际位置: " + Input.location.lastData.longitude + "," + Input.location.lastData.latitude; LonLatPoint lonlatpoint = new LonLatPoint(Input.location.lastData.longitude,Input.location.lastData.latitude); PixelPoint point = gp.lonlatToPixel (lonlatpoint,17); controller.SimpleMove (controller.transform.InverseTransformPoint((new Vector3 (-(float)point.pointX/100f,0.5f,(float)point.pointY/100f)))); // print ("position is " + new Vector3 (-(float)point.pointX / 100f, 0.5f, (float)point.pointY / 100f)); if (isRotateBack) {// Vector3 centerPoint = new Vector3(Screen.width/2,Screen.height/2,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x))); float angleBefore = transform.eulerAngles.y; float rotateSpeed; if(angleBefore > 180){ rotateSpeed = -50.0f*Time.deltaTime; }else{ rotateSpeed = 50.0f*Time.deltaTime; } transform.RotateAround (camera.ScreenToWorldPoint(centerPoint), new Vector3(0,1,0), rotateSpeed); float angleAfter = transform.eulerAngles.y; if(angleBefore <= 180 && angleAfter > 180){ isRotateBack = false; hasRotated = false; } } if (Input.touchCount == 1) { if (Input.GetTouch (0).phase == TouchPhase.Began) {//屏幕点击事件 touchBefore = Input.GetTouch (0).position; currentGesture = ""; startMove = false; startWatch = false; wantWatch = false; }else if (Input.GetTouch (0).phase == TouchPhase.Moved) { isDraging = true; if(!currentGesture.Equals("")){ return; } Vector2 touchAfter = Input.GetTouch (0).position; Vector2 touchDeltaPosition = touchAfter - touchBefore; float lengthScreen = touchDeltaPosition.magnitude; Vector3 touchAfterToWorld = camera.ScreenToWorldPoint(new Vector3(touchAfter.x,touchAfter.y,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)))); Vector3 touchBeforeToWorld = camera.ScreenToWorldPoint(new Vector3(touchBefore.x,touchBefore.y,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)))); Vector3 deltaWorld = touchAfterToWorld - touchBeforeToWorld; float lengthWorld = deltaWorld.magnitude; float scaleFromSceenToWorld = lengthWorld/lengthScreen; float y_weight = -touchDeltaPosition.y*Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); float z_weight = -touchDeltaPosition.y*Mathf.Cos(DegreetoRadians(transform.eulerAngles.x)); transform.Translate (-touchDeltaPosition.x * scaleFromSceenToWorld, y_weight * scaleFromSceenToWorld, z_weight*scaleFromSceenToWorld ); touchBefore = touchAfter; }else if (Input.GetTouch (0).phase == TouchPhase.Ended) {//屏幕点击事件 isDraging = false; } } else if (Input.touchCount == 2) { Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1); if(touchZero.phase == TouchPhase.Began || touchOne.phase == TouchPhase.Began){ touch0before = Input.GetTouch(0).position; touch1before = Input.GetTouch(1).position; currentGesture = "begin"; gestureList.Clear(); testCoorString0 = ""; testCoorString1 = ""; }else if (touchZero.phase == TouchPhase.Ended || touchOne.phase == TouchPhase.Ended) { isDraging = false; } else if (touchZero.phase == TouchPhase.Moved && touchOne.phase == TouchPhase.Moved) { isDraging = true; testCoorString0 += touchZero.position+"\n"; testCoorString1 += touchOne.position+"\n"; //判断之前和之后,两点间的距离的变化是否是巨大的 float pinchDistance = Vector2.Distance(touchZero.position, touchOne.position); float prevDistance = Vector2.Distance(touch0before,touch1before); pinchDistanceDelta = pinchDistance - prevDistance; if (Mathf.Abs(pinchDistanceDelta) > minPinchDistance) { isDistanceChangeHuge = true; }else{ isDistanceChangeHuge = false; } Vector2 vectorbefore01 = new Vector2(touch1before.x-touch0before.x,touch1before.y-touch0before.y); float angleZero = VectorAngle(vectorbefore01, touchZero.position - touch0before); Vector2 vectorbefore10 = new Vector2(touch0before.x-touch1before.x,touch0before.y-touch1before.y); float angleOne = VectorAngle(vectorbefore10, touchOne.position - touch1before); if(angleZero * angleOne > 0 && Mathf.Abs(angleZero) > 90-angleRangeOfRotate/2 && Mathf.Abs(angleZero) < 90+angleRangeOfRotate/2 && Mathf.Abs(angleOne) > 90-angleRangeOfRotate/2 && Mathf.Abs(angleOne) < 90+angleRangeOfRotate/2){ isRotate = true; }else{ isRotate = false; } if(isRotate){ if(currentGesture.Equals("begin")){ gestureList.Add("rotate"); if(isContinuousSameGesture("rotate")){//连续三个rotate currentGesture = "rotate"; }else{ touch0before = touchZero.position; touch1before = touchOne.position; return; } }else{ if(!currentGesture.Equals("rotate")){//zoom or updown touch0before = touchZero.position; touch1before = touchOne.position; return; } } Vector2 vectorAfter01 = new Vector2(touchOne.position.x-touchZero.position.x,touchOne.position.y-touchZero.position.y); float rotateAngle = VectorAngle(vectorbefore01, vectorAfter01); Vector3 centerPoint = new Vector3((touch0before.x+touch1before.x)/2,(touch0before.y+touch1before.y)/2,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x))); transform.RotateAround (camera.ScreenToWorldPoint(centerPoint),new Vector3(0,1,0) , -rotateAngle); hasRotated = true; }else{ if(isDistanceChangeHuge){ if(currentGesture.Equals("begin")){ gestureList.Add("zoom"); if(isContinuousSameGesture("zoom")){//连续三个zoom currentGesture = "zoom"; }else{ touch0before = touchZero.position; touch1before = touchOne.position; return; } }else{ if(!currentGesture.Equals("zoom")){//rotate or updown touch0before = touchZero.position; touch1before = touchOne.position; return; } } float scaleOfView = pinchDistance/prevDistance;//视野放大了几倍 Vector2 touchZeroPrevPos = touchZero.position - (touchZero.position - touch0before); Vector2 touchOnePrevPos = touchOne.position - (touchOne.position - touch1before); // Find the magnitude of the vector (the distance) between the touches in each frame. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; // Find the difference in the distances between each frame. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; // print ("height:"+transform.position.y); if(deltaMagnitudeDiff > 0){//zoom out if(transform.position.y > maxHigh){ print("too high"); hasUpDown = false; //call ios native function back touch0before = touchZero.position; touch1before = touchOne.position; if (platform.Equals ("ios")) { _unityCallIOS("back"); }else{ unityCallAndroid("unityCallAndroid","back"); } return; } }else{//zoom in if(transform.position.y < minHigh){ print("too low"); touch0before = touchZero.position; touch1before = touchOne.position; return; } } float h1 = transform.position.y; float afterh = h1 * scaleOfView; float h3 = Mathf.Abs(afterh - h1); float forwardDis = h3/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); if(deltaMagnitudeDiff > 0){//zoom out transform.Translate(-Vector3.forward*forwardDis); }else{ transform.Translate(Vector3.forward*forwardDis); } if(transform.position.y > fitHigh*9/10){ if(h1 <= fitHigh*9/10){//转回来 startRotateBack(); hasUpDown = false; } }else{ } float h2 = transform.position.y; float angle1 = 90.0f-transform.eulerAngles.x; if(deltaMagnitudeDiff > 0){//zoom out if(transform.eulerAngles.x < getMaxAngleByHeight()){ Vector3 cameraLeftWorldVector = transform.TransformDirection (Vector3.left); float anglecorret = getMaxAngleByHeight()-transform.eulerAngles.x; transform.RotateAround (transform.position, cameraLeftWorldVector,-anglecorret); float dis1 = Mathf.Tan(DegreetoRadians(angle1))*h2; float dis2 = Mathf.Tan(DegreetoRadians(angle1-anglecorret))*h2; float dis = dis1 - dis2; float y_weight = dis*Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); float z_weight = dis*Mathf.Cos(DegreetoRadians(transform.eulerAngles.x)); transform.Translate (0, y_weight, z_weight ); } }else{//zoom in if(!hasUpDown){ Vector3 cameraLeftWorldVector = transform.TransformDirection (Vector3.left); float anglecorret = transform.eulerAngles.x - getMaxAngleByHeight(); transform.RotateAround (transform.position, cameraLeftWorldVector,anglecorret); float dis1 = Mathf.Tan(DegreetoRadians(angle1))*h2; float dis2 = Mathf.Tan(DegreetoRadians(angle1+anglecorret))*h2; float dis = dis2 - dis1; float y_weight = dis*Mathf.Sin(DegreetoRadians(transform.eulerAngles.x)); float z_weight = dis*Mathf.Cos(DegreetoRadians(transform.eulerAngles.x)); transform.Translate (0, -y_weight, -z_weight ); } } }else{ if(currentGesture.Equals("begin")){ gestureList.Add("updown"); if(isContinuousSameGesture("updown")){//连续三个updown currentGesture = "updown"; }else{ touch0before = touchZero.position; touch1before = touchOne.position; return; } }else{ if(!currentGesture.Equals("updown")){//zoom or rotate touch0before = touchZero.position; touch1before = touchOne.position; return; } } float angle = (touchZero.position - touch0before).y*90.0f / Screen.height; if(angle > 0){//look up if(transform.eulerAngles.x < getMaxAngleByHeight()){ print("can not look up anymore"); touch0before = touchZero.position; touch1before = touchOne.position; return; } }else{ if(transform.eulerAngles.x > maxAngle){ print("can not look down anymore"); touch0before = touchZero.position; touch1before = touchOne.position; return; } } if(transform.eulerAngles.x - angle > maxAngle){//防止倒过来看 angle = transform.eulerAngles.x - maxAngle; } Vector3 cameraLeftWorldVector = transform.TransformDirection (Vector3.left); Vector3 centerPoint = new Vector3(Screen.width/2,Screen.height/2,transform.position.y/Mathf.Sin(DegreetoRadians(transform.eulerAngles.x))); transform.RotateAround (camera.ScreenToWorldPoint(centerPoint), cameraLeftWorldVector, angle); hasUpDown = true; } } touch0before = touchZero.position; touch1before = touchOne.position; } } }
void OnGUI() { if (GUI.Button (new Rect (10, 10, 50, 20), "farward")) { transform.Translate(Vector3.forward*0.5f); } if (GUI.Button (new Rect (130, 10, 50, 20), "back")) { transform.Translate(-Vector3.forward*0.5f); } if (GUI.Button (new Rect (10, 40, 50, 20), "left")) { transform.Translate(Vector3.left*0.5f); } if (GUI.Button (new Rect (130, 40, 50, 20), "right")) { transform.Translate(-Vector3.left*0.5f); } if (GUI.Button (new Rect (70, 10, 50, 20), "up")) { transform.Translate(Vector3.up*0.5f); } if (GUI.Button (new Rect (70, 70, 50, 20), "down")) { transform.Translate(-Vector3.up*0.5f); // zywx_highlightDisplayPoiWithType("0"); } GUI.backgroundColor = Color.clear; GUIStyle centeredStyle = GUI.skin.GetStyle("Label"); centeredStyle.alignment = TextAnchor.UpperCenter; centeredStyle.fontSize = 29; centeredStyle.normal.textColor = Color.black; string[] doors = doorString.Split(new char[] { ';' }); for (int i=0; i<doors.Length; i++) { string[] door = doors[i].Split(new char[] { ',' }); float lon = float.Parse(door[0]); float lat = float.Parse(door[1]); LonLatPoint lonlatpoint = new LonLatPoint(lon,lat); PixelPoint point = gp.lonlatToPixel (lonlatpoint,17); Vector3 screenpos = Camera.main.WorldToScreenPoint(new Vector3 (-(float)point.pointX/100f,0f,(float)point.pointY/100f)); GUI.Label (new Rect (screenpos.x-texture_door.width/8, Screen.height - screenpos.y-texture_door.height/8, texture_door.width/4, texture_door.height/4), texture_door); } listPoisAlreadyInScreen = new ArrayList (); for (int i=0; i<list_display.Count; i++) { PoiClass poi = (PoiClass)list_display[i]; int level = poi.level; float lon = poi.lon; float lat = poi.lat; int type = poi.type; string name = poi.name; int isSelected = poi.isSelected; LonLatPoint lonlatpoint = new LonLatPoint(lon,lat); PixelPoint point = gp.lonlatToPixel (lonlatpoint,17); Vector3 screenpos = Camera.main.WorldToScreenPoint(new Vector3 (-(float)point.pointX/100f,0f,(float)point.pointY/100f)); if(!isPositonInScreen(screenpos))continue;//不在屏幕内的不显示 float poiDistanceFromCamera = Vector3.Distance(new Vector3 (-(float)point.pointX/100f,0f,(float)point.pointY/100f),transform.position); if(kav.gdLevel2UnityCameraHeight(level) > poiDistanceFromCamera){ poi.screenPosition = new Vector2(screenpos.x,Screen.height - screenpos.y); if(!calculateWhichPositionShouldPlace(poi)){ continue; } listPoisAlreadyInScreen.Add(poi); if(type == selectedType || isSelected == 1){//big if (GUI.Button (new Rect (screenpos.x - 31.5f, Screen.height - screenpos.y - 76f, 63f, 76f), (GUIContent)texture_big[type])) { if(!isDraging){ if (platform.Equals ("ios")) { _unityCallIOS("clickpoi|"+name); }else{ unityCallAndroid("unityCallAndroid",""+i); } } } if (GUI.Button (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y-10, poi.labelLength, label_high), "")) { if(!isDraging){ if (platform.Equals ("ios")) { _unityCallIOS("clickpoi|"+name); }else{ unityCallAndroid("unityCallAndroid",""+i); } } } centeredStyle.alignment = TextAnchor.UpperCenter; centeredStyle.normal.textColor = new Color(1,1,1,0.5f); GUI.Label (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y-2-10, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (screenpos.x-poi.labelLength/2-2, Screen.height - screenpos.y-10, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (screenpos.x-poi.labelLength/2+2, Screen.height - screenpos.y-10, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y+2-10, poi.labelLength, label_high), name,centeredStyle); centeredStyle.normal.textColor = Color.black; GUI.Label (new Rect (screenpos.x-poi.labelLength/2, Screen.height - screenpos.y-10, poi.labelLength, label_high), name,centeredStyle); }else{//small if (GUI.Button (new Rect (screenpos.x - 22, Screen.height - screenpos.y - 22, 44, 44), (GUIContent)texture_small[type])) { if(!isDraging){ selectOnePoi(poi); if (platform.Equals ("ios")) { _unityCallIOS("clickpoi|"+name); }else{ unityCallAndroid("unityCallAndroid",""+i); } } } if(type == 0 || type == 1 || type ==2){ float label_position_x = 0.0f; float label_position_y = 0.0f; switch(poi.textPosition){ case 1://right label_position_x = screenpos.x + 22f; label_position_y = Screen.height - screenpos.y - label_high/2; centeredStyle.alignment = TextAnchor.MiddleLeft; break; case 2://left label_position_x = screenpos.x - 22f - poi.labelLength; label_position_y = Screen.height - screenpos.y - label_high/2; centeredStyle.alignment = TextAnchor.MiddleRight; break; case 3://top label_position_x = screenpos.x - poi.labelLength/2; label_position_y = Screen.height - screenpos.y - 22 - label_high + 10; centeredStyle.alignment = TextAnchor.LowerCenter; break; case 4://bottom label_position_x = screenpos.x - poi.labelLength/2; label_position_y = Screen.height - screenpos.y + 22 - 10; centeredStyle.alignment = TextAnchor.UpperCenter; break; default://right label_position_x = screenpos.x + 22f; label_position_y = Screen.height - screenpos.y - label_high/2; centeredStyle.alignment = TextAnchor.MiddleLeft; break; } if (GUI.Button (new Rect (label_position_x, label_position_y, poi.labelLength, label_high), "")) { if(!isDraging){ selectOnePoi(poi); if (platform.Equals ("ios")) { _unityCallIOS("clickpoi|"+name); }else{ unityCallAndroid("unityCallAndroid",""+i); } } } centeredStyle.normal.textColor = new Color(1,1,1,0.5f); GUI.Label (new Rect (label_position_x, label_position_y-2, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (label_position_x-2, label_position_y, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (label_position_x+2, label_position_y, poi.labelLength, label_high), name,centeredStyle); GUI.Label (new Rect (label_position_x, label_position_y+2, poi.labelLength, label_high), name,centeredStyle); centeredStyle.normal.textColor = Color.black; GUI.Label (new Rect (label_position_x, label_position_y, poi.labelLength, label_high), name,centeredStyle); } } } } }