-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_map.html
153 lines (129 loc) · 4.65 KB
/
test_map.html
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html>
<head>
<title>Place Searches</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
<style>
body {
background-color: #FAF3E3;
}
#random_field {
text-align: center;
}
#map {
width: 70%;
height: 40em;
margin: 0 auto;
background-color: white;
}
</style>
</head>
<body>
<nav>
<ul>
<li class="menu"><a href="member.html">Member</a></li>
<li class="menu"><a href="">ยังไม่รู้จะทำเป็นหน้าอะไร</a></li>
<li class="menu"><a class="active" href="test_map.html">Map</a></li>
<li class="menu"><a href="random.html">Random</a></li>
<li style="float: left" id="logo"><a id="img" href="index.html"><img src="images/icon/logo_new.png"></a>
</li>
</ul>
</nav>
<article>
<div id="random_field">
<input type="button" value="Search" onclick="initMap()">
<div id="map"></div>
</div>
</article>
<script>
var map;
var service;
var infowindow;
function initMap() {
infoWindow = new google.maps.InfoWindow;
// HTML5 geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
initialize(pos.lat, pos.lng);
}, function () {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
function initialize(lat, lng) {
var myLocation = new google.maps.LatLng(lat, lng);
map = new google.maps.Map(document.getElementById('map'), {
center: myLocation,
zoom: 15
});
map.setCenter(myLocation);
var marker = new google.maps.Marker({
map: map,
position: myLocation,
title: "Me",
icon: "images/icon/me_maker.png"
});
marker.setMap(map);
var infowindow = new google.maps.InfoWindow({
content: "I'm here"
});
infowindow.open(map, marker);
var request = {
location: myLocation,
radius: '500',
query: 'ข้าวมันไก่'
};
service = new google.maps.places.PlacesService(map);
service.textSearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
titile: place.name,
icon: "images/icon/food_maker.png"
});
var content = place.name + "<br>" + place.formatted_address
var infowindow = new google.maps.InfoWindow({
content: content
});
google.maps.event.addListener(marker, 'mouseover', function () {
infowindow.open(map, marker);
});
google.maps.event.addListener(marker, 'mouseout', function () {
infowindow.close();
});
google.maps.event.addListener(marker, 'click', function () {
map.setZoom(20);
map.setCenter(marker.getPosition());
});
}
</script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=<api_key>&libraries=places®ion=TH&callback=initMap">
</script>
</body>
</html>