-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (71 loc) · 2.08 KB
/
index.js
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
'use strict'
var noop = function() { }
, Emitter = require('emitter')
function GMap(element) {
this.element = element
this.markers = []
this.markerOptions = {}
this.tooltips = null
}
GMap.prototype.addMarkers = function(pairs) {
var i; for (i = 0; i < pairs.length; i++) {
this.markers.push(pairs[i])
}
}
GMap.prototype.init = function(done) {
done = done || noop
if (this._initialized) done()
else {
var jsonp = require('jsonp')
, gmap = this
jsonp('http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false', {}, function() {
gmap._initialized = true
gmap._initMap()
gmap._initMarkers()
done()
})
}
}
GMap.prototype._initMap = function() {
this.map = new google.maps.Map(this.element, {
mapTypeId: 'roadmap',
streetViewControl: false
})
this.map.fitBounds(this.getBounds())
}
GMap.prototype._initMarkers = function() {
var gmap = this
var i; for (i = 0; i < this.markers.length; i++) {
var pair = this.markers[i]
, options = {}
for (var key in this.markerOptions) {
if (this.markerOptions.hasOwnProperty(key)) options[key] = this.markerOptions[key]
}
options.position = new google.maps.LatLng(pair[0], pair[1])
options.map = this.map
var marker = new google.maps.Marker(options)
google.maps.event.addListener(marker, 'click', (function(i, marker) {
return function() {
gmap._showInfoWindow(i, marker)
gmap.emit('marker.click', i, marker)
}
})(i, marker))
}
}
GMap.prototype.getBounds = function() {
var bounds = new google.maps.LatLngBounds()
var i; for (i = 0; i < this.markers.length; i++) {
var pair = this.markers[i]
bounds.extend(new google.maps.LatLng(pair[0], pair[1]))
}
return bounds
}
GMap.prototype._showInfoWindow = function(index, marker) {
if (this.tooltips === null) return
var html = this.tooltips(index)
this._infoWindow = this._infoWindow || new google.maps.InfoWindow()
this._infoWindow.setContent(html)
this._infoWindow.open(this.map, marker)
}
Emitter(GMap.prototype)
module.exports = GMap