使用侧边栏点击触发google地图事件v3
仍然坚持这个问题,有人可以帮助我们取得一些进展,只是现在信息窗口出现问题。
<div id="map"></div>
<div id="sidebar">
<h2>Sidebar</h2>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.890542, 151.274856),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
//console.log(infowindow);
var markers = [];
var marker = [];
var html = "<ul>";
for (i = 0; i < locations.length; i++) {
//console.info(marker.position());
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: "http://lindsay-and-dan.gettingmarried.co.uk/images/maps/icon-music.gif"
});
//console.log(marker);
var center = marker.position;
markers.push(marker);
console.log(markers[i]);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
//console.log(marker.position);
map.panTo(center);
}
})(marker, i));
//console.log(marker);
//console.log(locations[i][0]);
html += '<li><a href="#" class="move" map="' + markers[i].Oa + ',' + markers[i].Pa + ',' + locations[i][0] + '">' + locations[i][0] + '</a></li>';
}
html += "</ul>";
$('#sidebar').html(html).fadeIn('slow');
$('.move').click(function(){
//infowindow.close();
var contents = $(this).attr('map');
var array = contents.split(',');
var lon = array[0];
var lat = array[1];
var content = array[2];
console.log(content);
infowindow.open(map, marker);
infowindow.setContent(content);
map.panTo(new google.maps.LatLng(lon,lat));
return false;
});
</script>
这是测试示例的链接。
http://extwit.users35.interdns.co.uk/gmap/
任何建议???
Still stuck with this one can someone please help made some progress just having issues with the info windows now.
<div id="map"></div>
<div id="sidebar">
<h2>Sidebar</h2>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.890542, 151.274856),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
//console.log(infowindow);
var markers = [];
var marker = [];
var html = "<ul>";
for (i = 0; i < locations.length; i++) {
//console.info(marker.position());
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: "http://lindsay-and-dan.gettingmarried.co.uk/images/maps/icon-music.gif"
});
//console.log(marker);
var center = marker.position;
markers.push(marker);
console.log(markers[i]);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
//console.log(marker.position);
map.panTo(center);
}
})(marker, i));
//console.log(marker);
//console.log(locations[i][0]);
html += '<li><a href="#" class="move" map="' + markers[i].Oa + ',' + markers[i].Pa + ',' + locations[i][0] + '">' + locations[i][0] + '</a></li>';
}
html += "</ul>";
$('#sidebar').html(html).fadeIn('slow');
$('.move').click(function(){
//infowindow.close();
var contents = $(this).attr('map');
var array = contents.split(',');
var lon = array[0];
var lat = array[1];
var content = array[2];
console.log(content);
infowindow.open(map, marker);
infowindow.setContent(content);
map.panTo(new google.maps.LatLng(lon,lat));
return false;
});
</script>
Here is a link to the test example.
http://extwit.users35.interdns.co.uk/gmap/
any suggestions???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以帮助你绝对帮助了我。您的标记应如下所示:
已添加 Oa 和 Pa。
希望这会有所帮助
P.S:我刚刚注意到您的信息窗口本身的其他问题。添加
infowindow.open(map,marker);
并且信息窗口的定位已关闭......对此不太确定。PPS:您的
$('.move')
代码块需要位于标记循环内;在此块中,将您的marker
引用替换为markers[i]
。这应该适合你!Ok, so helping you DEFINITELY helped me. Your marker should look something like this:
Oa and Pa were added.
Hope this helps
P.S: I just noticed your other issue with the info window itself. Add
infowindow.open(map,marker);
and also the positioning of the infowindow is off...not really sure about that one.P.P.S.: Your
$('.move')
block of code needs to be within the markers loop; within this block, replace yourmarker
reference withmarkers[i]
. This should do it for you!