RT# 81150 Google now requires api key for drawing api
[freeside.git] / httemplate / search / elements / gmap.html
index 8b070eb..399ed83 100644 (file)
@@ -1,5 +1,6 @@
 <%args>
 @features
+@overlays
 </%args>
 <%doc>
 Generic Google Maps front end.
@@ -24,6 +25,14 @@ Generic Google Maps front end.
       }
     }, # end of feature
   ],
+  overlays => [
+    { url => 'https://localhost/freeside/view/sector_map-png.html?102',
+      west  => -130.0,
+      east  => -128.0,
+      south => 10.0,
+      north => 12.0,
+    }, # make a ground overlay
+  ],
 &>
 
 </%doc>
@@ -37,7 +46,9 @@ my $tree = {
   features => \@features
 };
 
+my $apikey = FS::Conf->new->config('google_maps_api_key');
 </%init>
+
 <div id="map_canvas"></div>
 
 <style type="text/css">
@@ -48,12 +59,13 @@ body { height: 100%; margin: 0px; padding: 0px }
 #map_canvas { height: 100%; }
 </style>
 
-<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3">
+<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3<% $apikey ? "&key=$apikey" : '' %>">
 </script>
 
 <script type="text/javascript">
 
 var data_geojson = <% encode_json($tree) %>;
+var data_overlays = <% encode_json(\@overlays) %>;
 
 var baseStyle = {
   clickable: true,
@@ -75,6 +87,7 @@ var featureStyle = function(feature) {
 };
 
 var map;
+var overlays = [];
 function initMap() {
   var canvas = $('#map_canvas');
   map = new google.maps.Map(canvas[0], { zoom: 6 });
@@ -88,11 +101,16 @@ function initMap() {
   // construct bounds around all of the features
   var bounds = new google.maps.LatLngBounds;
   map.data.forEach(function(feature) {
-    var g = feature.getGeometry();
-    if (g.getType() == 'Point') {
-      bounds.extend(g.get());
-    } else if (g.getArray) {
-      g.getArray().forEach(function(point) { bounds.extend(point); });
+    var b = feature.getProperty('bounds');
+    if (b) { // if it specifies an ROI, include all of it 
+      bounds.union(b);
+    } else {
+      var g = feature.getGeometry();
+      if (g.getType() == 'Point') {
+        bounds.extend(g.get());
+      } else if (g.getArray) {
+        g.getArray().forEach(function(point) { bounds.extend(point); });
+      }
     }
   });
 
@@ -116,6 +134,15 @@ function initMap() {
     }
 
   }); // addListener()
+
+  data_overlays.forEach(function(x) {
+    var url = x.url;
+    delete x.url;
+    var overlay = new google.maps.GroundOverlay( url, x );
+    overlay.setMap(map);
+    overlay.setOpacity(0.4);
+    overlays.push(overlay); 
+  });
 }
 
 $().ready( initMap );