Diff of /trunk/src/map-tool.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 47 by harbaum, Thu Aug 6 20:23:12 2009 UTC revision 54 by harbaum, Wed Aug 12 19:20:00 2009 UTC
# Line 24  Line 24 
24  #include "osm-gps-map.h"  #include "osm-gps-map.h"
25  #endif  #endif
26    
27    #define GPS_DEFAULT_ZOOM 13
28    
29  /* equatorial radius in meters */  /* equatorial radius in meters */
30  #define EQ_RADIUS     (6378137.0)  #define EQ_RADIUS     (6378137.0)
31    
# Line 80  static const char *get_proxy_uri(appdata Line 82  static const char *get_proxy_uri(appdata
82  }  }
83    
84  static void map_zoom(map_context_t *context, int step) {  static void map_zoom(map_context_t *context, int step) {
85    int zoom;    gint zoom;
86    OsmGpsMap *map = OSM_GPS_MAP(context->widget);    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
87    g_object_get(map, "zoom", &zoom, NULL);    g_object_get(map, "zoom", &zoom, NULL);
88    zoom = osm_gps_map_set_zoom(map, zoom+step);    zoom = osm_gps_map_set_zoom(map, zoom+step);
# Line 88  static void map_zoom(map_context_t *cont Line 90  static void map_zoom(map_context_t *cont
90    /* enable/disable zoom buttons as required */    /* enable/disable zoom buttons as required */
91    gtk_widget_set_sensitive(context->zoomin, zoom<17);    gtk_widget_set_sensitive(context->zoomin, zoom<17);
92    gtk_widget_set_sensitive(context->zoomout, zoom>1);    gtk_widget_set_sensitive(context->zoomout, zoom>1);
93    
94      /* save new zoom */
95      context->appdata->map.zoom = zoom;
96  }  }
97    
98  static gboolean  static gboolean
# Line 107  cb_map_gps(GtkButton *button, map_contex Line 112  cb_map_gps(GtkButton *button, map_contex
112    pos_t *refpos = get_pos(context->appdata);    pos_t *refpos = get_pos(context->appdata);
113    if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {    if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
114      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
115                                refpos->lat, refpos->lon, 14);                        refpos->lat, refpos->lon, GPS_DEFAULT_ZOOM);
116    } else {    } else {
117      /* no coordinates given: display the entire world */      /* no coordinates given: display the entire world */
118      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
# Line 118  cb_map_gps(GtkButton *button, map_contex Line 123  cb_map_gps(GtkButton *button, map_contex
123  }  }
124    
125  static GtkWidget  static GtkWidget
126  *map_add_button(const gchar *icon, GCallback cb, gpointer data,  *map_add_button(int icon, GCallback cb, gpointer data,
127                  char *tooltip) {                  char *tooltip) {
128    GtkWidget *button = gtk_button_new();    GtkWidget *button = gtk_button_new();
129    gtk_button_set_image(GTK_BUTTON(button),    gtk_button_set_image(GTK_BUTTON(button), icon_get_widget(ICON_MISC, icon));
                        gtk_image_new_from_stock(icon, GTK_ICON_SIZE_BUTTON));  
130    g_signal_connect(button, "clicked", cb, data);    g_signal_connect(button, "clicked", cb, data);
131  #ifndef USE_MAEMO  #ifndef USE_MAEMO
132    gtk_widget_set_tooltip_text(button, tooltip);    gtk_widget_set_tooltip_text(button, tooltip);
# Line 133  static GtkWidget Line 137  static GtkWidget
137  static gboolean map_gps_update(gpointer data) {  static gboolean map_gps_update(gpointer data) {
138    map_context_t *context = (map_context_t*)data;    map_context_t *context = (map_context_t*)data;
139    
140      /* get reference position ... */
141    pos_t *refpos = get_pos(context->appdata);    pos_t *refpos = get_pos(context->appdata);
142    gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);    gboolean ok = (refpos!= NULL) && !isnan(refpos->lat) && !isnan(refpos->lon);
143    
144    /* get reference position and go there */    /* ... and enable "goto" button if it's valid */
145    gtk_widget_set_sensitive(context->gps, ok);    gtk_widget_set_sensitive(context->gps, ok);
146    
147      if(ok) {
148        float heading = NAN;
149        int radius = 0;
150    
151        if(context->appdata->use_gps) {
152          heading = gps_get_heading(context->appdata);
153    
154          /* get error */
155          float eph = gps_get_eph(context->appdata);
156          if(!isnan(eph)) {
157    
158            /* world at zoom 1 == 512 pixels */
159            gint zoom;
160            g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);
161            float m_per_pix =
162              cos(DEG2RAD(refpos->lat))*2*M_PI*EQ_RADIUS/(1<<(8+zoom));
163    
164            radius = eph/m_per_pix;
165          }
166        }
167    
168        g_object_set(context->widget, "gps-track-highlight-radius", radius, NULL);
169        osm_gps_map_draw_gps(OSM_GPS_MAP(context->widget),
170                             refpos->lat, refpos->lon, heading);
171      } else
172        osm_gps_map_clear_gps(OSM_GPS_MAP(context->widget));
173    
174    return TRUE;    return TRUE;
175  }  }
176    
# Line 146  static gboolean on_map_configure(GtkWidg Line 178  static gboolean on_map_configure(GtkWidg
178                                   GdkEventConfigure *event,                                   GdkEventConfigure *event,
179                                   map_context_t *context) {                                   map_context_t *context) {
180    
181    cb_map_gps(NULL, context);    /* set default values if they are invalid */
182      if(!context->appdata->map.zoom ||
183         isnan(context->appdata->map.pos.lat) ||
184         isnan(context->appdata->map.pos.lon)) {
185        printf("no valid map position found\n");
186    
187        pos_t *refpos = get_pos(context->appdata);
188        if(refpos && !isnan(refpos->lat) && !isnan(refpos->lon)) {
189          /* use gps position if present */
190          context->appdata->map.pos = *refpos;
191          context->appdata->map.zoom = GPS_DEFAULT_ZOOM;
192        } else {
193          /* use world map otherwise */
194          context->appdata->map.pos.lat = 0.0;
195          context->appdata->map.pos.lon = 0.0;
196          context->appdata->map.zoom = 1;
197        }
198      }
199    
200      /* jump to initial position */
201      osm_gps_map_set_mapcenter(OSM_GPS_MAP(context->widget),
202                                context->appdata->map.pos.lat,
203                                context->appdata->map.pos.lon,
204                                context->appdata->map.zoom);
205    
206    return FALSE;    return FALSE;
207  }  }
208    
# Line 522  pos_t coord2pos(coord_t coo) { Line 577  pos_t coord2pos(coord_t coo) {
577  }  }
578    
579  static int dist2pixel(map_context_t *context, float km, float lat) {  static int dist2pixel(map_context_t *context, float km, float lat) {
580    int zoom;    gint zoom;
581    g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);    g_object_get(OSM_GPS_MAP(context->widget), "zoom", &zoom, NULL);
582    
583    /* world at zoom 1 == 512 pixels */    /* world at zoom 1 == 512 pixels */
# Line 561  on_map_button_press_event(GtkWidget *wid Line 616  on_map_button_press_event(GtkWidget *wid
616  static gboolean  static gboolean
617  on_map_button_release_event(GtkWidget *widget,  on_map_button_release_event(GtkWidget *widget,
618                              GdkEventButton *event, map_context_t *context) {                              GdkEventButton *event, map_context_t *context) {
619    if(context->press_on) {    OsmGpsMap *map = OSM_GPS_MAP(context->widget);
     OsmGpsMap *map = OSM_GPS_MAP(context->widget);  
620    
621      if(context->press_on) {
622      pos_t pos =      pos_t pos =
623        coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));        coord2pos(osm_gps_map_get_co_ordinates(map, event->x, event->y));
624    
# Line 574  on_map_button_release_event(GtkWidget *w Line 629  on_map_button_release_event(GtkWidget *w
629          cache_popup(context, nearest);          cache_popup(context, nearest);
630      }      }
631      context->press_on = NULL;      context->press_on = NULL;
632      } else {
633        /* save new map position */
634        gfloat lat, lon;
635        g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
636        context->appdata->map.pos.lat = lat;
637        context->appdata->map.pos.lon = lon;
638    }    }
639    
640    return FALSE;    return FALSE;
641  }  }
642    
643    static void save_map_state(map_context_t *context) {
644      /* save map parameters */
645      OsmGpsMap *map = OSM_GPS_MAP(context->widget);
646      gint zoom;
647      g_object_get(map, "zoom", &zoom, NULL);
648      context->appdata->map.zoom = zoom;
649    
650      gfloat lat, lon;
651      g_object_get(map, "latitude", &lat, "longitude", &lon, NULL);
652      context->appdata->map.pos.lat = lat;
653      context->appdata->map.pos.lon = lon;
654    }
655    
656  #if MAEMO_VERSION_MAJOR == 5  #if MAEMO_VERSION_MAJOR == 5
657  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {  static void on_window_destroy(GtkWidget *widget, map_context_t *context) {
658    printf("destroy map view\n");    printf("destroy map view\n");
659    
660      save_map_state(context);
661    
662    /* restore cur_view */    /* restore cur_view */
663    context->appdata->cur_view = context->old_view;    context->appdata->cur_view = context->old_view;
664    
# Line 602  void map(appdata_t *appdata) { Line 677  void map(appdata_t *appdata) {
677    const char *proxy = get_proxy_uri(appdata);    const char *proxy = get_proxy_uri(appdata);
678    
679    context->widget = g_object_new(OSM_TYPE_GPS_MAP,    context->widget = g_object_new(OSM_TYPE_GPS_MAP,
680                  "repo-uri", MAP_SOURCE_OPENSTREETMAP,                   "repo-uri",             MAP_SOURCE_OPENSTREETMAP,
681                  "tile-cache", path,                   "tile-cache",           path,
682                  proxy?"proxy-uri":NULL, proxy,                   "auto-center",          FALSE,
683                     "record-trip-history",  FALSE,
684                     "show-trip-history",    FALSE,
685                   NULL);                   NULL);
686    
687      if(proxy)
688        g_object_set(OSM_GPS_MAP(context->widget), "proxy-uri", proxy, NULL);
689    
690    g_free(path);    g_free(path);
691    
692    char *name = NULL;    char *name = NULL;
# Line 663  void map(appdata_t *appdata) { Line 743  void map(appdata_t *appdata) {
743    GtkWidget *vbox = gtk_vbox_new(FALSE,0);    GtkWidget *vbox = gtk_vbox_new(FALSE,0);
744    
745    context->zoomin =    context->zoomin =
746      map_add_button(GTK_STOCK_ZOOM_IN, G_CALLBACK(cb_map_zoomin),      map_add_button(10, G_CALLBACK(cb_map_zoomin),
747                     context, _("Zoom in"));                     context, _("Zoom in"));
748    gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0);    gtk_box_pack_start(GTK_BOX(vbox), context->zoomin, FALSE, FALSE, 0);
749    
750    context->zoomout =    context->zoomout =
751      map_add_button(GTK_STOCK_ZOOM_OUT, G_CALLBACK(cb_map_zoomout),      map_add_button(11, G_CALLBACK(cb_map_zoomout),
752                     context, _("Zoom out"));                     context, _("Zoom out"));
753    gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0);    gtk_box_pack_start(GTK_BOX(vbox), context->zoomout, FALSE, FALSE, 0);
754    
755    context->gps =    context->gps =
756      map_add_button(GTK_STOCK_HOME, G_CALLBACK(cb_map_gps),      map_add_button(9, G_CALLBACK(cb_map_gps),
757                     context, _("Jump to GPS position"));                     context, _("Jump to GPS position"));
758    gtk_widget_set_sensitive(context->gps, FALSE);    gtk_widget_set_sensitive(context->gps, FALSE);
759    /* install handler for timed updates of the gps button */    /* install handler for timed updates of the gps button */
# Line 698  void map(appdata_t *appdata) { Line 778  void map(appdata_t *appdata) {
778    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
779    gtk_widget_show_all(dialog);    gtk_widget_show_all(dialog);
780    gtk_dialog_run(GTK_DIALOG(dialog));    gtk_dialog_run(GTK_DIALOG(dialog));
781      save_map_state(context);
782    gtk_timeout_remove(context->handler_id);    gtk_timeout_remove(context->handler_id);
783    gtk_widget_destroy(dialog);    gtk_widget_destroy(dialog);
784    g_free(context);    g_free(context);

Legend:
Removed from v.47  
changed lines
  Added in v.54