From 3add3bd49158c9ca134d0ab7aa189366a5b545ad Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Fri, 10 Aug 2007 13:16:55 +0300 Subject: [PATCH] Display course/heading to target. --- src/cb.c | 2 +- src/latlon.c | 29 +++++++++++++++++++++++++---- src/latlon.h | 1 + src/map.c | 23 +++++++++++++++-------- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/cb.c b/src/cb.c index c312cfc..ac8e02b 100644 --- a/src/cb.c +++ b/src/cb.c @@ -1025,7 +1025,7 @@ void cmenu_distance_to(guint unitx, guint unity) unit2latlon(unitx, unity, lat, lon); - snprintf(buffer, sizeof(buffer), "%s: %.02f %s", _("Distance"), + snprintf(buffer, sizeof(buffer), "%s: %.02lf %s", _("Distance"), calculate_distance(_gps.lat, _gps.lon, lat, lon) * UNITS_CONVERT[_units], UNITS_TEXT[_units]); MACRO_BANNER_SHOW_INFO(_window, buffer); diff --git a/src/latlon.c b/src/latlon.c index df7bd70..3cb06c9 100644 --- a/src/latlon.c +++ b/src/latlon.c @@ -67,10 +67,10 @@ calculate_distance(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2) gdouble dlat, dlon, slat, slon, a; /* Convert to radians. */ -lat1*=(M_PI_4l / 180.f); -lon1*=(M_PI_4l / 180.f); -lat2*=(M_PI_4l / 180.f); -lon2*=(M_PI_4l / 180.f); +lat1*=(M_PIl / 180.f); +lon1*=(M_PIl / 180.f); +lat2*=(M_PIl / 180.f); +lon2*=(M_PIl / 180.f); dlat=lat2 - lat1; dlon=lon2 - lon1; @@ -83,3 +83,24 @@ a=(slat * slat) + (cos(lat1) * cos(lat2) * slon * slon); return ((2.f * atan2(sqrt(a), sqrt(1.f - a))) * EARTH_RADIUS); } +gdouble +calculate_course(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2) +{ +gdouble c,dlon,x,y; + +lat1*=(M_PIl / 180.f); +lon1*=(M_PIl / 180.f); +lat2*=(M_PIl / 180.f); +lon2*=(M_PIl / 180.f); + +dlon=lon2-lon1; +y=sin(dlon)*cos(lat2); +x=cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(dlon); +c=atan2(y,x); +g_printf("H: %lf %lf\n", x ,y, c); + +/* Fixup for compass */ +c*=180.f/M_PIl; +c=c+360 % 360; +return c; +} diff --git a/src/latlon.h b/src/latlon.h index 1e4a581..162b4bf 100644 --- a/src/latlon.h +++ b/src/latlon.h @@ -13,5 +13,6 @@ gint32 lat2mp_int(gdouble lat); gdouble calculate_distance(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2); gulong calculate_idistance(gint lat1, gint lon1, gint lat2, gint lon2); gulong calculate_idistance_cmp(gint lat1, gint lon1, gint lat2, gint lon2); +gdouble calculate_course(gdouble lat1, gdouble lon1, gdouble lat2, gdouble lon2); #endif diff --git a/src/map.c b/src/map.c index a2ae11a..e754687 100644 --- a/src/map.c +++ b/src/map.c @@ -1396,6 +1396,20 @@ if (!s && !mp && !sp) { gtk_label_set_label(GTK_LABEL(info_banner.location), buffer); } +static void +map_update_destination(gdouble lat, gdouble lon) +{ +if (_dest.valid) { + gchar buffer[32]; + gdouble dt=calculate_distance(lat, lon, _dest.lat, _dest.lon); + gdouble dh=calculate_course(lat, lon, _dest.lat, _dest.lon); + snprintf(buffer, sizeof(buffer), "%.02f %s (%0.02f)", dt * UNITS_CONVERT[_units], UNITS_TEXT[_units], dh); + gtk_label_set_label(GTK_LABEL(info_banner.distance), buffer); +} else { + gtk_label_set_label(GTK_LABEL(info_banner.distance), ""); +} +} + static void map_update_location(gint x, gint y, gboolean force) { @@ -1426,14 +1440,7 @@ else map_set_place_information(NULL, NULL, NULL); osm_progress_set_widget(_db, NULL); -if (_dest.valid) { - gchar buffer[32]; - gdouble dt=calculate_distance(lat, lon, _dest.lat, _dest.lon); - snprintf(buffer, sizeof(buffer), "%.02f %s", dt * UNITS_CONVERT[_units], UNITS_TEXT[_units]); - gtk_label_set_label(GTK_LABEL(info_banner.distance), buffer); -} else { - gtk_label_set_label(GTK_LABEL(info_banner.distance), ""); -} +map_update_destination(lat, lon); inp=FALSE; } -- 2.39.5