]> err.no Git - libchamplain/commitdiff
Function to get map coordinates from events
authorPierre-Luc Beaudoin <pierre-luc@pierlux.com>
Wed, 19 Nov 2008 16:24:07 +0000 (11:24 -0500)
committerPierre-Luc Beaudoin <pierre-luc@pierlux.com>
Wed, 19 Nov 2008 16:24:07 +0000 (11:24 -0500)
champlain/champlainview.c
champlain/champlainview.h
demos/launcher.c

index 4ede33711cc386654199e7658df9871cec140098..8253419df960c244dfae4d04b6572a3fefa9efd6 100644 (file)
@@ -771,7 +771,7 @@ champlain_view_center_on (ChamplainView *view, gdouble latitude, gdouble longitu
       return;
     }
 
-  gdouble x, y;
+  gint x, y;
   x = priv->map->longitude_to_x(priv->map, longitude, priv->map->current_level->level);
   y = priv->map->latitude_to_y(priv->map, latitude, priv->map->current_level->level);
   ChamplainPoint* anchor = &priv->map->current_level->anchor;
@@ -905,3 +905,54 @@ champlain_view_add_layer (ChamplainView *view, ClutterActor *layer)
 
   clutter_container_foreach(CLUTTER_CONTAINER(layer), CLUTTER_CALLBACK(connect_marker_notify_cb), view);
 }
+
+gboolean 
+champlain_view_get_coords_from_event (ChamplainView *view, ClutterEvent *event, gdouble *lat, gdouble *lon)
+{
+  g_return_val_if_fail(CHAMPLAIN_IS_VIEW(view), FALSE);
+  g_return_val_if_fail(event, FALSE); // Apparently there isn't a more precise test
+
+  ChamplainViewPrivate *priv = CHAMPLAIN_VIEW_GET_PRIVATE (view);
+  guint x, y;
+
+  switch (clutter_event_type (event))
+    {
+      case CLUTTER_BUTTON_PRESS:
+      case CLUTTER_BUTTON_RELEASE:
+        {
+          ClutterButtonEvent *e = (ClutterButtonEvent*) event;
+          x = e->x;
+         y = e->y;
+        }
+        break;
+      case CLUTTER_SCROLL:
+        {
+          ClutterScrollEvent *e = (ClutterScrollEvent*) event;
+          x = e->x;
+         y = e->y;
+        }
+        break;
+      case CLUTTER_MOTION:
+        {
+          ClutterMotionEvent *e = (ClutterMotionEvent*) event;
+          x = e->x;
+         y = e->y;
+        }
+        break;
+      case CLUTTER_ENTER:
+      case CLUTTER_LEAVE:
+        {
+          ClutterCrossingEvent *e = (ClutterCrossingEvent*) event;
+          x = e->x;
+         y = e->y;
+        }
+        break;
+      default:
+       return FALSE;
+    }
+  if (lat)
+    *lat = viewport_get_latitude_at(priv, priv->viewport_size.y + y + priv->map->current_level->anchor.y);
+  if (lon)
+    *lon = viewport_get_longitude_at(priv, priv->viewport_size.x + x + priv->map->current_level->anchor.x);
+  return TRUE;
+}
index 88810f2443a48e9a523aee67e8b2562a25d0d0d1..ab627c5b6ba6f71793da6b82e9df0c88d710282f 100644 (file)
@@ -94,4 +94,6 @@ void champlain_view_add_layer (ChamplainView *champlainView, ClutterActor *layer
 
 void champlain_view_set_size (ChamplainView *view, guint width, guint height);
 
+gboolean champlain_view_get_coords_from_event (ChamplainView *view, ClutterEvent *event, gdouble *lat, gdouble *lon);
+
 #endif
index 958c80b2af6895951b9c05945de3213d98992bc1..3f4556285392fa3b577397488d673e9eab216e80 100644 (file)
 
 static gboolean
 montreal_click (ClutterActor *actor,
-                ClutterEvent *event,
-                gpointer      user_data)
-{      g_print("Montreal was clicked!\n");
+                ClutterButtonEvent *event,
+                ChamplainView * view)
+{      
+       g_print("Montreal was clicked!\n");
+       gdouble lat, lon;
+       if (champlain_view_get_coords_from_event (view, event, &lat, &lon))
+         g_print("%f, %f \n", lat, lon);
        return TRUE;
 }
 
 static ClutterActor*
-create_marker_layer ()
+create_marker_layer (ChamplainView *view)
 {
   ClutterActor *layer, *marker;
   
@@ -44,7 +48,7 @@ create_marker_layer ()
   g_signal_connect_after (marker,
                     "button-release-event",
                     G_CALLBACK (montreal_click),
-                    NULL);
+                    view);
   
   marker = champlain_marker_new_with_label("New York", "Sans 25", &white, NULL);
   champlain_marker_set_position(CHAMPLAIN_MARKER(marker), 40.77, -73.98);
@@ -73,11 +77,11 @@ main (int argc, char *argv[])
   
   champlain_view_set_size (CHAMPLAIN_VIEW (actor), 800, 600);
   
-  g_object_set(G_OBJECT(actor), "zoom-level", 5, NULL);
-  layer = create_marker_layer();
+  layer = create_marker_layer(actor);
   champlain_view_add_layer(CHAMPLAIN_VIEW (actor), layer);
 
   clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor);
+  g_object_set (G_OBJECT (actor), "zoom-level", 12, NULL);  
   champlain_view_center_on(CHAMPLAIN_VIEW(actor), 45.466, -73.75);
   
   clutter_actor_show (stage);