]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/osm.pm
make layers completely customizable
[ikiwiki.git] / IkiWiki / Plugin / osm.pm
index 6ba2524d81c25538671559fed0dcd19daa092b50..4c50fec538a6e37fa011f105eb93fbcd4475e407 100644 (file)
@@ -67,7 +67,29 @@ sub getsetup () {
                        safe => 0,
                        rebuild => 1,
                },
-
+               osm_layers => {
+                       type => "string",
+                       example => { OSM => 1,
+                                    Google => 'Hybrid',
+                       },
+                       description => "Layers to use in the map. If the value is 1, use the default for the map, otherwise the argument is a URL (for OSM layers, e.g. http://a.tile.stamen.com/toner/\${z}/\${x}/\${y}.png) or a type option for Google maps (Normal, Satellite, Hybrid or Physical).",
+                       safe => 0,
+                       rebuild => 1,
+               },
+               osm_layers_order => {
+                       type => "string",
+                       example => { 'OSM', 'Google' },
+                       description => "Display order for the layers. The first layer is the default layer, must match exactly the left side of the osm_layers hash.",
+                       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 {
@@ -137,6 +159,7 @@ sub preprocess {
                lat => $lat,
                lon => $lon,
                href => $href,
+               google_apikey => $config{'osm_google_apikey'},
        };
        return "<div id=\"mapdiv-$name\"></div>";
 }
@@ -348,6 +371,7 @@ sub writekml($;$) {
                        DATA_MODE => 1, ENCODING => 'UTF-8');
                $writer->xmlDecl();
                $writer->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2");
+               $writer->startTag("Document");
 
                # first pass: get the icons
                foreach my $name (keys %{$waypoints{$map}}) {
@@ -407,6 +431,7 @@ sub writekml($;$) {
                        $writer->endTag();
                }
                $writer->endTag();
+               $writer->endTag();
                $writer->end();
 
                writefile("pois.kml", $config{destdir} . "/$map", $output);
@@ -518,6 +543,7 @@ sub cgi($) {
                zoom => "urlParams['zoom']",
                fullscreen => 1,
                editable => 1,
+               google_apikey => $config{'osm_google_apikey'},
        );
        print "</script>";
        print "</body></html>";
@@ -528,9 +554,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($;@) {
@@ -538,6 +568,8 @@ sub map_setup_code($;@) {
        my $name=shift;
        my %options=@_;
 
+       my $mapurl = $config{osm_map_url};
+
        eval q{use JSON};
        error $@ if $@;
                                
@@ -554,6 +586,12 @@ sub map_setup_code($;@) {
                $options{'kmlurl'} = urlto($map."/pois.kml");
        }
 
+       if ($mapurl) {
+               $options{'mapurl'} = $mapurl;
+       }
+        $options{'layers'} = $config{osm_layers};
+        $options{'layers_order'} = $config{osm_layers_order};
+
        return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");";
 }