-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart-map.js
55 lines (34 loc) · 2.21 KB
/
smart-map.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
/**
* @file module.exports of the whole set of capabilities, plus a few useful functions to convert between {lat,lng}, {latitude,longitude}, LatLng()
* @author devbab
*/
/*global google */
function geo2Short(coord) {
if (typeof coord !== "object") throw new Error(`Not an object`);
if (Object.prototype.hasOwnProperty.call(coord, "latitude") && Object.prototype.hasOwnProperty.call(coord, "longitude")) return { lat: coord.latitude, lng: coord.longitude };
// before test of lat lng, as it has also these 2 fields
if (typeof google !== "undefined" && coord instanceof google.maps.LatLng) return { lat: coord.lat(), lng: coord.lng() };
if (Object.prototype.hasOwnProperty.call(coord, "lat") && Object.prototype.hasOwnProperty.call(coord, "lng")) return coord;
throw new Error(`unknown format for object`);
}
function geo2Long(coord) {
if (typeof coord !== "object") throw `Can't convert ${coord}`;
// before test of lat lng, as it has also these 2 fields
if (typeof google !== "undefined" && coord instanceof google.maps.LatLng) return { latitude: coord.lat(), longitude: coord.lng() };
if (Object.prototype.hasOwnProperty.call(coord, "lat") && Object.prototype.hasOwnProperty.call(coord, "lng")) return { latitude: coord.lat, longitude: coord.lng };
if (Object.prototype.hasOwnProperty.call(coord, "latitude") && Object.prototype.hasOwnProperty.call(coord, "longitude")) return coord;
throw new Error(`unknown format for object`);
}
function geo2LatLng(coord) {
if (typeof google === "undefined") throw new Error(`google.maps.LatLng is not defined`);
if (typeof coord !== "object") throw `Can't convert ${coord}`;
if (Object.prototype.hasOwnProperty.call(coord, "lat") && Object.prototype.hasOwnProperty.call(coord, "lng")) return new google.maps.LatLng(coord.lat, coord.lng);
if (Object.prototype.hasOwnProperty.call(coord, "latitude") && Object.prototype.hasOwnProperty.call(coord, "longitude")) return new google.maps.LatLng(coord.latitude, coord.longitude);
throw new Error(`unknown format for object`);
}
module.exports = {
ml: require("./map-layer.js"),
sm: require("./smart-marker.js"),
zc: require("./zoom-center.js"),
geo2Short, geo2Long, geo2LatLng
};