android - 如何根据计算的角度在 map 上显示方向

标签 android google-maps geometry compass-geolocation

我计算了两个纬度和经度坐标之间的角度,如下代码所示。它返回角度为 3(弧度)和 193(度)。我想根据这个角度在 map 上显示箭头标记。如何根据这个角度显示物体移动的方向?

public static double getAngle(double lat1, double lon1, double lat2, double lon2)
    {
        //Formulas

    //θ =   atan2(  sin(Δlong).cos(lat2),cos(lat1).sin(lat2) − sin(lat1).cos(lat2).cos(Δlong) )
    // Δlong = long2 - long1
    Log.i("angle", "Inside getAngle");
    double latitude1 = Math.toRadians(lat1);
    double longitude1 = Math.toRadians(lon1);
    double latitude2 = Math.toRadians(lat2);
    double longitude2 = Math.toRadians(lon2);


    double dlong = Math.toRadians(longitude2-longitude1);

    double y = Math.sin(dlong) * Math.cos(latitude2);
    double x = Math.cos(latitude1)*Math.sin(latitude2) - Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(dlong);
    double angle= Math.atan2(y, x);


    if (angle < 0)
        angle = Math.abs(angle);
    else
        angle = 2*Math.PI - angle;

    Log.i("angle", String.valueOf(angle)+" in radians");

    angle=Math.toDegrees(angle);
    Log.i("angle", String.valueOf(angle)+" in degrees");

    return angle;
}

最佳答案

我得到了角度并在 map 上以相同的角度旋转箭头图像。这使得路线的方向相似。

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/* inside for loop of all Latitude Longitude values */
float angle=(float)finalBearing(previousLocationLatitude, previousLocationLongitude, currentLocationLatitude, currentLocationLongitude);

Bitmap sprite = BitmapFactory.decodeResource(this.getResources(),R.drawable.dir_green_image);

Matrix mat = new Matrix();
mat.preRotate(angle);///in degree
Bitmap mBitmap = Bitmap.createBitmap(sprite, 0, 0, sprite.getWidth(), sprite.getHeight(), mat, true);

mMap.addMarker(new MarkerOptions().position(new LatLng(currentLocationLatitude, currentLocationLongitude)).icon(BitmapDescriptorFactory.fromBitmap(mBitmap)).anchor((float)0.5, (float)0.5));
/* inside for loop of all Latitude Longitude values */


static public double initialBearing (double lat1, double long1, double lat2, double long2)
{
    return (_bearing(lat1, long1, lat2, long2) + 360.0) % 360;
}

static public double finalBearing(double lat1, double long1, double lat2, double long2)
{
    return (_bearing(lat2, long2, lat1, long1) + 180.0) % 360;
}

static private double _bearing(double lat1, double long1, double lat2, double long2)
{
    double degToRad = Math.PI / 180.0;
    double phi1 = lat1 * degToRad;
    double phi2 = lat2 * degToRad;
    double lam1 = long1 * degToRad;
    double lam2 = long2 * degToRad;

    return Math.atan2(Math.sin(lam2-lam1)*Math.cos(phi2),
        Math.cos(phi1)*Math.sin(phi2) - Math.sin(phi1)*Math.cos(phi2)*Math.cos(lam2-lam1)
    ) * 180/Math.PI;
}

关于android - 如何根据计算的角度在 map 上显示方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15745002/

相关文章:

python - 如何找到线段上距离任意点最近的点?

php - 如何从 API 服务器删除特定的推送通知

android - Google Glass SDK:Gradle同步失败:原因:无法找到目标Google Inc.:Glass Development Kit预览:19

google-maps - Google Maps V3 API map 无法正确居中

javascript - 放大 GeoJSON 多边形

c++ - 计算旋转角度

android - 使用 R8 的 firebase 中的循环引用错误

使用 RxJava 或 kotlin 协程的 Android ViewState

javascript - 谷歌地图替代道路以不同颜色显示

android - KML 可扩展性?在Android 中使用KML 显示和过滤1600+ 个点?