summaryrefslogtreecommitdiff
path: root/httemplate/search/elements/gmap.html
blob: 69fdc5a09f8af737f7565f9dcb3740e070fe288c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<%args>
@features
@overlays
</%args>
<%doc>
Generic Google Maps front end.

<& /elements/gmap.html,
  features => [
    { id => 'svc_acct/12',
      geometry => {
        type        => 'Point',
        coordinates => [ -86, 40 ], # optionally altitude as the third coord
      },
      properties => {
        # see https://developers.google.com/maps/documentation/javascript/3.exp/reference#Data.StyleOptions
        style => {
          icon => {
            scale => 4,
            fillColor => 'orange',
          }
        },
        # content of popup info box (might AJAX this later)
        content => '<a href="view/svc_acct.cgi?12">username@example.com</a>',
      }
    }, # 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>
<%init>

my $apikey = FS::Conf->new->config('google_maps_api_key');

foreach (@features) {
  $_->{type} = 'Feature';
  # any other per-feature massaging can go here
}
my $tree = {
  type => 'FeatureCollection',
  features => \@features
};

</%init>
<div id="map_canvas"></div>

<style type="text/css">
html { height: 100% }

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&key=<% $apikey %>">
</script>

<script type="text/javascript">

var data_geojson = <% encode_json($tree) %>;
var data_overlays = <% encode_json(\@overlays) %>;

var baseStyle = {
  clickable: true,
  icon: {
    path: google.maps.SymbolPath.CIRCLE,
    scale: 4,
    fillColor: 'green',
    fillOpacity: 1,
    strokeColor: 'black',
    strokeWeight: 1,
  },
};

var featureStyle = function(feature) {
  // jQuery.extend(): merge properties of objects
  // 'true' makes it a deep copy; start the merge with {} so that
  // baseStyle doesn't get overwritten
  return $.extend(true, {}, baseStyle, feature.getProperty('style'));
};

var map;
var overlays = [];
var infoWindow; // shared among all users

var clickHandler = function(ev) {
  var feature = ev.feature;
  if ( feature.getGeometry().getType() == 'Point' ) {
    // then pop up an info box with the feature content
    infoWindow.close();
    infoWindow.setPosition(feature.getGeometry().get());

    if ( feature.getProperty('content') ) {
      infoWindow.setContent(feature.getProperty('content'));
    } else {
      infoWindow.setContent('');
    }

    if ( feature.getProperty('url') ) {
      $.ajax({
        url: feature.getProperty('url'),
        success: function(data) {
          infoWindow.setContent(data);
        }
      });
      infoWindow.open(map);
    } else {
      infoWindow.open(map);
    }
  }

  // snap to feature ROI if it has one
  if ( feature.getProperty('bounds') ) {
    map.fitBounds( feature.getProperty('bounds') );
  }

};

var initMap = function() {
  var canvas = $('#map_canvas');
  map = new google.maps.Map(canvas[0], { zoom: 6 });
  try {
    map.data.addGeoJson(data_geojson);
  } catch(ex) {
    console.log(ex.toString);
    debugger;
  }

  // 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); });
    }
  });

  map.fitBounds(bounds);
  map.data.setStyle(featureStyle);

  infoWindow = new google.maps.InfoWindow;
  map.data.addListener('click', clickHandler);
  // xxx remove this later
  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 );
</script>