谷歌地图API-带有位置详细信息的信息窗口自动打开,不包含内容
Google Maps API - InfoWindow with Place Details auto-opens without content
我是新来的,我是谷歌地图API或任何API的新手!我已经设法拼凑出了一些东西,但请耐心等待
我已经加载了我的基本地图,在我正在处理的业务(地方)上放置了一个标记,并在信息窗口中填充从Places API获取的相关业务详细信息。单击标记时,将显示信息窗口,并显示内容。到目前为止,很好
现在,我希望信息窗口在加载地图时自动显示。我有这个工作,但出现的信息窗口是空的。。。只出现一个带有X的小气泡。如果我单击该标记,则会显示包含业务详细信息的信息窗口
我的代码是从我在这里和那里找到的片段中拼凑而成的,所以我知道它一团糟,其中的某些东西没有触发我想要的信息窗口。有人能认出我哪里错了吗?
提前感谢
C
作为参考,当我点击标记时,我会得到(我已经清空了详细信息)
然而,当我把它运行到自动打开时,我会得到这个
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 54.71891, lng: -8.716691},
zoom: 16,
disableDefaultUI: true,
keyboardShortcuts: false,
disableDoubleClickZoom: true,
draggable: false
});
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: 'ChIJBy4MUDc8X0gR7LGOUiAYQeQ'
}, 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.open(map, marker);
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
'<p>' + place.formatted_address + '</p>' +
'<p><a href="https://www.google.com/maps?cid=16447453841236865516"><strong>Get Directions / Larger Map</strong> </a></p>' +
'</div>');
});
google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
infowindow.open(map, marker);
});
}
});
}
</script>
在代码中,您可以通过单击标记来设置infowindow
的内容。所以如果你在addListener
之前设置内容,我认为这是可以的
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 54.71891, lng: -8.716691},
zoom: 16,
disableDefaultUI: true,
keyboardShortcuts: false,
disableDoubleClickZoom: true,
draggable: false
});
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: 'ChIJBy4MUDc8X0gR7LGOUiAYQeQ'
}, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
var infowindow = new google.maps.InfoWindow();
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
'<p>' + place.formatted_address + '</p>' +
'<p><a href="https://www.google.com/maps?cid=16447453841236865516"><strong>Get Directions / Larger Map</strong> </a></p>' +
'</div>');
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
infowindow.open(map, marker);
});
}
});
}
</script>
我希望这正是你所需要的。
相关文章:
- 无法在窗口内使用rootScope.通知
- 华丽的弹出窗口内弹出
- 建议将包含不同参数的内联JS转换为外部脚本
- 如何隐藏标记簇内标记的信息窗口
- Angular$scope在模式窗口内不起作用
- 剑道UI下拉列表在剑道窗口内不起作用
- 浏览器窗口中的应用内FB页面插件链接
- window.open没有't在设置setTimeout内工作-弹出窗口被阻止
- 在谷歌地图的信息窗口内点击按钮
- 使用多功能框在 chrome 扩展程序中包含内联自动填充功能的任何方法
- ng-class 在 angularJs 中由 ng-include 包含的文件内不起作用
- 无法在指令内的弹出窗口中触发联机功能
- 模式弹出窗口内的按钮点击不起作用
- 在引导模式窗口内路由
- 滚动窗口内的一个元素,该窗口具有滚动条
- 如果指令包含在另一个指令中,我如何在隔离范围内添加双向数据绑定属性
- Javascript,包含窗口边缘内的图像
- 尝试在主窗口中包含子窗口
- 启用 JQ 网格上的“保存”按钮,其中包含内联编辑和单元格编辑
- 如何在Haml中包含内联JavaScript