customer.ejs
6.99 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script>
$(function () {
// Geolocation API에 액세스할 수 있는지를 확인
if (navigator.geolocation) {
//위치 정보를 얻기
navigator.geolocation.getCurrentPosition(function (pos) {
$('#latitude').html(pos.coords.latitude); // 위도
$('#longitude').html(pos.coords.longitude); // 경도
});
} else {
alert("이 브라우저에서는 Geolocation이 지원되지 않습니다.")
}
});
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyABf3oQzEyKYDxSDhpYYEKuSdgqprwODYU">
</script>
<script type="text/javascript">
function initialize() {
if (navigator.geolocation) {
//위치 정보를 얻기
navigator.geolocation.getCurrentPosition(function (pos) {
var latitude = pos.coords.latitude; // 위도
var longitude = pos.coords.longitude; // 경도
$('#latitude').html(latitude); // 위도
$('#longitude').html(longitude); // 경도
var mapLocation = new google.maps.LatLng(latitude, longitude); // 지도에서 가운데로 위치할 위도와 경도
var markerLocation = new google.maps.LatLng(latitude, longitude); // 마커가 위치할 위도와 경도
var mapOptions = {
center: mapLocation, // 지도에서 가운데로 위치할 위도와 경도(변수)
zoom: 18, // 지도 zoom단계
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), // id: map-canvas, body에 있는 div태그의 id와 같아야 함
mapOptions);
var size_x = 60; // 마커로 사용할 이미지의 가로 크기
var size_y = 60; // 마커로 사용할 이미지의 세로 크기
// 마커로 사용할 이미지 주소
var image = new google.maps.MarkerImage('/public/you.png',
new google.maps.Size(size_x, size_y),
'',
'',
new google.maps.Size(size_x, size_y));
//var marker;
//marker = new google.maps.Marker({
// position: markerLocation, // 마커가 위치할 위도와 경도(변수)
// map: map,
// icon: 'images/market.png', // 마커로 사용할 이미지(변수)
// title: '대박나는 장소' // 마커에 마우스 포인트를 갖다댔을 때 뜨는 타이틀
//});
var content = "현재 당신의 위치입니다. <br/>"; // 말풍선 안에 들어갈 내용
//alert(content);
// 마커를 클릭했을 때의 이벤트. 말풍선 뿅~
var infowindow = new google.maps.InfoWindow({content: content});
google.maps.event.addListener(marker, "click", function () {
infowindow.open(map, marker);
});
});
}
}
//google.maps.event.addDomListener(window, 'load',initialize());
</script>
</head>
<body>
<form action="/market" method="get">
<span id="latitude"></span>
<span id="longitude"></span>
</form>
<input type="submit" value="상점 조회하기" class="btn" id="ajaxsend"/>
<div id="googleMap" style="width:1000px;height:1000px;"></div>
</body>
<script>
document.querySelector('#ajaxsend').addEventListener('click', function () {
var lat = $("#latitude").text()
var lon = $("#longitude").text()
//var title = document.getElementsByName("title")[0].value;
//alert(lat)
//alert(lon)
sendAjax('http://localhost:3000/market', {'latitude': lat, 'longitude': lon});
});
function sendAjax(url, data) {
data = JSON.stringify(data);
alert(data)
var xhr = new XMLHttpRequest();
xhr.open('get', url);
xhr.setRequestHeader('Content-Type', "application/json");
xhr.send(data);
xhr.addEventListener('load', function () {
console.log(xhr.responseText);
var result = JSON.parse(xhr.responseText);
var latitude = result.data[0].latitude
var longitude= result.data[0].longitude
var title = result.data[0].title;
var mapLocation = new google.maps.LatLng(latitude, longitude); // 지도에서 가운데로 위치할 위도와 경도
var mapOptions = {
center: mapLocation, // 지도에서 가운데로 위치할 위도와 경도(변수)
zoom: 18, // 지도 zoom단계
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), // id: map-canvas, body에 있는 div태그의 id와 같아야 함
mapOptions);
var marker;
var markerLocation;
for(var i=0;i<result.data.length;i++) {
//alert(result.data[i].latitude);
//alert(result.data[i].longitude);
var latitude = result.data[i].latitude; // 위도
var longitude = result.data[i].longitude; // 경도
var title = result.data[i].title;
markerLocation = new google.maps.LatLng(latitude, longitude); // 마커가 위치할 위도와 경도
marker = new google.maps.Marker({
position: markerLocation, // 마커가 위치할 위도와 경도(변수)
map: map,
icon: 'images/market.png', // 마커로 사용할 이미지(변수)
title: title // 마커에 마우스 포인트를 갖다댔을 때 뜨는 타이틀
});
//marker.setMap(map);
var content = title + "이네 상점입니다~ <br/>"; // 말풍선 안에 들어갈 내용
var infowindow = new google.maps.InfoWindow({content: content});
// 마커를 클릭했을 때의 이벤트. 말풍선 뿅~
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
//nfowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
if (result == 1) {
//window.location.href = "/customer"
}
else {
//window.location.href = "/customer"
}
})
}
</script>
</html>