%headertop{ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "vikcoord.h" #include "mapcoord.h" #include "vik-map-type.h" %} %{ #ifdef HAVE_MATH_H #include #endif #include "googlemaps.h" /* 1 << (x-1) is like a 2**(x-1) */ #define GZ(x) ((1<<(x-1))*GOOGLEMAPS_ZOOM_ONE_MPP) static const gdouble scale_mpps[] = { GZ(1)/2, GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9), GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15) }; static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); #define ERROR_MARGIN 0.01 static guint8 googlemaps_zoom ( gdouble mpp ) { gint i; for ( i = 0; i < num_scales; i++ ) { if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) return i; } return 255; } %} class Google:Maps from Vik:Map:Type { override (Vik:Map:Type) void mapcoord_to_center_coord ( Vik:Map:Type *self, MapCoord *src, VikCoord *dest ) { dest->mode = VIK_COORD_LATLON; dest->north_south = 39.5 - (1.0/(131072 >> src->scale) * (src->y+0.5) * 128); dest->east_west = 1.0 / (131072 >> src->scale) * (src->x+0.5) / 0.77162458338772 * 128 - 98.35; } override (Vik:Map:Type) gboolean coord_to_mapcoord ( self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ) { g_assert ( src->mode == VIK_COORD_LATLON ); if ( xzoom != yzoom ) return FALSE; dest->scale = googlemaps_zoom ( xzoom ); if ( dest->scale == 255 ) return FALSE; dest->x = (gint) floor ( floor ((src->east_west + 98.35) * (131072 >> dest->scale) * 0.77162458338772) / 128); dest->y = (gint) floor ( floor ((39.5 - src->north_south) * (131072 >> dest->scale)) / 128); dest->z = 0; return TRUE; } }