指南针+图片定位

来源:互联网 发布:淘宝兼职外宣说说内容 编辑:程序博客网 时间:2024/05/16 19:50
@SuppressWarnings("deprecation")
public class CompassActivity extends Activity implements SensorListener {

    private CompassView view;
    private SensorManager sm;
    private double[] picWhere = new double[2];// 图片的经度,纬度 34/108
    private double[] myWhere = new double[2];// 我的经度,纬度
    private double jiao;
    private int jiaodu;
    private String nan, dong;
    private int bei;
    private String tmp;
    private Bitmap ima[] = new Bitmap[10];
    private int fangxiang;// 面朝方向
    private int sgx, sgy;
    private Sensor sensor;
    private SensorEventListener lsn;
    public boolean runTo = true;
    /** 感应器 */
    private static float degree0 = 0;
    private float degree1 = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();

        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();
        view = new CompassView(this, screenWidth, screenHeight);
        setContentView(view);
        if (sm != null) {
            sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);// 重力感应器
        }
        // 注册listener,第三个参数是检测的精确度
        lsn = new SensorEventListener() {
            public void onSensorChanged(SensorEvent e) {
                synchronized (this) {
                    sgx = (int) e.values[SensorManager.DATA_X];
                    sgy = (int) e.values[SensorManager.DATA_Y];
                    onStarts();
                }
            }

            public void onAccuracyChanged(Sensor s, int accuracy) {
            }
        };
        sm.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
    }

    /** 重力感应 */
    private boolean getGrivity() {
        if (sgx >= sgy && sgy >= 0) {
            return true;
        } else {
            return false;
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        // 关闭感应器
        view.removeSensorListener();
    }

    public void onSensorChanged(int sensor, float[] values) {
        if (Math.abs(values[0] - degree0) > 16
                || Math.abs(values[0] - degree1) > 10) {// 1)方向感应器的改变, 灵敏度,视野范围
            synchronized (this) {
                if (sensor == SensorManager.SENSOR_ORIENTATION) {
                    degree0 = (int) values[0];
                    degree1 = (int) values[1];
                }
                getOrientation();
            }
        }
    }

    /** 得到图片的经纬度 */
    // 东加西减// 赤道向两级增加
    private double[] getPhotoWhere() {
        // TODO
        picWhere[0] = myWhere[0] + 1;
        picWhere[1] = myWhere[1];
        return picWhere;
    }

    /**
     * 得到自己的经纬度
     *
     * @return double[] myWhere
     * */
    private double[] getLocationInfo() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            double myLongitude = location.getLatitude();// 经度
            double myLatitude = location.getLongitude();// 纬度
            myWhere[0] = myLongitude;
            myWhere[1] = myLatitude;
        }
        return myWhere;
    }

    public void onAccuracyChanged(int sensor, int accuracy) {
    }

    @Override
    protected void onResume() {
        super.onResume();
        sm.registerListener(this, SensorManager.SENSOR_ORIENTATION
                | Sensor.TYPE_ACCELEROMETER, SensorManager.SENSOR_DELAY_GAME);
    }

    private Handler checkHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:
                runTo = true;
                break;
            case 1:
                runTo = false;
                break;
            }
        }
    };

    @Override
    protected void onStop() {
        sm.unregisterListener(this);
        super.onStop();
    }

    /**
     * 图片相对自己的方向角 *********************
     *
     * @得到:(picLongitude,picLatitude)图片的经纬度 && 我的经纬度
     * @返回:相对自己的方向角
     * @经度:东加西减; 西经20度到东经160度为东半球,西经20度以西为西半球 ;
     * @纬度:赤道向两级增加;
     */
    public double getOrientation() {
        // 2)得到我的经纬度
        getLocationInfo();
        // Log.i("wop", "我的经纬度:" + myWhere[0] + "||||" + myWhere[1]);
        // 3)得到图片的经纬度
        getPhotoWhere();
        // Log.i("wop", "图片的经纬度:" + picWhere[0] + "||||" + picWhere[1]);
        /** 已正北方向为基准点 */
        // 北极和赤道除外
        double tanJiao = (picWhere[1] - myWhere[1])
                / (picWhere[0] - myWhere[0]);
        jiao = Math.atan(tanJiao) * 180 / Math.PI;
        // 弧度=角度*Math.PI/180
        // 角度=弧度*180/Math.PI
        if (picWhere[1] == myWhere[1] && picWhere[0] == myWhere[0]) {
            nan = "您所在的位置";
        }

        if (picWhere[1] - myWhere[1] > 0 && picWhere[0] == myWhere[0]) {
            bei = ((int) Math.abs(jiao) - 90);
            nan = "正北" + bei;
        }
        if (picWhere[1] - myWhere[1] < 0 && picWhere[0] == myWhere[0]) {
            bei = ((int) Math.abs(jiao) + 90);
            nan = "正南" + bei;
        }
        if (picWhere[1] == myWhere[1] && picWhere[0] - myWhere[0] > 0) {
            bei = ((int) Math.abs(jiao) + 90);
            nan = "正东" + bei;
        }
        if (picWhere[1] == myWhere[1] && picWhere[0] - myWhere[0] < 0) {
            bei = ((int) Math.abs(jiao) + 270);
            nan = "正西" + bei;
        }

        if (picWhere[1] - myWhere[1] > 0 && picWhere[0] - myWhere[0] < 0) {
            bei = ((int) Math.abs(jiao) + 270);
            nan = "西北" + bei;
        }
        if (picWhere[1] - myWhere[1] > 0 && picWhere[0] - myWhere[0] > 0) {
            bei = (90 - (int) Math.abs(jiao));
            nan = "东北" + bei;
        }
        if (picWhere[1] - myWhere[1] < 0 && picWhere[0] - myWhere[0] > 0) {
            bei = ((int) Math.abs(jiao) + 90);
            nan = "东南" + bei;
        }
        if (picWhere[1] - myWhere[1] < 0 && picWhere[0] - myWhere[0] < 0) {
            bei = (270 - (int) Math.abs(jiao));
            nan = "西南" + bei;
        }
        Log.i("where", "图片方位:" + nan);
        return bei;
    }

    /** 指南针绘图类 */
    class CompassView extends SurfaceView implements SurfaceHolder.Callback,
            SensorEventListener {
        private SurfaceHolder mholder;
        private Bitmap background1;// 指针图
        private Sensor orieSensor;// 水平感应器
        private int screenWidth = 0;
        private float degree = 0;
        private final Object LOCK = new Object();
        private int chajiao;
        private int mNumber = 1;// 加载图片数量
        private int mM = 0;// 图片加载量

        public CompassView(Context context, int screenWidth, int screenHeight) {
            super(context);
            getBitmapTo(context);
            getHolder().addCallback(this);
            this.screenWidth = screenWidth; // this.getWidth();

            sm = (SensorManager) context
                    .getSystemService(Context.SENSOR_SERVICE);
            orieSensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
            if (orieSensor != null) {
                sm.registerListener(this, orieSensor,
                        SensorManager.SENSOR_DELAY_NORMAL);
            }
        }

        private void getBitmapTo(Context context) {
            // TODO Auto-generated method stub
            background1 = BitmapFactory.decodeResource(this.getResources(),
                    R.drawable.needle);
            ima[0] = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.def_01);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            synchronized (LOCK) {
                mholder = holder;
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            synchronized (LOCK) {
                mholder = null;
            }
        }

        public void removeSensorListener() {
            sm.unregisterListener(this);
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        // 画界面
        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO
            fangxiang = (int) (event.values[0] + 90);
            jiaodu = (int) CompassActivity.degree0;
            chajiao = Math.abs((int) getOrientation()) - jiaodu;

            if (!runTo) {
                return;
            }
            if (Math.abs(event.values[1]) > 32 || // 围绕x轴的偏转(-180 to 180)
                    Math.abs(event.values[2]) > 20) { // 围绕y轴的偏转 (-90 to
                                                        // 90),
                synchronized (LOCK) {
                    if (mholder != null) {
                        Canvas can = mholder.lockCanvas();
                        if (can == null)
                            return;
                        can.drawRGB(0, 0, 0);
                        Paint paint = new Paint();
                        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
                        paint.setColor(Color.GREEN);
                        paint.setTextSize(8);
                        can.drawText("E", 548, 40, paint);
                        can.drawText("W", 508, 40, paint);
                        can.drawText("N", 528, 20, paint);
                        can.drawText("S", 528, 60, paint);
                        can.drawText("( " + getJiaodu() + ")", 480, 14, paint);
                        can.save();
                        for (mM = 0; mM < mNumber; mM++) {
                            getToDrawImage(can, paint, chajiao, jiaodu);
                        }
                        can.rotate(fangxiang, 530, 37);
                        can.drawBitmap(background1, 528, 20, null);
                        can.restore();
                        mholder.unlockCanvasAndPost(can);
                    }
                }
            } else {
                synchronized (LOCK) {
                    if (mholder != null
                            && (Math.abs(event.values[0] - degree) > 0.4)) { // 避免屏幕不动时重复绘制
                        degree = event.values[0];
                        Canvas can = mholder.lockCanvas();
                        if (can == null)
                            return;
                        can.drawRGB(0, 0, 0);
                        Paint paint = new Paint();
                        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
                        paint.setColor(Color.GREEN);
                        paint.setTextSize(8);
                        can.drawText("E", 548, 40, paint);
                        can.drawText("W", 508, 40, paint);
                        can.drawText("N-", 528, 20, paint);
                        can.drawText("S", 528, 60, paint);
                        can.drawText("( " + getJiaodu() + ")", 480, 14, paint);
                        can.save();
                        for (mM = 0; mM < mNumber; mM++) {
                            getToDrawImage(can, paint, chajiao, jiaodu);
                        }
                        can.rotate(fangxiang, 530, 37);
                        can.drawBitmap(background1, 528, 20, null);
                        can.restore();
                        mholder.unlockCanvasAndPost(can);
                    }
                }
            }
        }

        private void getToDrawImage(Canvas can, Paint paint, int chajiao,
                int jiaodu) {
            // TODO Auto-generated method stub
            Log.i("draw", "mM...." + mM);
            int jjjj = 0;
            if (jiaodu < 0) {
                jiaodu = 0;
            }
            if (jiaodu >= 0 && jiaodu < 315) {
                jjjj = chajiao * 6;
                can.drawText("image", screenWidth / 2 - ima[0].getWidth() / 2
                        + jjjj + 20, 200, paint);
                can.drawBitmap(ima[0], screenWidth / 2 - ima[0].getWidth() / 2
                        + jjjj, 120, null);
            }
            if (jiaodu >= 315 && jiaodu <= 360) {
                jjjj = (360 + chajiao) * 6;
                can.drawText("image", screenWidth / 2 - ima[0].getWidth() / 2
                        + jjjj + 20, 200, paint);
                can.drawBitmap(ima[0], screenWidth / 2 - ima[0].getWidth() / 2
                        + jjjj, 120, null);
            }
        }
    }

    // 得到方向角
    private String getJiaodu() {
        jiaodu = (int) CompassActivity.degree0;
        if (jiaodu < 0) {
            return "";
        }
        if (jiaodu == 0 || jiaodu == 360) {
            dong = "正北";
        }
        if (jiaodu > 0 && jiaodu < 90) {
            dong = "东北";
        }
        if (jiaodu == 90) {
            dong = "正东";
        }
        if (jiaodu > 90 && jiaodu < 180) {
            dong = "东南";
        }
        if (jiaodu == 180) {
            dong = "正南";
        }
        if (jiaodu > 180 && jiaodu < 270) {
            dong = "西南";
        }
        if (jiaodu == 270) {
            dong = "正西";
        }
        if (jiaodu > 270 && jiaodu < 359) {
            dong = "西北";
        }
        tmp = dong + jiaodu;
        return tmp;
    }

    protected void onStarts() {
        if (getGrivity()) {
            checkHandler.sendEmptyMessage(0);
        } else {
            checkHandler.sendEmptyMessage(1);
        }
    }
}
原创粉丝点击