我编辑javascript以显示googlemaps
我想只显示一个infowindow(如googlemapsV2)
我在infowindow.close
之前尝试了infowindow.open
。但infowindow.close
无效
我在这个网站上搜索相同的主题并尝试,但我无法解决
我发布了我的剧本。
请帮帮我。
var uptownMap,
defLng = -73.963245,
defLat = 40.779438,
san ={
init :
function(){
$(document).ready(function(){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=san.putGmap";
document.body.appendChild(script);
});
},
putGmap :
function(){
var myLatlng = new google.maps.LatLng(defLat, defLng);
var myOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
uptownMap = new google.maps.Map(document.getElementById("gmapArea"), myOptions);
san.getXmlData();
},
getXmlData : function(){
$.ajax({
type: 'GET',
cache: true,
async: true,
url: '/newyork/map_uptown_xml/',
datatype: 'xml',
success: san.parseXmlData
});
},
parseXmlData : function(xml){
var i = 0, id,name, url, lat, lng, copy, lead, ename,tag;
$("<ol/>").attr('id', 'gmapAnchor').appendTo('div#gmapController');
$(xml).find('item').each(function(i){
i++;
id = $(this).find('description id').text();
name = $(this).find('description name').text();
url = $(this).find('description path').text();
lat = $(this).find('description lat').text();
lng = $(this).find('description lng').text();
copy = $(this).find('description copy').text();
lead = $(this).find('description lead').text();
ename = $(this).find('description ename').text();
tag = $(this).find('description tag').text();
tag = tag.slice(5,20);
var customIcons =
{
hotel:
{
icon: 'http://www.tabikobo.com/newyork/img/icon_hotel.png'
},
shopping:
{
icon: 'http://www.tabikobo.com/newyork/img/icon_shop.png'
},
gourmet:
{
icon: 'http://www.tabikobo.com/newyork/img/icon_gourmet.png'
},
kanko:
{
icon: 'http://www.tabikobo.com/newyork/img/icon_spot.png'
},
};
var icon = customIcons[tag] || {};
var myLatLng = new google.maps.LatLng(lat, lng);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: uptownMap,
icon: icon.icon,
});
var htmlTmpl =
{
hotel:
{
content: '<div class="infoWinWrapper"><strong><a href="' + url + '">' + name + '</a></strong><br />' + ename + '<br />' + lead + '</div>'
},
shopping:
{
content: '<div class="infoWinWrapper"><strong><a href="' + url + '">' + name + '</a></strong><br />' + ename + '<br />' + lead + '</div>'
},
gourmet:
{
content: '<div class="infoWinWrapper"><strong><a href="' + url + '">' + name + '</a></strong><br />' + ename + '<br />' + lead + '</div>'
},
kanko:
{
content: '<div class="infoWinWrapper"><strong><a href="#' + id + '">' + name + '</a></strong><br />' + ename + '<br />' + copy + '</div>'
},
};
var htmlTmpl = htmlTmpl[tag] || {};
var offset = new google.maps.Size(0, 10);
var infowindow = new google.maps.InfoWindow({
content: '<div class="hook">'+htmlTmpl.content+'</div>',
pixelOffset: offset
});
google.maps.event.addListener(beachMarker, 'click', function() {
if(infowindow) infowindow.close();
infowindow.open(uptownMap, beachMarker);
});
google.maps.event.addListener(infowindow, 'domready', function() {
var l = $('.hook').parent().parent().parent().siblings().addClass("infoBox");
});
//Creat a Tag
san.putData(name, url, lat, lng, i);
});
},
putData : function(name, url, lat, lng, num){
var x = num;
x += '';
if(x.length == 1){
var y = '0' + x;
}else {
y = x;
}
san.setEvent();
},
setEvent : function(){
$("ul#area_list li a").bind('mouseover', function(){
$(this).parent().find('span.lat').text();
var point = new google.maps.LatLng(
$(this).parent().find('span.lat').text(),
$(this).parent().find('span.lng').text()
);
uptownMap.setZoom(17);
uptownMap.setCenter(point);
});
$("#btnResetZoom a").bind('click', function(){
var point = new google.maps.LatLng(defLat, defLng);
uptownMap.setZoom(15);
uptownMap.setCenter(point);
return false;
});
}
}
window.onload = san.init();
答案 0 :(得分:0)
您为每个标记创建单独的infoWindows(标记可能无法访问其他标记的信息窗口,infowindow
始终指向所单击标记的infoWindow。)
改为创建一个infoWindow。
parseXmlData: function (xml) {
//a single infowindow for all markers
var infowindow = new google.maps.InfoWindow({
pixelOffset: new google.maps.Size(0, 10)
});
var i = 0,
id, name, url, lat, lng, copy, lead, ename, tag;
$("<ol/>")
.attr('id', 'gmapAnchor')
.appendTo('div#gmapController');
$(xml)
.find('item')
.each(function (i) {
i++;
id = $(this)
.find('description id')
.text();
name = $(this)
.find('description name')
.text();
url = $(this)
.find('description path')
.text();
lat = $(this)
.find('description lat')
.text();
lng = $(this)
.find('description lng')
.text();
copy = $(this)
.find('description copy')
.text();
lead = $(this)
.find('description lead')
.text();
ename = $(this)
.find('description ename')
.text();
tag = $(this)
.find('description tag')
.text();
tag = tag.slice(5, 20);
console.log(tag)
var customIcons = {
hotel:
{
icon: 'http://www.tabikobo.com/newyork/img/icon_hotel.png'
},
shopping:
{
icon: 'http://www.tabikobo.com/newyork/img/icon_shop.png'
},
gourmet:
{
icon: 'http://www.tabikobo.com/newyork/img/icon_gourmet.png'
},
kanko:
{
icon: 'http://www.tabikobo.com/newyork/img/icon_spot.png'
},
};
var icon = customIcons[tag] || {};
var myLatLng = new google.maps.LatLng(lat, lng);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: uptownMap,
icon: icon.icon,
});
var htmlTmpl = {
hotel:
{
content: '<div class="infoWinWrapper"><strong><a href="' +
url + '">' + name +
'</a></strong><br />' + ename +
'<br />' + lead + '</div>'
},
shopping:
{
content: '<div class="infoWinWrapper"><strong><a href="' +
url + '">' + name +
'</a></strong><br />' + ename +
'<br />' + lead + '</div>'
},
gourmet:
{
content: '<div class="infoWinWrapper"><strong><a href="' +
url + '">' + name +
'</a></strong><br />' + ename +
'<br />' + lead + '</div>'
},
kanko:
{
content: '<div class="infoWinWrapper"><strong><a href="#' +
id + '">' + name +
'</a></strong><br />' + ename +
'<br />' + copy + '</div>'
},
};
var htmlTmpl = htmlTmpl[tag] || {};
//click-listener for marker
google.maps.event.addListener(beachMarker, 'click',
function () {
//update the content
infowindow.setContent(
'<div class="hook">' + htmlTmpl
.content + '</div>');
infowindow.open(uptownMap, this);
google.maps.event.addListenerOnce(
infowindow, 'domready',
function () {
var l = $('.hook')
.parent()
.parent()
.parent()
.siblings()
.addClass("infoBox");
});
});
//Creat a Tag
san.putData(name, url, lat, lng, i);
});
}