Jelajahi Sumber

Add autocomplete field to Map Overview page

Alan Lam 8 tahun lalu
induk
melakukan
b3de5f0921
3 mengubah file dengan 33 tambahan dan 1 penghapusan
  1. 21 0
      public/js/app.js
  2. 1 1
      views/footer.php
  3. 11 0
      views/map_overview.php

+ 21 - 0
public/js/app.js

@@ -128,4 +128,25 @@ function initialiseMaps() {
 
 	});
 
+	$("input[data-autocomplete]").each(function() {
+		let mapId = $(this).data("autocomplete");
+		let map = maps[mapId];
+
+		let autocomplete = new google.maps.places.Autocomplete(this);
+		autocomplete.bindTo("bounds", map);
+		autocomplete.setFields(["address_components", "geometry", "icon", "name"]);
+
+		autocomplete.addListener("place_changed", function() {
+			let place = autocomplete.getPlace();
+			if(place.geometry.viewport) {
+				map.fitBounds(place.geometry.viewport);
+			}
+			else {
+				map.setCenter(place.geometry.location);
+				map.setZoom(17);
+			}
+		});
+
+	});
+
 }

+ 1 - 1
views/footer.php

@@ -70,7 +70,7 @@
 		}
 		?>
 
-		<script src="https://maps.googleapis.com/maps/api/js?key=<?=GOOGLE_MAPS_API_KEY?>&callback=initialiseMaps" async defer></script>
+		<script src="https://maps.googleapis.com/maps/api/js?key=<?=GOOGLE_MAPS_API_KEY?>&amp;libraries=places&amp;callback=initialiseMaps" async defer></script>
 		<!--<script src="https://www.google.com/recaptcha/api.js" async defer></script>-->
 	</body>
 </html>

+ 11 - 0
views/map_overview.php

@@ -4,6 +4,17 @@
 
 	<h2>Map Overview</h2>
 
+	<div class="input-group">
+		<div class="input-group-prepend">
+			<span class="input-group-text">Location</span>
+		</div>
+		<input type="text" class="form-control" placeholder="Type location" data-autocomplete="map" />
+	</div>
+
+	<div class="help-text text-muted mb-3">
+		<small>Type location and select to jump to location.</small>
+	</div>
+
 	<div class="mb-4" id="map" data-maps data-lat="51.3555177" data-lng="-1.1743197" data-zoom="7" style="height: 512px;"></div>
 
 	<div class="row">