var map;
var infowindow;
var latLng;
var address = "400 West Anderson Ln, Austin, Texas";
function initMap() {
infowindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
address: address
}, function(results, status) {
if (status === 'OK') {
map = new google.maps.Map(document.getElementById('map'), {
center: results[0].geometry.location,
zoom: 15
});
latLng = results[0].geometry.location;
console.log(results[0].geometry.location.toUrlValue(6))
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: marker.getPosition(),
rankBy: google.maps.places.RankBy.DISTANCE,
types: ["establishment"]
}, function(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
console.log(results.length + " results returned");
for (var i = 0; i < results.length; i++) {
var marker = createMarker(results[i], i);
if (i == 0)
google.maps.event.trigger(marker, 'click');
else
marker.setMap(null);
}
} else {
alert('nearbySearch was not successful for the following reason: ' + status);
}
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
})
}
function createMarker(place, index) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name + "<br>" + address + "<br>" + index);
infowindow.open(map, this);
});
return marker;
}
#map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&callback=initMap" async defer></script>