Issue
I need to make MapView from osmdroid as in navigator. The driver rides the car by the route which was taken from server. The map should be rotated for him as the route was always in front of his point on the map. The problem is that I can't to get right angle for every rotation. Sometimes it works, sometimes not. And how to define the rotate will be in 1 meter for example? Maybe, is some library or classes in osmdroid that can help me to solve these problems?
Solution
I've found a solution. This method calculates me a correct degree to rotate my map. I cut my route to straight lines. When current location is near next line, I call this method
public double rotateToNextCheckPoint() {
int lineId = OfferPreference.getInstance().getCurrentLineId();
if (rotates != null && lineId >= 0 && road.mRouteHigh.size() > 0) {
if (lineId < (rotates.size() - 1)) {
GeoPoint nextPoint = rotates.get(lineId).getLast();
GeoPoint currPoint = rotates.get(lineId).getFirst();
if (nextPoint == null || currPoint == null) {
return 0;
}
double lat1 = Math.toRadians(currPoint.getLatitude());
double lon1 = Math.toRadians(currPoint.getLongitude());
double lat2 = Math.toRadians(nextPoint.getLatitude());
double lon2 = Math.toRadians(nextPoint.getLongitude());
double cos1 = Math.cos(lat1);
double cos2 = Math.cos(lat2);
double sin1 = Math.sin(lat1);
double sin2 = Math.sin(lat2);
double delta = lon2 - lon1;
double deltaCos = Math.cos(delta);
double deltaSin = Math.sin(delta);
double x = (cos1 * sin2) - (sin1 * cos2 * deltaCos);
double y = deltaSin * cos2;
double z = Math.toDegrees(Math.atan((-y / x)));
if (x < 0) {
z += 180;
}
double z2 = (z + 180) % 360 - 180;
z2 = -Math.toRadians(z2);
double angleRad = z2 - (Math.PI * 2 * Math.floor(z2 / (2 * Math.PI)));
double angle = Math.toDegrees(angleRad);
rotationGestureOverlay.onRotate(-(float) angle, false);
return angle;
}
}
return 0;
}
Answered By - Sandra Dita
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.