1. 程式人生 > >android map (google map ,百度 ,高德 )在路線 上畫箭頭

android map (google map ,百度 ,高德 )在路線 上畫箭頭

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