Created
July 25, 2018 19:28
-
-
Save marvinhosea/f98fd5aa56a0b726817e72d7fbf95bf3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=places" type="application/javascript"></script> | |
<script> | |
var map = new google.maps.Map(document.getElementById("map-convas"),{ | |
center:{ | |
lat:27.72, | |
lng: 85.36 | |
}, | |
zoom:15 | |
}); | |
var marker = addMark({lat:-1.2920659, lng:36.82194619999996}); | |
var input = document.getElementById('searchmap') | |
var searchbox = new google.maps.places.SearchBox(input); | |
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); | |
searchbox.addListener('places_changed', function() { | |
var places = searchbox.getPlaces(); | |
var bounds = new google.maps.LatLngBounds(); | |
var i,place; | |
for(i =0; place = places[i]; i++){ | |
console.log(place); | |
bounds.extend(place.geometry.location); | |
marker.setPosition(place.geometry.location); | |
} | |
console.log(bounds,places); | |
map.fitBounds(bounds); | |
map.setZoom(17); | |
setGeoLatLng(); | |
}); | |
marker.addListener('dragend', function () { | |
setGeoLatLng(); | |
}); | |
function addMark(latLng) { | |
var marker = new google.maps.Marker({ | |
position:latLng, | |
animation: google.maps.Animation.DROP, | |
map:map, | |
draggable:true | |
}); | |
map.panTo(marker.getPosition()); | |
infowindow = new google.maps.InfoWindow; | |
infowindow.setContent('Nairobi'); | |
infowindow.open(map, marker); | |
return marker; | |
} | |
function setGeoLatLng() { | |
map.panTo(marker.getPosition()); | |
var lat = marker.getPosition().lat(); | |
var lng = marker.getPosition().lng(); | |
var geocoder = new google.maps.Geocoder; | |
infowindow = new google.maps.InfoWindow; | |
geocodeLatLng(geocoder,map, infowindow, lat+','+lng); | |
} | |
function geocodeLatLng(geocoder, map, infowindow, values) { | |
var latlngStr = values.split(',', 2); | |
console.log(latlngStr); | |
var latlng = {lat: parseFloat(latlngStr[0]), lng: parseFloat(latlngStr[1])}; | |
geocoder.geocode({'location': latlng}, function(results, status) { | |
if (status === 'OK') { | |
if (results[0]) { | |
// this.marker.infowindow.close(); | |
this.marker.setPosition(new google.maps.LatLng(latlng.lat, latlng.lng)); | |
// var marker = addMark(latlng); | |
console.log(marker); | |
input.value = results[0].formatted_address; | |
infowindow.setContent(results[0].formatted_address); | |
infowindow.open(map, this.marker); | |
} else { | |
window.alert('No results found'); | |
} | |
} else { | |
window.alert('Geocoder failed due to: ' + status); | |
} | |
}); | |
} | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment