我对JavaScript和google maps API很新颖。我无法弄清楚什么是错的。Google maps v3 API标记在关闭InfoWindow时消失(InfoBubble)
InfoWindow实际上是我发现“InfoBubble”的类。 http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobubble/examples/example.html/
这里的情况是:
1. I create my map and add a 'click' event. The event creates Markers.
2. I create one global infoWindow.
3. I click the map, a Marker appears.
4. I click the map, a Marker appears.
5. I click the map, a Marker appears. Step 3-5 can be repeated lots and lots.
6. I click marker number X
6.1. An infoWindow pops up.
7. I click marker number Y
7.1. The infoWindow is closed. (.close())
7.2. Its content and position is changed
7.3. It is opened on the new position (.open(map,marker))
7.4. Also Marker number X is removed.
自己尝试一下:http://dl.dropbox.com/u/6084360/test/index.html
为什么步7.4。发生了什么?发生后,我可以点击标记,但我觉得没有任何东西消失。 为什么?
我曾尝试通过谷歌Chrome浏览器的调试有点,但之后我就一步7.3需要我到一些缩小的代码,我迷路。
这是删除标记的行。我不知道为什么它将其删除或如何知道从哪里开始。
R.addDomListenerOnce=function(a,b,c,d){var e=R[yc](a,b,function(){e[wb]();return c[Cc](this,arguments)},d);return e};R.R=function(a,b,c,d){c=cf(c,d);return R[yc](a,b,c)};function cf(a,b){return function(c){return b[oc](a,c,this)}}R.bind=function(a,b,c,d){return R[G](a,b,P(c,d))};R.addListenerOnce=function(a,b,c){var d=R[G](a,b,function(){d[wb]();return c[Cc](this,arguments)});return d};R.forward=function(a,b,c){return R[G](a,b,df(b,c))};R.ua=function(a,b,c,d){return R[yc](a,b,df(b,c,!d))};
我的代码:
var times = 0;
var treasureLocation = new google.maps.LatLng(62.05350309096103, 15.373047874999997);
var map, infoBubble = null;
function initialize()
{
// Create the actual map.
map = new google.maps.Map(document.getElementById("map_canvas"),
{
zoom: 4,
center: new google.maps.LatLng(62.05350309096103, 15.373047874999997),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
);
infoBubble = new InfoBubble({
disableAutoPan: true
});
// Add an eventlistener to the map.
google.maps.event.addListener
(
map,
'click',
function(ev)
{
addMarker(ev);
}
);
}
function addMarker(ev)
{
// Get the distance.
var distance = getDistance(ev.latLng , treasureLocation);
// Skriv ut vart man klickade.
document.getElementById("click_info").innerHTML = "Du klickade " + distance + " ifrån skatten, försök igen!";
// Create a marker
var marker = new google.maps.Marker({
position: ev.latLng,
map: map,
clickable: true,
title: "Härifån är det bara " + distance + " till skatten!",
icon: "shovel.png"
});
// Hook the click on the created marker to show the created popup
google.maps.event.addListener
(
marker,
'click',
function(ev)
{
if(infoBubble != null){infoBubble.close();}
infoBubble.setContent(marker.title);
infoBubble.setPosition(marker.position);
infoBubble.open(map, marker);
}
);
}
嗯,我不能真正解释为什么,但我发现,'infoBubble.setPosition(marker.position);'导致我的问题。如果你注释掉或删除,第一个标记不会消失(同样再次点击也不会改变泡泡内容,这也发生了)。那么,你应该使用'infoBubble.open(map,marker)来指定它应该打开的地方;'所以我想你不会需要它:)试试吧,让我知道它是否工作。 – Kedor 2012-03-07 22:38:04
谢谢!我认为该行将标记移到了第二次点击标记。我看到我的一个标记文本离开了它应该出现的地方,但我只是把它擦掉了,然后继续前进。 无论如何,谢谢!你应该把它作为一个“答案”,以便我可以放弃和接受,也许更多的人会看到它。 – 2012-03-07 22:51:26