This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathxivelygmaps.js
93 lines (76 loc) · 2.4 KB
/
xivelygmaps.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
// xively-gmaps
// version 0.1.1
// (c) 2013 Xively
// http://xively.github.com/xively-gmaps/
// BSD 3-Clause license
var xivelyGmaps = (function ($) {
"use strict";
var methods,
APIendpoint = "https://api.xively.com/v2/",
WSendpoint = "wss://api.xively.com:8080/",
previousLocation,
map,
marker,
mapElement = "#map-canvas";
methods = {
setKey: function(apiKey) {
xively.setKey(apiKey);
},
setApiEndpoint: function(api_endpoint) {
xively.setEndpoint(api_endpoint || APIendpoint)
},
setWsEndpoint: function(ws_endpoint) {
xively.setWSEndpoint();
},
subscribe: function(feed_id) {
var self = this;
xively.feed.get(feed_id, function(feedData) {
self.renderMap(feedData);
});
xively.feed.subscribe(feed_id, function(event, feedData){
self.renderMap(feedData);
});
},
setMapElement: function(ele) {
mapElement = ele;
},
renderMap: function(feedData) {
var self = this;
var location = feedData.location;
if (feedData && self.checkValidLocation(location)) {
var position = new google.maps.LatLng(location.lat, location.lon);
var markerTitle = "Feed id: " + feedData.id + "\n" +
"Latitude: " + location.lat + "\n" +
"Longitude: " + location.lon;
if ( typeof(previousLocation) === 'undefined' ) {
var mapOptions = {
center: position,
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROAD_MAP
};
map = new google.maps.Map($(mapElement)[0], mapOptions);
self.setMarker(position, markerTitle);
} else if ((location.lat !== previousLocation.lat) || (location.lon !== previousLocation.lon)) {
if (marker) { marker.setMap(null); }
self.setMarker(position);
map.setCenter(position, markerTitle);
}
previousLocation = location;
} else {
$(mapElement).html("Awaiting Location Data");
}
},
checkValidLocation: function(location) {
return location && location.lat && location.lon;
},
setMarker: function(position, title) {
marker = new google.maps.Marker({
position: position,
map: map,
zoom: 13,
title: title
});
}
}
return methods;
})(jQuery);