![](/img/trans.png)
[英]how to draw a path between two geopoints(not specific) in google map in android?
[英]Direction between two GeoPoints in Google map in android
我正在開發一個android應用程序,我在Google地圖中有兩個GeoPoints。一個GeoPoint是修復的,另一個GeoPoint是我當前的位置。 在我當前的位置上,我將一個箭頭指向固定GeoPoint的方向,因為我的當前位置發生變化,因此箭頭將更改其方向。它可以正常工作,但在大多數情況下顯示的方向是錯誤的。
這是我的代碼。
Paint paint = new Paint();
GeoPoint gp1;
GeoPoint gp2;
Point point1,point2;
Projection projection = mapView.getProjection();
projection.toPixels(gp1, point1);
projection.toPixels(gp2, point2);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.arrow);
double dlon = gp2.getLongitudeE6() - gp1.getLongitudeE6();
double dlat = gp2.getLatitudeE6() - gp1.getLatitudeE6();
double a = (Math.sin(dlat / 2) * Math.sin(dlat / 2)) + Math.cos(gp1.getLatitudeE6()) * Math.cos(gp2.getLatitudeE6()) * (Math.sin(dlon / 2) * Math.sin(dlon / 2));
double angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
Matrix matrix = new Matrix();
matrix.postTranslate(-25, -25);
matrix.postRotate(convertToRadians(angle));
matrix.postTranslate(point1.x, point1.y);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
canvas.drawBitmap(bmp, matrix, paint);
等待您的幫助並提前致謝。
阿爾塔夫
使用投影點而不是地理位置點進行角度計算,因為您的箭頭將顯示投影點的方向。
更換
double dlon = gp2.getLongitudeE6() - gp1.getLongitudeE6();
double dlat = gp2.getLatitudeE6() - gp1.getLatitudeE6();
與
double dlon = point2.x - point1.x;
double dlat = point2.y - point1.y;
對於角度計算,“ Geobits”建議的公式更簡單,更好。
double angle = Math.atan2(dlat, dlon);
我徹底測試了此代碼,它適用於我的應用程序中的所有情況。
你從哪里得到那個公式的? 對於簡單的角度計算,它似乎非常復雜。 通常我只將atan2與y,x一起使用:
double angle = Math.atan2(dlat, dlon); // in radians
假設默認情況下,您的箭頭指向水平軸(向東)。 您可能必須通過可繪制箭頭所指向的任何方向進行調整。 例如,如果它指向恆定的90度角,只需將其旋轉額外的0.5 * PI rad以使其對齊即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.