Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. <!DOCTYPE html >
  2. <head>
  3. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  5. <title>Using MySQL and PHP with 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.  
  21. <body>
  22. <div id="map"></div>
  23.  
  24. <script>
  25. var customLabel = {
  26. restaurant: {
  27. label: 'R'
  28. },
  29. bar: {
  30. label: 'B'
  31. }
  32. };
  33.  
  34. function initMap() {
  35.  
  36. var origin = {lat: -33.871, lng: 151.197};
  37.  
  38.  
  39. var map = new google.maps.Map(document.getElementById('map'), {
  40. center: origin,
  41. zoom: 12
  42. });
  43.  
  44. var clickHandler = new ClickEventHandler(map, origin);
  45.  
  46. var infoWindow = new google.maps.InfoWindow;
  47.  
  48. // Change this depending on the name of your PHP or XML file
  49. downloadUrl('https://www.tivahost.com/test/maps/NLPOI.php', function(data) {
  50. var xml = data.responseXML;
  51. var markers = xml.documentElement.getElementsByTagName('marker');
  52. Array.prototype.forEach.call(markers, function(markerElem) {
  53. var name = markerElem.getAttribute('name');
  54. var address = markerElem.getAttribute('address');
  55. var type = markerElem.getAttribute('type');
  56. var point = new google.maps.LatLng(
  57. parseFloat(markerElem.getAttribute('lat')),
  58. parseFloat(markerElem.getAttribute('lng')));
  59.  
  60. var infowincontent = document.createElement('div');
  61. var strong = document.createElement('strong');
  62. strong.textContent = name
  63. infowincontent.appendChild(strong);
  64. infowincontent.appendChild(document.createElement('br'));
  65.  
  66. var text = document.createElement('text');
  67. text.textContent = address
  68. infowincontent.appendChild(text);
  69.  
  70.  
  71. var icon = customLabel[type] || {};
  72.  
  73.  
  74. var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
  75. var icons = {
  76. restaurant: {
  77. icon: iconBase + 'parking_lot_maps.png'
  78. },
  79. bar: {
  80. icon: iconBase + 'library_maps.png'
  81. },
  82. info: {
  83. icon: iconBase + 'info-i_maps.png'
  84. }
  85. };
  86.  
  87. var marker = new google.maps.Marker({
  88. map: map,
  89. position: point,
  90. icon: icons[type].icon,
  91.  
  92. label: icon.label
  93. });
  94. marker.addListener('click', function() {
  95. infoWindow.setContent(infowincontent);
  96. infoWindow.open(map, marker);
  97.  
  98. });
  99.  
  100. });
  101. });
  102. }
  103.  
  104.  
  105.  
  106. /**
  107. * @constructor
  108. */
  109. var ClickEventHandler = function(map, origin) {
  110. this.origin = origin;
  111. this.map = map;
  112. this.directionsService = new google.maps.DirectionsService;
  113. this.directionsDisplay = new google.maps.DirectionsRenderer;
  114. this.directionsDisplay.setMap(map);
  115. // this.placesService = new google.maps.places.PlacesService(map);
  116. this.infowindow = new google.maps.InfoWindow;
  117. this.infowindowContent = document.getElementById('infowindow-content');
  118. this.infowindow.setContent(this.infowindowContent);
  119.  
  120. // Listen for clicks on the map.
  121. this.map.addListener('click', this.handleClick.bind(this));
  122. };
  123.  
  124. ClickEventHandler.prototype.handleClick = function(event) {
  125. console.log('You clicked on: ' + event.latLng);
  126. // If the event has a placeId, use it.
  127. if (event.placeId) {
  128. console.log('You clicked on place:' + event.placeId);
  129.  
  130. // Calling e.stop() on the event prevents the default info window from
  131. // showing.
  132. // If you call stop here when there is no placeId you will prevent some
  133. // other map click event handlers from receiving the event.
  134. event.stop();
  135. this.calculateAndDisplayRoutePlace(event.placeId);
  136. // this.getPlaceInformation(event.placeId);
  137. }
  138. else {
  139. event.stop();
  140. this.calculateAndDisplayRoute(event.latLng);
  141. }
  142. };
  143.  
  144. ClickEventHandler.prototype.calculateAndDisplayRoutePlace = function(placeId) {
  145. var me = this;
  146. this.directionsService.route({
  147. origin: this.origin,
  148. destination: {placeId: placeId},
  149. travelMode: 'WALKING'
  150. }, function(response, status) {
  151. if (status === 'OK') {
  152. me.directionsDisplay.setDirections(response);
  153. } else {
  154. window.alert('Directions request failed due to ' + status);
  155. }
  156. });
  157. };
  158.  
  159. ClickEventHandler.prototype.calculateAndDisplayRoute = function(latLng) {
  160. var me = this;
  161. this.directionsService.route({
  162. origin: this.origin,
  163. destination: latLng,
  164. travelMode: 'WALKING'
  165. }, function(response, status) {
  166. if (status === 'OK') {
  167. me.directionsDisplay.setDirections(response);
  168. } else {
  169. window.alert('Directions request failed due to ' + status);
  170. }
  171. });
  172. };
  173.  
  174. ClickEventHandler.prototype.getPlaceInformation = function(placeId) {
  175. var me = this;
  176. this.placesService.getDetails({placeId: placeId}, function(place, status) {
  177. if (status === 'OK') {
  178. me.infowindow.close();
  179. me.infowindow.setPosition(place.geometry.location);
  180. me.infowindowContent.children['place-icon'].src = place.icon;
  181. me.infowindowContent.children['place-name'].textContent = place.name;
  182. me.infowindowContent.children['place-id'].textContent = place.place_id;
  183. me.infowindowContent.children['place-address'].textContent =
  184. place.formatted_address;
  185. me.infowindow.open(me.map);
  186. }
  187. });
  188. };
  189.  
  190.  
  191.  
  192. function downloadUrl(url, callback) {
  193. var request = window.ActiveXObject ?
  194. new ActiveXObject('Microsoft.XMLHTTP') :
  195. new XMLHttpRequest;
  196.  
  197. request.onreadystatechange = function() {
  198. if (request.readyState == 4) {
  199. request.onreadystatechange = doNothing;
  200. callback(request, request.status);
  201. }
  202. };
  203.  
  204. request.open('GET', url, true);
  205. request.send(null);
  206. }
  207.  
  208. function doNothing() {}
  209. </script>
  210. <script async defer
  211. src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAeuBDdFj7e9OS-gIXIKD51gVd3Ko4hod4&callback=initMap">
  212. </script>
  213. </body>
  214. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement