android - How to show direction on map based on calculated angle in degrees -
i have calculated angle between 2 latitude , longitude coordinates below code.it returns angle 3 in radians, , 193 in degrees. want show arrow marker on map based on angle.how display object direction moved,based on 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 , rotate arrow image on same angle on map.this makes direction of route.
import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.matrix; /* inside loop of 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 loop of 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; }
Comments
Post a Comment