页面加载时打开Goog​​le Maps API信息窗口

时间:2019-02-05 09:22:23

标签: google-maps google-maps-api-3

我使用Google Maps&Google Places API在地图上显示我的业务数据。实际上,当我单击地图标记时,信息窗口会打开,但我的目标是在页面加载时打开信息窗口。

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

service.getDetails(request, function(place, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
        var marker = new google.maps.Marker({
            map: map,
            position: place.geometry.location
        });

        google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
            /* place.formatted_address +  '<br />' + */
            place.address_components[1].long_name + ' ' + place.address_components[0].long_name + '<br />' +
            place.address_components[6].long_name + ' ' + place.address_components[2].long_name + '<br />' +

            place.rating + place.user_ratings_total + '</div>');

            infowindow.open(map, this);
        });
    }
});

1 个答案:

答案 0 :(得分:0)

您应在主区域中移动信息窗口和标记的代码

var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location
});

infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
            /* place.formatted_address +  '<br />' + */
            place.address_components[1].long_name + ' ' + place.address_components[0].long_name + '<br />' +
            place.address_components[6].long_name + ' ' + place.address_components[2].long_name + '<br />' +

            place.rating + place.user_ratings_total + '</div>');

infowindow.open(map, marker);