【问题标题】:Compass: Rotate image towards a particular location指南针:将图像旋转到特定位置
【发布时间】:2015-07-01 16:52:32
【问题描述】:

我需要将图像箭头旋转到特定位置。这是我当前的代码:

    public void onSensorChanged(SensorEvent event) {
        float azimuth = event.values[0];
        Location currentLoc = getHelper().getCurrentLocation();
        if(currentLoc == null || cardinal == null || cardinal.getLocation() == null) {
            return;
        }

        azimuth = (float) Math.toDegrees(azimuth);
        GeomagneticField geoField = new GeomagneticField(
                (float) currentLoc.getLatitude(),
                (float) currentLoc.getLongitude(),
                (float) currentLoc.getAltitude(),
                System.currentTimeMillis());

        azimuth += geoField.getDeclination(); // converts magnetic north into true north
        float bearing = currentLoc.bearingTo(cardinal.getLocation()); // (it's already in degrees)
        float direction = azimuth - bearing;
        direction = -direction;
        if(compass != null) {
            compass.setDirection(direction);
        }
}

这是我的自定义图像视图:

public void onDraw(Canvas canvas) { //
    int height = this.getHeight();  //
    int width = this.getWidth();

    canvas.rotate(direction, width / 2, height / 2); //
    super.onDraw(canvas); //
}

这是我正在使用的箭头图像:

此代码根本不起作用。谁能指出我做错了什么?

【问题讨论】:

    标签: android location latitude-longitude compass


    【解决方案1】:

    我自己找到了解决方案。我正在为正在寻找相同解决方案的其他人发布答案:这是更新后的代码:

        float azimuth = event.values[0];
            Location currentLoc = getHelper().getCurrentLocation();
            if(currentLoc == null || cardinal == null || cardinal.getLocation() == null) {
                return;
            }
    
            try {
                GeomagneticField geoField = new GeomagneticField(
                        Double.valueOf(currentLoc.getLatitude()).floatValue(),
                        Double.valueOf(currentLoc.getLongitude()).floatValue(),
                        Double.valueOf(currentLoc.getAltitude()).floatValue(),
                        System.currentTimeMillis() );
    
                float myBearing = currentLoc.bearingTo(cardinal.getLocation());
    
                azimuth += geoField.getDeclination();
                azimuth = (myBearing - azimuth) * -1;
                azimuth = normalizeDegree(azimuth);
    //            compass.updateValues(-azimuth);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(compass != null) {
                compass.setDirection(-azimuth);
            }
    

    【讨论】:

    • 你能用activity和xml代码发布这个示例代码吗?
    猜你喜欢
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多