]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/osm.pm
osm: optimization: do not create duplicate styles
[ikiwiki.git] / IkiWiki / Plugin / osm.pm
index 764767525485d9df1fda2175ec5e5e0c1f832f0e..466006cbfb9463abd0bc7df481bbec064904d895 100644 (file)
@@ -67,7 +67,20 @@ sub getsetup () {
                        safe => 0,
                        rebuild => 1,
                },
-
+               osm_layers => {
+                       type => "string",
+                       example => { 'OSM', 'GoogleSatellite' },
+                       description => "Layers to use in the map. Can be either the 'OSM' string or a type option for Google maps (GoogleNormal, GoogleSatellite, GoogleHybrid or GooglePhysical). It can also be an arbitrary URL in a syntax acceptable for OpenLayers.Layer.OSM.url parameter.",
+                       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 +150,7 @@ sub preprocess {
                lat => $lat,
                lon => $lon,
                href => $href,
+               google_apikey => $config{'osm_google_apikey'},
        };
        return "<div id=\"mapdiv-$name\"></div>";
 }
@@ -351,17 +365,23 @@ sub writekml($;$) {
                $writer->startTag("Document");
 
                # first pass: get the icons
+               my %tags_map = (); # keep track of tags seen
                foreach my $name (keys %{$waypoints{$map}}) {
                        my %options = %{$waypoints{$map}{$name}};
-                       $writer->startTag("Style", id => $options{tag});
-                       $writer->startTag("IconStyle");
-                       $writer->startTag("Icon");
-                       $writer->startTag("href");
-                       $writer->characters($options{icon});
-                       $writer->endTag();
-                       $writer->endTag();
-                       $writer->endTag();
-                       $writer->endTag();
+                       if (!$tags_map{$options{tag}}) {
+                           debug("found new style " . $options{tag});
+                           $tags_map{$options{tag}} = ();
+                           $writer->startTag("Style", id => $options{tag});
+                           $writer->startTag("IconStyle");
+                           $writer->startTag("Icon");
+                           $writer->startTag("href");
+                           $writer->characters($options{icon});
+                           $writer->endTag();
+                           $writer->endTag();
+                           $writer->endTag();
+                           $writer->endTag();
+                       }
+                       $tags_map{$options{tag}}{$name} = \%options;
                }
        
                foreach my $name (keys %{$waypoints{$map}}) {
@@ -520,6 +540,7 @@ sub cgi($) {
                zoom => "urlParams['zoom']",
                fullscreen => 1,
                editable => 1,
+               google_apikey => $config{'osm_google_apikey'},
        );
        print "</script>";
        print "</body></html>";
@@ -530,9 +551,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($;@) {
@@ -540,6 +565,8 @@ sub map_setup_code($;@) {
        my $name=shift;
        my %options=@_;
 
+       my $mapurl = $config{osm_map_url};
+
        eval q{use JSON};
        error $@ if $@;
                                
@@ -556,6 +583,11 @@ sub map_setup_code($;@) {
                $options{'kmlurl'} = urlto($map."/pois.kml");
        }
 
+       if ($mapurl) {
+               $options{'mapurl'} = $mapurl;
+       }
+        $options{'layers'} = $config{osm_layers};
+
        return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");";
 }