Skip to content

Instantly share code, notes, and snippets.

@ivan-kalachikov
Created November 16, 2018 14:40
Show Gist options
  • Save ivan-kalachikov/fa57fcea06b9a36c0473a7c28c598e86 to your computer and use it in GitHub Desktop.
Save ivan-kalachikov/fa57fcea06b9a36c0473a7c28c598e86 to your computer and use it in GitHub Desktop.
Google map with info window from place id information
global.initMap = () => {
jQuery('.map-canvas').each((index, element) => {
let map = jQuery(element);
const position = new google.maps.LatLng(map.data('latitude'), map.data('longitude'));
const markerIcon = map.data('icon');
const mapZoom = map.data('zoom');
const dataPlaceId = map.data('place-id') || 'null';
let infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(element, {
zoom: mapZoom || 17,
center: position,
disableDefaultUI: true
});
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: dataPlaceId
}, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
map.setCenter(place.geometry.location);
infowindow.setContent('<div><strong>' +
place.formatted_address + '</div>');
infowindow.open(map, marker);
} else {
new google.maps.Marker({
position: position,
map: map,
icon: markerIcon || '',
});
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment