]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/osm.pm
make layers completely customizable
[ikiwiki.git] / IkiWiki / Plugin / osm.pm
index e96a1283752ff5dfe721d73fd09f22746831413d..4c50fec538a6e37fa011f105eb93fbcd4475e407 100644 (file)
@@ -60,6 +60,36 @@ sub getsetup () {
                        safe => 0,
                        rebuild => 1,
                },
+               osm_openlayers_url => {
+                       type => "string",
+                       example => "http://www.openlayers.org/api/OpenLayers.js",
+                       description => "Url for the OpenLayers.js file",
+                       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 {
@@ -129,6 +159,7 @@ sub preprocess {
                lat => $lat,
                lon => $lon,
                href => $href,
+               google_apikey => $config{'osm_google_apikey'},
        };
        return "<div id=\"mapdiv-$name\"></div>";
 }
@@ -340,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}}) {
@@ -399,6 +431,7 @@ sub writekml($;$) {
                        $writer->endTag();
                }
                $writer->endTag();
+               $writer->endTag();
                $writer->end();
 
                writefile("pois.kml", $config{destdir} . "/$map", $output);
@@ -510,6 +543,7 @@ sub cgi($) {
                zoom => "urlParams['zoom']",
                fullscreen => 1,
                editable => 1,
+               google_apikey => $config{'osm_google_apikey'},
        );
        print "</script>";
        print "</body></html>";
@@ -519,9 +553,14 @@ sub cgi($) {
 
 sub embed_map_code(;$) {
        my $page=shift;
-       return '<script src="http://www.openlayers.org/api/OpenLayers.js" type="text/javascript" charset="utf-8"></script>'.
+       my $olurl = $config{osm_openlayers_url} || "http://www.openlayers.org/api/OpenLayers.js";
+       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($;@) {
@@ -529,6 +568,8 @@ sub map_setup_code($;@) {
        my $name=shift;
        my %options=@_;
 
+       my $mapurl = $config{osm_map_url};
+
        eval q{use JSON};
        error $@ if $@;
                                
@@ -545,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) . ");";
 }