customer.ejs 6.99 KB
<!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>