Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <title>Places Search Demo SRC</title>
- <style>
- html, body {
- padding: 0;
- }
- #map {
- height: 100%;
- }
- .controls {
- }
- #pac-input {
- }
- #pac-input:focus {
- border-color: #4d90fe;
- }
- .pac-container {
- font-family: Ariel;
- }
- #type-selector {
- color: #000;
- background-color: #4d90fe;
- padding: 5px 11px 0px 11px;
- }
- #type-selector label {
- font-family: Ariel;
- font-size: 13px;
- font-weight: 300;
- }
- #target {
- width: 345px;
- }
- </style>
- </head>
- <body>
- <input id="pac-input" class="controls" type="text" placeholder="Search Box">
- <div id="map"></div>
- <script>
- function init() {
- var map = new google.maps.Map(document.getElementById('map'), {
- center: {lat: 33.8688, lng: -117.2195},
- zoom: 15,
- mapTypeId: 'hybrid'
- });
- var input = document.getElementById('pac-input');
- var searchBox = new google.maps.places.SearchBox(input);
- map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
- map.addListener('bounds_changed', function() {
- searchBox.setBounds(map.getBounds());
- });
- var markers = [];
- // Listen for the event fired when the user selects a prediction and retrieve
- // more details for that place.
- searchBox.addListener('places_changed', function() {
- var places = searchBox.getPlaces();
- if (places.length == 0) {
- //return;
- }
- // Clear out the old markers.
- markers.forEach(function(marker) {
- marker.setMap(markers);
- });
- markers = [];
- var bounds = new google.maps.LatLngBounds();
- places.forEach(function(place) {
- if (!place.geometry) {
- console.log("Returned place contains no geometry");
- return;
- }
- var icon = {
- url: place.icon,
- size: new google.maps.Size(71, 71),
- origin: new google.maps.Point(0, 0),
- anchor: new google.maps.Point(17, 34),
- scaledSize: new google.maps.Size(25, 25)
- };
- markers.push(new google.maps.Marker({
- map: map,
- icon: icon,
- title: place.name,
- position: place.geometry.location
- }));
- if (place.geometry.viewport) {
- bounds.union(place.geometry.viewport);
- } else {
- bounds.extend(place.geometry.location);
- }
- });
- map.fitBounds(bounds);
- alert(place.geometry.location);
- });
- }
- </script>
- <script src="https://maps.googleapis.com/maps/api/js?key=&libraries=places&callback=init"
- async defer></script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment