gpt4 book ai didi

javascript - 可点击的侧边栏 markerclusterer google map api v.3

转载 作者:行者123 更新时间:2023-11-29 14:48:18 25 4
gpt4 key购买 nike

使用 Google map API v.3。附上markerclusterer.js

我想制作一张显示标记簇的 map 。 (完成!)

当我点击标记时,会显示一个信息窗口。 (完成!)

在 map 旁边制作了一个侧边栏。单击它时,将显示相关标记的信息窗口。 (失败,为什么?)

请帮我看一下javascript和html的代码。

var hk_markers = [
{
'id':1,
'name':'Sanrio Puroland(サンリオピューロランド)',
'location':'Tokyo, Japan',
'lat':35.62464,
'lng':139.42916,
},
{
'id':2,
'name':'Harmonyland(ハーモニーランド)',
'location':'Oita, Japan',
'lat':33.40162,
'lng':131.54657,
},
{
'id':3,
'name':'Sanrio Hello Kitty Town',
'location':'Johor, Malaysia',
'lat':1.41385,
'lng':103.66031,
},
{
'id':4,
'name':'China Hello Kitty Park(浙江安吉凯蒂猫家园)',
'location':'Zhejiang, China',
'lat':30.62377,
'lng':119.73663,
},
{
'id':5,
'name':'Hello Kitty Go Green Organic Farm (Hello Kitty有機薈低碳農莊)',
'location':'near Kam Sheung Road MTR Station, West Rail Line, Yuen lng, the New Territories, Hong Kong',
'lat':22.43402,
'lng':114.06037,
},
{
'id':6,
'name':'Jeju Hello Kitty Island - Museum & Cafe (헬로키티아일랜드)',
'location':'340, Hanchang-ro, Andeok-myeon, Seogwipo, Jeju-do, South Korea',
'lat':33.29021,
'lng':126.35205,
},
{
'id':7,
'name':'Dubai Hello Kitty Beauty Spa (مرحبا كيتي الجمال سبا)',
'location':'Jumeirah town centre, Jumeirah Beach Road, Dubai, the United Arab Emirates',
'lat':25.21491,
'lng':55.25100,
},];

var map;

var myCentreLat = 22.3700556;
var myCentreLng = 114.1535941;

var initialZoom = 2;
var markers = [];
var markerCluster;

var side_bar_html = "";



function showAll(){

var infowindow = new google.maps.InfoWindow();



for (var i = 0; i <hk_markers.length; i++) {

var myPos = new google.maps.LatLng(hk_markers[i].lat,hk_markers[i].lng);
var marker = new google.maps.Marker({
position: myPos,
map: map
});

var info = hk_markers[i].name;

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(hk_markers[i].name);
infowindow.open(map, marker);
}
})(marker, i));

markers.push(marker);

side_bar_html += '<a href="javascript: myclick('+ (markers.length-1) +')">' + '<p style="font-size:10px">' + hk_markers[i].name + '<\/p>' + '<\/a>';

}
markerCluster = new MarkerClusterer(map, markers);


}

function myclick(i) {
google.maps.event.trigger(markers[i], "click");
}




function initialize() {

var myDest = new google.maps.LatLng(myCentreLat,myCentreLng);

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

map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});

showAll();
document.getElementById("side_bar").innerHTML = side_bar_html;
markers=[];

}

这是 html 文件。

<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3.8&sensor=false"></script>

<script type="text/javascript" src="hkmarkersetup2.js"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" ></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>



</head>

<body onload="initialize()">
<h2>Testing</h2>



<table border=1>
<td>
<div class="gmap" id="map_canvas" style="height: 450px; width: 900px"></div>
</td>
<td width = 450>
<div class ="wrap" id="side_bar" style="overflow:auto; height:450px"></div>
</td>
</tr>
</table>

谢谢!

最佳答案

您在设置所有侧边栏条目后清除标记数组以使用它:

showAll();
document.getElementById("side_bar").innerHTML = side_bar_html;
markers=[];

不要那样做。

working fiddle

代码片段:

var hk_markers = [{
'id': 1,
'name': 'Sanrio Puroland(サンリオピューロランド)',
'location': 'Tokyo, Japan',
'lat': 35.62464,
'lng': 139.42916,
}, {
'id': 2,
'name': 'Harmonyland(ハーモニーランド)',
'location': 'Oita, Japan',
'lat': 33.40162,
'lng': 131.54657,
}, {
'id': 3,
'name': 'Sanrio Hello Kitty Town',
'location': 'Johor, Malaysia',
'lat': 1.41385,
'lng': 103.66031,
}, {
'id': 4,
'name': 'China Hello Kitty Park(浙江安吉凯蒂猫家园)',
'location': 'Zhejiang, China',
'lat': 30.62377,
'lng': 119.73663,
}, {
'id': 5,
'name': 'Hello Kitty Go Green Organic Farm (Hello Kitty有機薈低碳農莊)',
'location': 'near Kam Sheung Road MTR Station, West Rail Line, Yuen lng, the New Territories, Hong Kong',
'lat': 22.43402,
'lng': 114.06037,
}, {
'id': 6,
'name': 'Jeju Hello Kitty Island - Museum & Cafe (헬로키티아일랜드)',
'location': '340, Hanchang-ro, Andeok-myeon, Seogwipo, Jeju-do, South Korea',
'lat': 33.29021,
'lng': 126.35205,
}, {
'id': 7,
'name': 'Dubai Hello Kitty Beauty Spa (مرحبا كيتي الجمال سبا)',
'location': 'Jumeirah town centre, Jumeirah Beach Road, Dubai, the United Arab Emirates',
'lat': 25.21491,
'lng': 55.25100,
}, ];

var map;

var myCentreLat = 22.3700556;
var myCentreLng = 114.1535941;

var initialZoom = 2;
var markers = [];
var markerCluster;

var side_bar_html = "";



function showAll() {

var infowindow = new google.maps.InfoWindow();



for (var i = 0; i < hk_markers.length; i++) {

var myPos = new google.maps.LatLng(hk_markers[i].lat, hk_markers[i].lng);
var marker = new google.maps.Marker({
position: myPos,
map: map
});

var info = hk_markers[i].name;

google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(hk_markers[i].name);
infowindow.open(map, marker);
}
})(marker, i));

markers.push(marker);

side_bar_html += '<a href="javascript: myclick(' + (markers.length - 1) + ')">' + '<p style="font-size:10px">' + hk_markers[i].name + '<\/p>' + '<\/a>';

}
markerCluster = new MarkerClusterer(map, markers);


}

function myclick(i) {
// zoom to marker so added to map
map.setCenter(markers[i].getPosition());
map.setZoom(19);
google.maps.event.trigger(markers[i], "click");
}




function initialize() {

var myDest = new google.maps.LatLng(myCentreLat, myCentreLng);

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

map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function () {
infowindow.close();
});

showAll();
document.getElementById("side_bar").innerHTML = side_bar_html;
// markers = [];
}
google.maps.event.addDomListener(window,'load',initialize);
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<script src="https://unpkg.com/@googlemaps/markerclustererplus/dist/index.min.js"></script>
<h2>Testing</h2>

<table border="1">
<tr>
<td>
<div class="gmap" id="map_canvas" style="height: 450px; width: 450px"></div>
</td>
<td width="450">
<div class="wrap" id="side_bar" style="overflow:auto; height:450px"></div>
</td>
</tr>
</table>

关于javascript - 可点击的侧边栏 markerclusterer google map api v.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29775702/

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