gpt4 book ai didi

php - 使用信息窗口设置标记聚类器标记

转载 作者:行者123 更新时间:2023-11-28 10:27:22 25 4
gpt4 key购买 nike

我在为 google map api 中的多个标记设置信息窗口时遇到问题。基本上,我从外部 Json 文件获取纬度和经度以及其他所有内容。我正在正确解析它,因为标记显示在 map 上。但是,当我尝试为每个标记添加一个信息窗口时,它仅显示最近添加的标记的信息窗口+每当我指向不同的标记时,它仍然会转到显示其信息窗口的最新标记。这是源代码:

  function initialize() 
{
var latlng = new google.maps.LatLng(-34.397, 150.644);

var myOptions = {
zoom: 1,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("tab14"), myOptions);

/* Sets up markers */
markers = [];

for (var i = 0, coordinates; coordinates = data.coords[i]; i++)
{
//marker = new google.maps.LatLng(coordinates.latitude, coordinates.longitude);

var iconImage = new google.maps.MarkerImage("http://map.lgfl.org.uk/images/arrow_green.png");
var LatLng = new google.maps.LatLng(coordinates.latitude, coordinates.longitude);
var marker = new google.maps.Marker({position: LatLng, icon: iconImage });

var contentString = '<div>'+
'<div>'+
'</div>'+
'<h4>'+coordinates.title+'</h1>'+
'<div>'+
'<p>'+coordinates.excerpt+'</p>'+
'</div>'+
'</div>';

var infowindow = new google.maps.InfoWindow({
content: contentString
});

google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});


markers.push(marker);
}

/* Sets up marker clusterer */
var markerCluster = new MarkerClusterer(map, markers);
}

有人可以给我一些帮助吗?谢谢。

//已编辑-------------------------------------------------------- ----------

所以我改变了它,现在它看起来像这样:

infoWindow = new google.maps.InfoWindow();

for (var i = 0, coordinates; coordinates = data.coords[i]; i++)
{
//marker = new google.maps.LatLng(coordinates.latitude, coordinates.longitude);

var iconImage = new google.maps.MarkerImage("http://map.lgfl.org.uk/images/arrow_green.png");
var LatLng = new google.maps.LatLng(coordinates.latitude, coordinates.longitude);
var marker = new google.maps.Marker({position: LatLng, icon: iconImage });

var contentString = '<div>'+
'<div>'+
'</div>'+
'<h4>'+coordinates.title+'</h1>'+
'<div>'+
'<p>'+coordinates.excerpt+'</p>'+
'</div>'+
'</div>';

google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(contentString);
infoWindow.open(map,marker);
});

markers.push(marker);
}

/* Sets up marker clusterer */
var markerCluster = new MarkerClusterer(map, markers);

还是不行:/

最佳答案

据我所知,你只能有一个InfoWindow。您应该向每个定义窗口内容的标记添加一个 onclick 事件。

我认为你应该改变:

var infowindow = new google.maps.InfoWindow({
content: contentString
});

google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});

类似:

google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});

infoWindow 应该在 for 循环之外定义。

祝你好运!

** 编辑 **

我认为您需要为标记使用唯一的名称。

这是一个工作示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="http://maps.google.com/maps/api/js?v=3&amp;sensor=false&amp;key=
<<INSERT YOUR KEY HERE>>" type="text/javascript"></script>
<script>
function initialize() {
// Create google map
var latlng = new google.maps.LatLng(31.0293534,5.1736923);
var myOptions = {
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 1,
backgroundColor: "#99B3CC",
mapTypeControl: false
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
var infoWindow = new google.maps.InfoWindow();

/**
* First marker
*/

var latLng = new google.maps.LatLng(31.0293534,5.1736923);
var marker1 = new google.maps.Marker({
map: map,
position: latLng,
visible: true
});


google.maps.event.addListener(marker1, 'click', function() {
infoWindow.setContent("Some content Marker 1");
infoWindow.open(map,marker1);
});

/**
* Second marker
*/

var latLng = new google.maps.LatLng(25.271139,55.307485);
var marker2 = new google.maps.Marker({
map: map,
position: latLng,
visible: true
});
marker2.onclick = function() {
infoWindow.setContent("Some content marker2");
infoWindow.open(map,marker2);
};
google.maps.event.addListener(marker2, 'click', function() {
infoWindow.setContent("Some content marker2");
infoWindow.open(map,marker2);
});
}
window.onload=initialize;
</script>

</head>
<body>
<div id="map" style="width: 400px; height: 400px;"></div>
</body>
</html>

您可能需要更改 key 。但在我这里,这就像一个魅力。我可以给你一个最简单的例子。

关于php - 使用信息窗口设置标记聚类器标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3985754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com