yudaduy

Nearby Places Example

Sep 22nd, 2021
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Nearby Places</title>
  5. <style>
  6. /* Always set the map height explicitly to define the size of the div
  7. * element that contains the map. */
  8. #map {
  9. height: 100%;
  10. }
  11.  
  12. /* Optional: Makes the sample page fill the window. */
  13. html,
  14. body {
  15. height: 100%;
  16. margin: 0;
  17. padding: 0;
  18. }
  19.  
  20. #floating-panel {
  21. position: absolute;
  22. top: 10px;
  23. left: calc(50% - 185px);
  24. z-index: 5;
  25. background-color: #fff;
  26. padding: 5px;
  27. text-align: center;
  28. font-family: 'Roboto','sans-serif';
  29. line-height: 30px;
  30. padding-left: 10px;
  31. }
  32.  
  33. </style>
  34.  
  35. <!-- jsFiddle will insert css and js -->
  36. </head>
  37. <body>
  38. <div id="map"></div>
  39. <div id="floating-panel">
  40. <span>Find Store</span><br>
  41. <input id="address" type="textbox" placeholder="Search Center Point" style="width:300px;">
  42. </div>
  43.  
  44. <!-- Async script executes immediately and must be after any DOM elements used in callback. -->
  45. <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
  46. <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&v=weekly&channel=2&libraries=places" async></script>
  47.  
  48. <script>
  49.  
  50. function initMap(){
  51. var initLatlong = new google.maps.LatLng(-6.2297264,106.9894288);
  52.  
  53. map = new google.maps.Map(document.getElementById('map'), {
  54. center: initLatlong,
  55. zoom: 10,
  56. mapTypeControl: false,
  57. zoomControl: true,
  58. zoomControlOptions: {
  59. position: google.maps.ControlPosition.LEFT_BOTTOM
  60. },
  61. styles: [
  62. {
  63. featureType: "poi",
  64. elementType: "labels",
  65. stylers: [{ visibility: "off" }]
  66. }
  67. ]
  68. });
  69.  
  70. var infoWindow = new google.maps.InfoWindow();
  71.  
  72. var icon = {
  73. url: "https://storage.googleapis.com/terra_web/images/marker.png", // url
  74. Scalessize: new google.maps.Size(50, 50), // scaled size
  75. origin: new google.maps.Point(0,0), // origin
  76. anchor: new google.maps.Point(0, 0) // anchor
  77. };
  78. marker = new google.maps.Marker({
  79. icon:"https://storage.googleapis.com/terra_web/images/marker_50.png",
  80. map: map
  81. });
  82. marker.addListener('click', function() {
  83. infoWindow.open(map, marker);
  84. });
  85.  
  86. var autocomplete = new google.maps.places.Autocomplete(
  87. (document.getElementById('address'))
  88. );
  89.  
  90. autocomplete.addListener('place_changed', function() {
  91. infoWindow.close();
  92. var place = autocomplete.getPlace();
  93. if (!place.geometry) {
  94. return;
  95. }
  96.  
  97. if (place.geometry.viewport) {
  98. map.fitBounds(place.geometry.viewport);
  99. map.setZoom(16);
  100. var servicePlace = new google.maps.places.PlacesService(map);
  101. servicePlace.nearbySearch({
  102. location: place.geometry.location,
  103. radius: 5000,
  104. type: ['restaurant']
  105. }, callback);
  106.  
  107. function callback(results, status) {
  108. if (status === google.maps.places.PlacesServiceStatus.OK) {
  109. for (var i = 0; i < results.length; i++) {
  110. createMarker(results[i]);
  111. }
  112. }
  113. }
  114.  
  115. function createMarker(restaurant) {
  116. var placeLoc = restaurant.geometry.location;
  117. var marker = new google.maps.Marker({
  118. map: map,
  119. position: restaurant.geometry.location
  120. });
  121.  
  122. google.maps.event.addListener(marker, 'click', function() {
  123. infoWindow.setContent(restaurant.name);
  124. infoWindow.open(map, this);
  125. });
  126.  
  127. }
  128. } else {
  129.  
  130. map.setCenter(place.geometry.location);
  131. map.setZoom(17);
  132. }
  133.  
  134. // Set the position of the marker using the place ID and location.
  135. marker.setPlace({
  136. placeId: place.place_id,
  137. location: place.geometry.location
  138. });
  139. marker.setVisible(true);
  140.  
  141. infoWindow.setContent("latlng : "+place.geometry.location);
  142. infoWindow.open(map, marker);
  143.  
  144. /* circle test
  145. ----------------------------------------------- */
  146. var _radius = 2000;
  147. var rMin = _radius * 4/5;
  148. var rMax = _radius;
  149. var direction = 1;
  150.  
  151. var circleOption = {
  152. center: place.geometry.location,
  153. fillColor: '#3878c7',
  154. fillOpacity: 0.2,
  155. map: map,
  156. radius: 2000,
  157. strokeColor: '#3878c7',
  158. strokeOpacity: 1,
  159. strokeWeight: 0.5
  160. };
  161.  
  162. var circle = new google.maps.Circle(circleOption);
  163. });
  164. }
  165.  
  166. </script>
  167. </body>
  168. </html>
Advertisement
Add Comment
Please, Sign In to add comment