osm: add optional google maps support
authorAntoine Beaupré <anarcat@koumbit.org>
Thu, 9 Aug 2012 05:07:20 +0000 (01:07 -0400)
committerAntoine Beaupré <anarcat@koumbit.org>
Sat, 25 Aug 2012 12:20:30 +0000 (08:20 -0400)
for google maps to work, an API key needs to be added to the configuration

IkiWiki/Plugin/osm.pm
underlays/osm/ikiwiki/osm.js

index 21d1dce3f2f9692373a4f582009e8570a8cf9311..d86dbd66ec9d992aeb33243e95d64357fdd20d42 100644 (file)
@@ -74,7 +74,13 @@ sub getsetup () {
                        safe => 0,
                        rebuild => 1,
                },
-
+               osm_google_apikey => {
+                       type => "string",
+                       example => "",
+                       description => "Google maps API key, Google layer not used if missing, see https://code.google.com/apis/console/ to get an API key",
+                       safe => 1,
+                       rebuild => 1,
+               },
 }
 
 sub register_rendered_files {
@@ -144,6 +150,7 @@ sub preprocess {
                lat => $lat,
                lon => $lon,
                href => $href,
+               google_apikey => $config{'osm_google_apikey'},
        };
        return "<div id=\"mapdiv-$name\"></div>";
 }
@@ -527,6 +534,7 @@ sub cgi($) {
                zoom => "urlParams['zoom']",
                fullscreen => 1,
                editable => 1,
+               google_apikey => $config{'osm_google_apikey'},
        );
        print "</script>";
        print "</body></html>";
@@ -537,9 +545,13 @@ sub cgi($) {
 sub embed_map_code(;$) {
        my $page=shift;
        my $olurl = $config{osm_openlayers_url} || "http://www.openlayers.org/api/OpenLayers.js";
-       return '<script src="'.$olurl.'" type="text/javascript" charset="utf-8"></script>'.
+       my $code = '<script src="'.$olurl.'" type="text/javascript" charset="utf-8"></script>'."\n".
                '<script src="'.urlto("ikiwiki/osm.js", $page).
                '" type="text/javascript" charset="utf-8"></script>'."\n";
+       if ($config{'osm_google_apikey'}) {
+           $code .= '<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='.$config{'osm_google_apikey'}.'&sensor=false" type="text/javascript" charset="utf-8"></script>';
+       }
+       return $code;
 }
 
 sub map_setup_code($;@) {
index 388d625f15039aea6734bb782910b8e6b6612135..9269bd8993250581b598cfd49abcef903ac04896 100644 (file)
@@ -34,6 +34,10 @@ function mapsetup(divname, options) {
                        new OpenLayers.Control.Permalink(permalink)
                ],
                displayProjection: new OpenLayers.Projection("EPSG:4326"),
+               maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
+               projection: "EPSG:900913",
+               units: "m",
+               maxResolution: 156543.0339,
                numZoomLevels: 18
        });
 
@@ -44,6 +48,17 @@ function mapsetup(divname, options) {
                map.addLayer(new OpenLayers.Layer.OSM());
        }
 
+       // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html
+       if (options.google_apikey && options.google_apikey != 'null') {
+               googleLayer = new OpenLayers.Layer.Google(
+                       "Google Hybrid",
+                       {type: G_HYBRID_MAP,
+                        'sphericalMercator': true,
+                        'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
+                        projection: new OpenLayers.Projection("EPSG:3857")}
+               );
+               map.addLayer(googleLayer);
+       }
        if (options.format == 'CSV') {
                pois = new OpenLayers.Layer.Text( "CSV",
                        { location: options.csvurl,