谷歌地图Api相当新的.我有一系列数据,我想循环并绘制在地图上.看起来相当简单,但我发现的所有多标记教程都非常复杂.
让我们使用谷歌网站上的数据数据作为例子:
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]
];
Run Code Online (Sandbox Code Playgroud)
我只是想绘制所有这些点,并在单击时弹出一个infoWindow以显示名称.
正如标题所述,在一个特定的事件上(对我而言,这恰好是在打开一个新的时候google.maps.InfoWindow我希望能够关闭任何其他当前打开的信息窗口.现在,我可以一次打开很多..但我只想要一次打开1次.
我正在动态创建信息窗口(即我不知道会生成多少个),所以在当前信息窗口的click事件中(我希望所有其他打开的窗口关闭)我不知道t引用任何其他要打开的信息窗口close().我想知道如何实现这一目标.我不是一个经验丰富的JavaScript程序员,所以我不知道我是否需要在这里使用反射或类似的东西.
最好的方法是将所有引用保存在某种集合中,然后遍历列表将它们全部关闭?
谢谢.
我有一个谷歌地图javascript 的工作部分,我确实有一个问题.
现在我遇到的问题是只有一个信息显示,最后一个信息显示.我找到了另一个堆栈线程的解决方案.但我无法弄清楚原因.我对Javascript很新,所以我希望有人可以向我解释改变了什么以及如何改变.
这是工作代码:
function setMarkers(map, locations){
for(var i = 0; i < locations.length; i++){
var marker = locations[i];
var latLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
var content = locations[i][0];
var infowindow = new google.maps.InfoWindow();
marker = new google.maps.Marker({
position:latLng,
map: map
});
google.maps.event.addListener(marker, 'click', function(content){
return function(){
infowindow.setContent(content);
infowindow.open(map, this);
}
}(content));
}
}
Run Code Online (Sandbox Code Playgroud)
这是原始的破解代码(我只会粘贴更改的代码):
google.maps.event.addListener(marker, 'click', function(){
infowindow.setContent(content);
infowindow.open(map, marker);
});
Run Code Online (Sandbox Code Playgroud)
现在,如果你这么善良,我想知道的是:
返回fn服务的功能是什么,以及
什么是最后添加(content),addListener (}(content));)因为据我所知,内容已经在setContent属性中设置.
谢谢!
如何在关闭另一个引脚或单击地图时关闭所有信息窗口?我正在使用http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/reference.html 和kml叠加层.继到我的JS到目前为止:
jQuery(document).ready(function ($) {
function initialize() {
google.maps.visualRefresh = true;
var myLatlng = new google.maps.LatLng(51.201465, -0.30244);
var mapOptions = {
zoom: 12,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var kmlLayer = new google.maps.KmlLayer({
url: 'http://***.com/new/wp-content/themes/required-starter/CGAGolfAcademies.kml?rand=' + (new Date()).valueOf(),
suppressInfoWindows: true,
map: map
});
google.maps.event.addListener(kmlLayer, 'click', function (kmlEvent) {
showInContentWindow(kmlEvent.latLng, kmlEvent.featureData.description);
});
function showInContentWindow(position, text) {
var content = "<div class='info_win'><p>" + text + "</p></div>";
var infowindow =new InfoBox({
content: content,
disableAutoPan: false,
maxWidth: …Run Code Online (Sandbox Code Playgroud) 我知道我的问题很多。但是我几乎看到了所有它们,并尝试应用其中一些。但是什么也没发生。下面是我的代码
var locations =
[{ "id": 1, "ReferenceNumber": "52525252525", "Address" : "School" , "Latitude": "21.585486", "Longitude": "50.258745" },
{ "id": 2, "ReferenceNumber": "6262626262", "Address" : "University", "Latitude": "21.54484411", "Longitude": "50.14846648" },
{ "id": 3, "ReferenceNumber": "424242424", "Address": "PUMPING STATION ", "Latitude": "21.9856341", "Longitude": "61.2587466" }];
Run Code Online (Sandbox Code Playgroud)
我添加的上述位置只有少数带有虚假条目的位置,否则我有150多个位置
$.each(locations, function(i, item) {
var marker = new google.maps.Marker({
'position': new google.maps.LatLng(item.Latitude, item.Longitude),
'map': map,
'title': item.Latitude + "," + item.Longitude
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/red-dot.png')
var infowindow = new google.maps.InfoWindow({
content: "<div class='infoDiv'><h3>Reference#: </h3> <h6>" + …Run Code Online (Sandbox Code Playgroud)