【发布时间】:2010-10-22 09:29:24
【问题描述】:
您好,我不知道为什么会出现此错误。
我有一个输入列表,其中的值包含 lat/long/name/address。
在地图上打印标记工作正常,使用 infowin 工作正常,直到我意识到它只在所有窗口中打开相同的信息。 Soo,显然我需要将this(我刚刚单击的标记)附加到 eventListner。
但是,执行此操作时,我收到一条错误消息view.createMarkerHTML is not a function。
问题:如何在正确的标记上附加要打开的正确信息?
view = {
map: {
init: function init (){
view.map.initMap();
},
initMap: function initMap() {
if(navigator.geolocation) {
function hasPosition(position) {
var point = new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
myOptions = {
zoom: 12,
center: point,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
mapDiv = document.getElementById("map"),
map = new google.maps.Map(mapDiv, myOptions);
view.map.handleMarkers(map);
}
navigator.geolocation.getCurrentPosition(hasPosition);
}
},
handleMarkers: function handleMarkers(map) {
var list = $('#pois input'),
l = list.length,
i = 0, lat = '', long = '', marker = {}, theName = '', address = '', info = {};
for (i = 0; i < l; i += 1) {
info = $(list[i]).val().split('|');
lat = parseFloat(info[0], 10);
long = parseFloat(info[1], 10);
theName = info[2];
address = info[3];
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, long),
map: map,
icon: icon
});
google.maps.event.addListener(marker, 'click', function() {
currentMarker = this;view.map.createMarkerHTML(map, theName, address);
});
}
} ,
createMarkerHTML: function createMarkerHTML(map, theName, address) {
var contentString =
'<div id="gMapInfoWin">' +
'<h1>' + theName + '</h1>' +
'<ul>' +
'<li>' + address + '</li>' +
'</ul>' +
'</div>'
;
currentMarker.infowindow = new google.maps.InfoWindow({
content: contentString
});
currentMarker.infowindow.open(map, currentMarker);
}
}
};
【问题讨论】:
-
我在任何地方都看不到
view的定义,如果没有这个,这个问题非常很难回答。不过,如果想玩,请尝试view.map.createMarkerHTML(map, theName, address); -
尼克,你应该把它作为答案发布,因为这就是答案。
-
当然是 view.map.function() 我完全忘记了地图对象。很容易对自己的代码视而不见,嘿。非常感谢。
-
但是,主要问题仍然存在,我将更改我的代码以便更容易理解,谢谢
标签: javascript jquery google-maps-api-3