-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
94 lines (76 loc) · 2.41 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
require('angular');
require('typeahead.an'); // we are going to use typeahead in this demo
require('an').controller(DemoCtrl);
require('an').run();
function DemoCtrl($scope, $http) {
$scope.getLocationString = function(input) {
return getAddress(input).then(mapToFormattedAddress);
};
$scope.getLocationObject = function(input) {
return getAddress(input).then(mapToLocationObject);
};
$scope.formatAddress = function(modelOrString) {
if (typeof modelOrString === 'string' || !modelOrString) {
return "";
}
return modelOrString.label;
};
function getAddress(input) {
return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {
params: {
address: input,
sensor: false
}
}).then(function (res) {
return res.data.results.slice(0, 4);
});
}
function mapToLocationObject(results) {
var prefix = "https://maps.googleapis.com/maps/api/staticmap?size=400x120";
return results.map(function(item) {
var center = item.geometry.location;
var mapUrl = prefix;
mapUrl += '¢er=' + center.lat + ',' + center.lng;
mapUrl += '&markers=size:med|' + center.lat + ',' + center.lng;
mapUrl += '&zoom=' + getBoundsZoomLevel(item.geometry.bounds, {
width: 400,
height: 120
});
return {
label: item.formatted_address,
mapUrl: mapUrl
};
});
}
function mapToFormattedAddress(results) {
return results.map(function(item) {
return item.formatted_address;
});
}
}
function getBoundsZoomLevel(bounds, mapDim) {
var WORLD_DIM = {
height: 256,
width: 256
};
var ZOOM_MAX = 21;
if (!bounds) {
return 13;// something is wrong.
}
var ne = bounds.northeast;
var sw = bounds.southwest;
var latFraction = (latRad(ne.lat) - latRad(sw.lat)) / Math.PI;
var lngDiff = ne.lng - sw.lng;
var lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;
var latZoom = zoom(mapDim.height, WORLD_DIM.height, latFraction);
var lngZoom = zoom(mapDim.width, WORLD_DIM.width, lngFraction);
return Math.min(latZoom, lngZoom, ZOOM_MAX);
function latRad(lat) {
var sin = Math.sin(lat * Math.PI / 180);
var radX2 = Math.log((1 + sin) / (1 - sin)) / 2;
return Math.max(Math.min(radX2, Math.PI), -Math.PI) / 2;
}
function zoom(mapPx, worldPx, fraction) {
return Math.floor(Math.log(mapPx / worldPx / fraction) / Math.LN2);
}
}