【问题标题】:Using orientation sensor to point towards a specific location使用方向传感器指向特定位置
【发布时间】:2011-07-25 16:25:48
【问题描述】:

我正在尝试实现一个使用方向传感器指向特定位置的箭头。 Google 地方信息在它找到的每个地方都在 ListView 中实现了这个箭头。

我已经设法得到方位角,但是给定一个位置,我不知道如何继续计算我需要的角度。此外,我需要从真北和磁北进行转换。有人有这种实现的例子吗?

提前致谢。

【问题讨论】:

    标签: android location orientation compass-geolocation sensors


    【解决方案1】:

    我解决了。

    float azimuth = // get azimuth from the orientation sensor (it's quite simple)
    Location currentLoc = // get location from GPS or network
    // convert radians to degrees
    azimuth = Math.toDegrees(azimuth);
    GeomagneticField geoField = new GeomagneticField(
                 (float) currentLoc.getLatitude(),
                 (float) currentLoc.getLongitude(),
                 (float) currentLoc.getAltitude(),
                 System.currentTimeMillis());
    azimuth += geoField.getDeclination(); // converts magnetic north to true north
    float bearing = currentLoc.bearingTo(target); // (it's already in degrees)
    float direction = azimuth - bearing;
    

    如果您要绘制箭头或其他东西来指向方向,请使用 canvas.rotate(-direction)。我们传递了一个否定参数,因为画布旋转是逆时针的。

    【讨论】:

    • 您可以简化将方位角转换为度数:azimuth = azimuth * 180 / (float)Math.PI;
    • 使用 Math.toDegrees(azimuth) 更容易
    • 是用户插入的目标位置。
    • 谁能解释一下如何获取方位角值
    • @MuhammadHamzaShahid 见developer.android.com/reference/android/hardware/…, float[])
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多