如何根据度计算的角度,在地图上显示方向 [英] How to show direction on map based on calculated angle in degrees

查看:795
本文介绍了如何根据度计算的角度,在地图上显示方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经计算出角2纬度之间和经度坐标如下code.It返回角度3弧度,和193。我想说明的箭头标记在地图上在此基础上angle.How显示对象的方向移动,基于这个角度?

 公共静态双getAngle(双LAT1,双lon1,双LAT2,双lon2)
    {
        //公式

    //θ= ATAN2(SIN(Δlong)名为.cos(LAT2),COS(LAT1).sin(LAT2) - 罪(LAT1)名为.cos(LAT2)名为.cos(Δlong))
    //Δlong= long2  -  long1
    Log.i(角,内部getAngle);
    双latitude1 = Math.toRadians(LAT1);
    双longitude1 = Math.toRadians(lon1);
    双latitude2 = Math.toRadians(LAT2);
    双longitude2 = Math.toRadians(lon2);


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

    双Y = Math.sin(dlong)* Math.cos(latitude2);
    双X = Math.cos(latitude1)* Math.sin(latitude2) -  Math.sin(latitude1)* Math.cos(latitude2)* Math.cos(dlong);
    双角= Math.atan2(Y,X);


    如果(角℃,)
        角= Math.abs(角度);
    其他
        角= 2 * Math.PI  - 角;

    Log.i(角,将String.valueOf(弧度角)+);

    角= Math.toDegrees(角度);
    Log.i(角,将String.valueOf(度角)+);

    返回角;
}
 

解决方案

我的角度和旋转箭头图像上Map.This相同的角度使得像路线的方向。

 进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Matrix;

/ *为所有纬度经度值的循环中* /
浮动角=(浮点)finalBearing(previousLocationLatitude,previousLocationLongitude,currentLocationLatitude,currentLocationLongitude);

位图精灵= BitmapFactory.de codeResource(this.getResources(),R.drawable.dir_green_image);

矩阵垫=新的Matrix();
在学历///;垫preRotate(角度)
位图mBitmap = Bitmap.createBitmap(精灵,0,0,sprite.getWidth(),sprite.getHeight(),垫,真正的);

mMap.addMarker(新MarkerOptions()位置(新的经纬度(currentLocationLatitude,currentLocationLongitude))图标(BitmapDesc​​riptorFactory.fromBitmap(mBitmap))锚((浮点)0.5,(浮动)0.5));
/ *为所有纬度经度值的循环中* /


静态公共双initialBearing(双LAT1,双long1,双LAT2,双long2)
{
    返程(_bearing(LAT1,long1,LAT2,long2)+ 360.0)%360;
}

静态公共双finalBearing(双LAT1,双long1,双LAT2,双long2)
{
    返程(_bearing(LAT2,long2,LAT1,long1)+ 180.0)%360;
}

静态专用双_bearing(双LAT1,双long1,双LAT2,双long2)
{
    双degToRad = Math.PI / 180.0;
    双PHI1 = LAT1 * degToRad;
    双PHI2 = LAT2 * degToRad;
    双lam1 = long1 * degToRad;
    双lam2 = long2 * degToRad;

    返回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;
}
 

I have calculated angle between two Latitude and Longitude coordinates as below code.It returns angle as 3 in radians, and 193 in degrees. I want to show arrow marker on map based on this angle.How to display object direction moved,based on this angle?

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;
}

解决方案

I got angle and rotate arrow image on same angle on Map.This makes like direction of Route.

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;
}

这篇关于如何根据度计算的角度,在地图上显示方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆