Maps API v3 Get Place details from place id

http://stackoverflow.com/questions/32887878

by upsidown

HTML

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&amp;libraries=places"></script>
<div id="map-canvas"></div>
<div id="opening-hours"></div>

CSS

body {
    font-family: Arial;
    font-size: 10px;
}
#map-canvas {
    height: 400px;
}
#opening-hours {
    margin: 5px 0;
    padding: 5px;
    border: 1px solid grey;
}
#opening-hours div {
    padding: 5px 0;
}

JavaScript

function initialize() {
    var map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: new google.maps.LatLng(0, 0),
        zoom: 15
    });

    var service = new google.maps.places.PlacesService(map);

    service.getDetails({
        placeId: 'ChIJod7tSseifDUR9hXHLFNGMIs'
    }, function (place, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {

            // Create marker
            var marker = new google.maps.Marker({
                map: map,
                position: place.geometry.location
            });

            // Center map on place location
            map.setCenter(place.geometry.location);

            // Get DIV element to display opening hours
            var opening_hours_div = document.getElementById("opening-hours");

            // Loop through opening hours weekday text
            for (var i = 0; i < place.opening_hours.weekday_text.length; i++) {

                // Create DIV element and append to opening_hours_div
                var content = document.createElement('div');
                content.innerHTML = place.opening_hours.weekday_text[i];
                opening_hours_div.appendChild(content);
            }
        }
    });
}

initialize();