android map (google map ,百度 ,高德 )在路线 上画箭头

来源:互联网 发布:淘宝认证照片 编辑:程序博客网 时间:2024/06/18 09:56

 android  map  (google map ,百度 ,高德 )在路线 上画箭头!

其实分为2步骤: 算角度,画箭头marker ,marker 画在路线两个点的中点

public void drawArrow(List<LatLng> list) {    LogUtils.show(TAG, "开始画箭头");    if (list == null || list.size() <= 1) {        LogUtils.show(TAG, "开始画箭头------失败");        return;    }    LogUtils.show(TAG, "开始画箭头------失满足条件");    for (int i = 1; i < list.size(); i++) {        double lat1 = list.get(i - 1).latitude;        double lng1 = list.get(i - 1).longitude;        double lat2 = list.get(i).latitude;        double lng2 = list.get(i).longitude;        double lat = (lat1 + lat2) / 2;        double lng = (lng1 + lng2) / 2;        LatLng modLat = new LatLng(lat, lng);        double brng = computeAzimuth(new LatLng(lat1, lng1), new LatLng(lat2, lng2));        MarkerOptions marker = new MarkerOptions().position(modLat);        marker.anchor(0.5f, 0.5f);        marker.icon(BitmapDescriptorFactory.fromResource(R.mipmap.line_arrow));        marker.rotation((float) brng);        marker.flat(true);        mMap.addMarker(marker);        LogUtils.show(TAG, "开始画箭头------箭头-- " + i + "角度为  " + brng);    }}// 计算方位角,正北向为0度,以顺时针方向递增private double computeAzimuth(LatLng la1, LatLng la2) {    double lat1 = la1.latitude, lon1 = la1.longitude, lat2 = la2.latitude,            lon2 = la2.longitude;    double result = 0.0;    int ilat1 = (int) (0.50 + lat1 * 360000.0);    int ilat2 = (int) (0.50 + lat2 * 360000.0);    int ilon1 = (int) (0.50 + lon1 * 360000.0);    int ilon2 = (int) (0.50 + lon2 * 360000.0);    lat1 = Math.toRadians(lat1);    lon1 = Math.toRadians(lon1);    lat2 = Math.toRadians(lat2);    lon2 = Math.toRadians(lon2);    if ((ilat1 == ilat2) && (ilon1 == ilon2)) {        return result;    } else if (ilon1 == ilon2) {        if (ilat1 > ilat2)            result = 180.0;    } else {        double c = Math                .acos(Math.sin(lat2) * Math.sin(lat1) + Math.cos(lat2)                        * Math.cos(lat1) * Math.cos((lon2 - lon1)));        double A = Math.asin(Math.cos(lat2) * Math.sin((lon2 - lon1))                / Math.sin(c));        result = Math.toDegrees(A);        if ((ilat2 > ilat1) && (ilon2 > ilon1)) {        } else if ((ilat2 < ilat1) && (ilon2 < ilon1)) {            result = 180.0 - result;        } else if ((ilat2 < ilat1) && (ilon2 > ilon1)) {            result = 180.0 - result;        } else if ((ilat2 > ilat1) && (ilon2 < ilon1)) {            result += 360.0;        }    }    return result;}

原创粉丝点击