X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/409c4e48f983d10aceb6321148d7f440d17eb28f..d8f12120d15cdbe22507842f0aba04ead294bcba:/IkiWiki/Plugin/osm.pm

diff --git a/IkiWiki/Plugin/osm.pm b/IkiWiki/Plugin/osm.pm
index 6ba2524d8..27db31af5 100644
--- a/IkiWiki/Plugin/osm.pm
+++ b/IkiWiki/Plugin/osm.pm
@@ -14,7 +14,7 @@ sub import {
 	hook(type => "format", id => "osm", call => \&format);
 	hook(type => "preprocess", id => "osm", call => \&preprocess);
 	hook(type => "preprocess", id => "waypoint", call => \&process_waypoint);
-	hook(type => "savestate", id => "waypoint", call => \&savestate);
+	hook(type => "changes", id => "waypoint", call => \&changes);
 	hook(type => "cgi", id => "osm", call => \&cgi);
 }
 
@@ -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>";
 }
@@ -178,6 +192,7 @@ sub process_waypoint {
 		}
 	}
 	$icon = urlto($icon, $dest, 1);
+	$icon =~ s!/*$!!; # hack - urlto shouldn't be appending a slash in the first place
 	$tag = '' unless $tag;
 	register_rendered_files($map, $page, $dest);
 	$pagestate{$page}{'osm'}{$map}{'waypoints'}{$name} = {
@@ -218,7 +233,7 @@ sub get_tag_icon($) {
 	my $tag = shift;
 	# look for an icon attached to the tag
 	my $attached = $tag . '/' . $config{'osm_tag_default_icon'};
-	if (srcfile($attached)) {
+	if (srcfile($attached, 1)) {
 		return $attached;
 	}
 	else {
@@ -265,7 +280,7 @@ sub scrub_lonlat($$$) {
 	return ($lon, $lat);
 }
 
-sub savestate {
+sub changes {
 	my %waypoints = ();
 	my %linestrings = ();
 
@@ -345,22 +360,29 @@ sub writekml($;$) {
 	foreach my $map (keys %waypoints) {
 		my $output;
 		my $writer = XML::Writer->new( OUTPUT => \$output,
-			DATA_MODE => 1, ENCODING => 'UTF-8');
+			DATA_MODE => 1, DATA_INDENT => ' ', ENCODING => 'UTF-8');
 		$writer->xmlDecl();
 		$writer->startTag("kml", "xmlns" => "http://www.opengis.net/kml/2.2");
+		$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}}) {
@@ -407,6 +429,7 @@ sub writekml($;$) {
 			$writer->endTag();
 		}
 		$writer->endTag();
+		$writer->endTag();
 		$writer->end();
 
 		writefile("pois.kml", $config{destdir} . "/$map", $output);
@@ -484,7 +507,7 @@ sub include_javascript ($) {
 		}
 	}
 	if ($loader) {
-		return embed_map_code($page) . "<script type=\"text/javascript\" charset=\"utf-8\">$loader</script>";
+		return embed_map_code($page) . "<script type=\"text/javascript\">$loader</script>";
 	}
 	else {
         	return '';
@@ -511,13 +534,14 @@ sub cgi($) {
 	print "<html><body>";
 	print "<div id=\"mapdiv-$map\"></div>";
 	print embed_map_code();
-	print "<script type=\"text/javascript\" charset=\"utf-8\">";
+	print "<script type=\"text/javascript\">";
 	print map_setup_code($map, $map,
 		lat => "urlParams['lat']",
 		lon => "urlParams['lon']",
 		zoom => "urlParams['zoom']",
 		fullscreen => 1,
 		editable => 1,
+		google_apikey => $config{'osm_google_apikey'},
 	);
 	print "</script>";
 	print "</body></html>";
@@ -528,9 +552,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 +566,8 @@ sub map_setup_code($;@) {
 	my $name=shift;
 	my %options=@_;
 
+	my $mapurl = $config{osm_map_url};
+
 	eval q{use JSON};
 	error $@ if $@;
 				
@@ -554,6 +584,12 @@ sub map_setup_code($;@) {
 		$options{'kmlurl'} = urlto($map."/pois.kml");
 	}
 
+	if ($mapurl) {
+		$options{'mapurl'} = $mapurl;
+	}
+        $options{'layers'} = $config{osm_layers};
+
+	$name=~s/'//g; # $name comes from user input
 	return "mapsetup('mapdiv-$name', " . to_json(\%options) . ");";
 }