-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplore.js
80 lines (75 loc) · 1.82 KB
/
explore.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
hospitals = [
{
name: "aaa",
address: "台北市辛亥路二段157號"
},
{
name: "bbb",
address: "台北市辛亥路二段300號"
},
{
name: "ccc",
address: "台北市辛亥路一段20號"
},
{
name: "ddd",
address: "台北市汀州路三段100號"
},
{
name: "eee",
address: "高雄市凱旋三路1號"
}
];
// center the map on current location of the user
function centerOnCurrentLocation(map) {
// http://stackoverflow.com/questions/17382128/google-maps-api-center-map-on-clients-current-location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(initialLocation);
});
}
else {
alert("Your browser is not supported.");
}
}
// initilaize Google Map
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: {lat: 25.03, lng: 121.30}
});
centerOnCurrentLocation(map);
window.map = map;
}
// query GeoCode of the specified address and call callback() after it's finished.
function getGeoCode(map, addr, callback) {
var gc = new google.maps.Geocoder();
gc.geocode({address: addr}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
var n = results.length;
for(var i = 0; i < n; ++i) {
console.log(results[i]);
var pt = results[i].geometry.location;
callback({lat: pt.lat(), lng: pt.lng()});
}
}
});
}
// add a mark for the address on the map
function markAddress(map, address) {
getGeoCode(map, address, function(pos) {
var marker = new google.maps.Marker({
position: pos,
map: map /*,
icon: image */
});
});
}
function markSites(map, sites) {
var n = sites.length;
for(var i = 0; i < n; ++i) {
var site = sites[i];
markAddress(map, site.address);
}
}