Rotating an ImageView like a compass (with the “north pole” set elsewhere)

后端 未结 3 410
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 09:11

I\'m stumped regarding how to implement a \"personal compass\", ie a compass that points to a specific bearing instead of the standard \"north pole\"... unfortunatly, my cu

3条回答
  •  误落风尘
    2020-12-07 10:10

    I spent about 40 hours one weekend trying to do this.

    Pain in the butt, hopefully I can spare you that pain.

    Ok, I am warning you, this is some ugly code. I was in a pinch to finish it, it has no naming schemes, but i tried to comment it as best as I could for you.

    It was used to locate large piles of nuts laying out in fields for storage

    Using the phones current latitude and longitude, the lat/lon of the destination, the compass sensor, and some algebra, I was able to calculate the direction to the destination.

    Lat/lon and sensor readings are pulled from the MainApplication class

    This is some of the code for arrow.class, which I used to draw an arrow on a canvas towards a direction.

        //The location you want to go to//
        //"Given North"
        double lat=0;
        double lon=0;
        //////////////////////////////////
        protected void onDraw(Canvas canvas) {
    
        //Sensor values from another class managing Sensor
        float[] v = MainApplication.getValues();
    
        //The current location of the device, retrieved from another class managing GPS
        double ourlat=  MainApplication.getLatitudeD();
        double ourlon=  MainApplication.getLongitudeD(); 
    
        //Manually calculate the direction of the pile from the device
        double a= Math.abs((lon-ourlon));
        double b= Math.abs((lat-ourlat));
        //archtangent of a/b is equal to the angle of the device from 0-degrees in the first quadrant. (Think of a unit circle)
        double thetaprime= Math.atan(a/b);
        double theta= 0;
    
        //Determine the 'quadrant' that the desired location is in
        //ASTC (All, Sin, Tan, Cos)  Determines which value is positive
        //Gotta love Highschool algebra
    
        if((latourlon)){//-+ 
            //theta is 180-thetaprime because it is in the 2nd quadrant
            theta= ((Math.PI)-thetaprime); 
    
            //subtract theta from the compass value retrieved from the sensor to get our final direction
            theta=theta - Math.toRadians(v[0]);
    
        }else if((latourlat)&&(lon>ourlon)){ //++
            //No change is needed in the first quadrant
            theta= thetaprime; 
    
            //subtract theta from the compass value retreived from the sensor to get our final direction
            theta=theta - Math.toRadians(v[0]);
    
        }else if((lat>ourlat)&&(lon

    Hopefully you can manage to read my code... If I get time, I will make it a bit prettier.

    If you need any explaining, let me know.

    -MrZander

提交回复
热议问题