-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.js
75 lines (68 loc) · 1.93 KB
/
view.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
var map;
var infoWindow;
var service;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 36.109979, lng: 140.101418},
zoom: 15,
styles: [{
stylers: [{ visibility: 'simplified' }]
}, {
elementType: 'labels',
stylers: [{ visibility: 'off' }]
}]
});
infoWindow = new google.maps.InfoWindow();
service = new google.maps.places.PlacesService(map);
// The idle event is a debounced event, so we can query & listen without
// throwing too many requests at the server.
map.addListener('idle', performSearch);
}
function performSearch() {
var request = {
location: new google.maps.LatLng(36.109979, 140.101418),
radius: 5000,
keyword: 'best view'
};
service.radarSearch(request, callback);
}
function callback(results, status) {
if (status !== google.maps.places.PlacesServiceStatus.OK) {
console.error(status);
return;
}
var max = 0;
var index = null;
for (var i = 0, result; result = results[i]; i++) {
addMarker(result);
var from = new google.maps.LatLng(36.109979, 140.101418);
var to = result.geometry.location;
var distance = google.maps.geometry.spherical.computeDistanceBetween(from, to);
if(max < distance){
max = distance;
index = i;
}
}
console.log(results[index]);
}
function addMarker(place) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: {
url: 'http://maps.gstatic.com/mapfiles/circle.png',
anchor: new google.maps.Point(10, 10),
scaledSize: new google.maps.Size(10, 17)
}
});
google.maps.event.addListener(marker, 'click', function() {
service.getDetails(place, function(result, status) {
if (status !== google.maps.places.PlacesServiceStatus.OK) {
console.error(status);
return;
}
infoWindow.setContent(result.name);
infoWindow.open(map, marker);
});
});
}