Guest User

Untitled

a guest
Sep 5th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. <!DOCTYPE html >
  2. <head>
  3. <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  4. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  5. <title>Creating a Store Locator on Google Maps</title>
  6. <style>
  7. /* Always set the map height explicitly to define the size of the div
  8. * element that contains the map. */
  9. #map {
  10. height: 100%;
  11. }
  12. /* Optional: Makes the sample page fill the window. */
  13. html, body {
  14. height: 100%;
  15. margin: 0;
  16. padding: 0;
  17. }
  18. </style>
  19. </head>
  20. <body style="margin:0px; padding:0px;" onload="initMap()">
  21. <div>
  22. <label for="raddressInput">Search location:</label>
  23. <input type="text" id="addressInput" size="15"/>
  24. <label for="radiusSelect">Radius:</label>
  25. <select id="radiusSelect" label="Radius">
  26. <option value="50" selected>50 kms</option>
  27. <option value="30">30 kms</option>
  28. <option value="20">20 kms</option>
  29. <option value="10">10 kms</option>
  30. </select>
  31.  
  32. <input type="button" id="searchButton" value="Search"/>
  33. </div>
  34. <div><select id="locationSelect" style="width: 10%; visibility: hidden"></select></div>
  35. <div id="map" style="width: 100%; height: 90%"></div>
  36. <script>
  37. var map;
  38. var markers = [];
  39. var infoWindow;
  40. var locationSelect;
  41.  
  42. function initMap() {
  43. var sydney = {lat: -33.863276, lng: 151.107977};
  44. map = new google.maps.Map(document.getElementById('map'), {
  45. center: sydney,
  46. zoom: 11,
  47. mapTypeId: 'roadmap',
  48. mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
  49. });
  50. infoWindow = new google.maps.InfoWindow();
  51.  
  52. searchButton = document.getElementById("searchButton").onclick = searchLocations;
  53.  
  54. locationSelect = document.getElementById("locationSelect");
  55. locationSelect.onchange = function() {
  56. var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
  57. if (markerNum != "none"){
  58. google.maps.event.trigger(markers[markerNum], 'click');
  59. }
  60. };
  61. }
  62.  
  63. function searchLocations() {
  64. var address = document.getElementById("addressInput").value;
  65. var geocoder = new google.maps.Geocoder();
  66. geocoder.geocode({address: address}, function(results, status) {
  67. if (status == google.maps.GeocoderStatus.OK) {
  68. searchLocationsNear(results[0].geometry.location);
  69. } else {
  70. alert(address + ' not found');
  71. }
  72. });
  73. }
  74.  
  75. function clearLocations() {
  76. infoWindow.close();
  77. for (var i = 0; i < markers.length; i++) {
  78. markers[i].setMap(null);
  79. }
  80. markers.length = 0;
  81.  
  82. locationSelect.innerHTML = "";
  83. var option = document.createElement("option");
  84. option.value = "none";
  85. option.innerHTML = "See all results:";
  86. locationSelect.appendChild(option);
  87. }
  88.  
  89. function searchLocationsNear(center) {
  90. clearLocations();
  91.  
  92. var radius = document.getElementById('radiusSelect').value;
  93. var searchUrl = 'storelocator.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
  94. downloadUrl(searchUrl, function(data) {
  95. var xml = parseXml(data);
  96. var markerNodes = xml.documentElement.getElementsByTagName("marker");
  97. var bounds = new google.maps.LatLngBounds();
  98. for (var i = 0; i < markerNodes.length; i++) {
  99. var id = markerNodes[i].getAttribute("id");
  100. var name = markerNodes[i].getAttribute("name");
  101. var address = markerNodes[i].getAttribute("address");
  102. var distance = parseFloat(markerNodes[i].getAttribute("distance"));
  103. var latlng = new google.maps.LatLng(
  104. parseFloat(markerNodes[i].getAttribute("lat")),
  105. parseFloat(markerNodes[i].getAttribute("lng")));
  106.  
  107. createOption(name, distance, i);
  108. createMarker(latlng, name, address);
  109. bounds.extend(latlng);
  110. }
  111. map.fitBounds(bounds);
  112. locationSelect.style.visibility = "visible";
  113. locationSelect.onchange = function() {
  114. var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
  115. google.maps.event.trigger(markers[markerNum], 'click');
  116. };
  117. });
  118. }
  119.  
  120. function createMarker(latlng, name, address) {
  121. var html = "<b>" + name + "</b> <br/>" + address;
  122. var marker = new google.maps.Marker({
  123. map: map,
  124. position: latlng
  125. });
  126. google.maps.event.addListener(marker, 'click', function() {
  127. infoWindow.setContent(html);
  128. infoWindow.open(map, marker);
  129. });
  130. markers.push(marker);
  131. }
  132.  
  133. function createOption(name, distance, num) {
  134. var option = document.createElement("option");
  135. option.value = num;
  136. option.innerHTML = name;
  137. locationSelect.appendChild(option);
  138. }
  139.  
  140. function downloadUrl(url, callback) {
  141. var request = window.ActiveXObject ?
  142. new ActiveXObject('Microsoft.XMLHTTP') :
  143. new XMLHttpRequest;
  144.  
  145. request.onreadystatechange = function() {
  146. if (request.readyState == 4) {
  147. request.onreadystatechange = doNothing;
  148. callback(request.responseText, request.status);
  149. }
  150. };
  151.  
  152. request.open('GET', url, true);
  153. request.send(null);
  154. }
  155.  
  156. function parseXml(str) {
  157. if (window.ActiveXObject) {
  158. var doc = new ActiveXObject('Microsoft.XMLDOM');
  159. doc.loadXML(str);
  160. return doc;
  161. } else if (window.DOMParser) {
  162. return (new DOMParser).parseFromString(str, 'text/xml');
  163. }
  164. }
  165.  
  166. function doNothing() {}
  167. </script>
  168. <script async defer
  169. src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCbn9gIka8i-33HotlIor7GHMt2WEo_aAQ&callback=initMap">
  170. </script>
  171. </body>
  172. </html>
  173.  
  174. <?php
  175. require("phpsqlsearch_dbinfo.php");
  176. // Get parameters from URL
  177. $center_lat = $_GET["lat"];
  178. $center_lng = $_GET["lng"];
  179. $radius = $_GET["radius"];
  180. // Start XML file, create parent node
  181. $dom = new DOMDocument("1.0");
  182. $node = $dom->createElement("markers");
  183. $parnode = $dom->appendChild($node);
  184. // Opens a connection to a mySQL server
  185. $connection=mysql_connect ($server, $username, $password);
  186. if (!$connection) {
  187. die("Not connected : " . mysql_error());
  188. }
  189. // Set the active mySQL database
  190. $db_selected = mysql_select_db($database, $connection);
  191. if (!$db_selected) {
  192. die ("Can't use db : " . mysql_error());
  193. }
  194. // Search the rows in the markers table
  195. $query = sprintf("SELECT id, name, address, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
  196. mysql_real_escape_string($center_lat),
  197. mysql_real_escape_string($center_lng),
  198. mysql_real_escape_string($center_lat),
  199. mysql_real_escape_string($radius));
  200. $result = mysql_query($query);
  201. $result = mysql_query($query);
  202. if (!$result) {
  203. die("Invalid query: " . mysql_error());
  204. }
  205. header("Content-type: text/xml");
  206. // Iterate through the rows, adding XML nodes for each
  207. while ($row = @mysql_fetch_assoc($result)){
  208. $node = $dom->createElement("marker");
  209. $newnode = $parnode->appendChild($node);
  210. $newnode->setAttribute("id", $row['id']);
  211. $newnode->setAttribute("name", $row['name']);
  212. $newnode->setAttribute("address", $row['address']);
  213. $newnode->setAttribute("lat", $row['lat']);
  214. $newnode->setAttribute("lng", $row['lng']);
  215. $newnode->setAttribute("distance", $row['distance']);
  216. }
  217. echo $dom->saveXML();
  218. ?>
  219.  
  220. <?php
  221. $username="root";
  222. $password="root";
  223. $database="ca2solution";
  224. $server="localhost";
  225.  
  226. $connection = mysqli_connect($username, $password, $database, $server);
  227. ?>
Add Comment
Please, Sign In to add comment