geolocation.blade.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <title>Print Preview</title>
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <link href="{{ csset('/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css">
  9. <script src="{{ csset('/js/main/jquery.min.js') }}"></script>
  10. <script src="{{ csset('/js/external/jquery-ui.js') }}"></script>
  11. <script src="{{ csset('/js/main/bootstrap.bundle.min.js') }}"></script>
  12. <script src="{{ csset('/js/utils/distance.js') }}"></script>
  13. <script type="text/javascript" src="//dapi.kakao.com/v2/maps/sdk.js?appkey=1b0c21220a0a5d2f4f4869f1e182bb07&libraries=services"></script>
  14. <script src="//t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>
  15. <style>
  16. @media print {
  17. @page { margin: 0; }
  18. body { margin: 1.6cm; }
  19. }
  20. body {
  21. font-family: Arial, Helvetica, Gulim, sans-serif;
  22. font-size: 12px;
  23. line-height: 1.42857143;
  24. color: #000000;
  25. background-color: #ffffff;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <h1 class="mb-2 text-center text-danger">거리: <span id="distance"></span></h1>
  31. <hr>
  32. <div class="d-flex align-items-center flex-column">
  33. <div class="d-flex align-items-center">
  34. <h1 class="mb-0">가게위치</h1>
  35. <button onclick="getZipCode()" class="btn btn-primary mb-1 ml-2">
  36. 가게위치 선택
  37. </button>
  38. </div>
  39. <div>
  40. <h1>주소: <span id="addr"></span></h1>
  41. <h1>위도: <span id="fromLat"></span></h1>
  42. <h1>경도: <span id="fromLon"></span></h1>
  43. </div>
  44. <div id="fromMap" style="margin: auto; width:800px; height:800px;"></div>
  45. </div>
  46. <div style="text-align: center;">
  47. <h1 style="margin-top: 30px;">현재위치</h1>
  48. <h1>위도: <span id="toLat"></span></h1>
  49. <h1>경도: <span id="toLon"></span></h1>
  50. <div id="toMap" style="margin: auto; width:800px; height:800px;"></div>
  51. </div>
  52. </body>
  53. </html>
  54. <script>
  55. function getZipCode(){
  56. new daum.Postcode({
  57. oncomplete: function(data) {
  58. document.getElementById('addr').innerHTML = data.roadAddress + `(${data.zonecode})`
  59. var geocoder = new kakao.maps.services.Geocoder();
  60. const { address } = data;
  61. geocoder.addressSearch(address, (result, status) => {
  62. const { x, y } = result[0];
  63. getLocation(y, x)
  64. });
  65. },
  66. width: "100%",
  67. height: window.innerHeight
  68. }).open();
  69. }
  70. const getLocation = (fromLat, fromLon) => {
  71. if (navigator.geolocation) {
  72. // GPS를 지원하면
  73. navigator.geolocation.getCurrentPosition(
  74. (position) => {
  75. // position 객체 내부에 timestamp(현재 시간)와 coords 객체
  76. const time = new Date(position.timestamp);
  77. test(fromLat, fromLon, 'fromMap')
  78. document.getElementById('fromLat').innerHTML = fromLat;
  79. document.getElementById('fromLon').innerHTML = fromLon;
  80. const toLat = position.coords.latitude
  81. const toLon = position.coords.longitude
  82. test(toLat, toLon, 'toMap')
  83. document.getElementById('toLat').innerHTML = toLat;
  84. document.getElementById('toLon').innerHTML = toLon;
  85. // const toLat = '35.13513513513514'
  86. // const toLon = '129.087748126891'
  87. const dist = distance(fromLat, fromLon, toLat, toLon);
  88. document.getElementById('distance').innerHTML = dist + ' km';
  89. // alert(`위도 : ${position.coords.latitude} 경도 : ${position.coords.longitude}`);
  90. return position;
  91. },
  92. (error) => {
  93. console.error(error);
  94. },
  95. {
  96. enableHighAccuracy: true,
  97. maximumAge: 30000,
  98. timeout: 27000
  99. }
  100. );
  101. } else {
  102. alert("GPS를 지원하지 않습니다");
  103. }
  104. };
  105. function test(latitude, longitude, id) {
  106. var container = document.getElementById(id);
  107. var options = {
  108. center: new kakao.maps.LatLng(latitude, longitude),
  109. level: 3
  110. };
  111. var map = new kakao.maps.Map(container, options);
  112. var marker = new kakao.maps.Marker({
  113. // 지도 중심좌표에 마커를 생성합니다
  114. position: map.getCenter()
  115. });
  116. // 지도에 마커를 표시합니다
  117. marker.setMap(map);
  118. }
  119. </script>