1
我使用gmap进行地理定位,即在特定位置上的谷歌地图上设置标记。 现在我想实现的是设置标记,只要用户点击其中一个标记,信息窗口就会打开并显示特定的文本。每个标记都有自己的文本。javascript - 将文字添加到gmap标记
现在的问题是我无法确定用户点击了哪个标记,因此无法设置正确的文本。
这里有一个代码片段:
//**Global variables**/
var thingsOfInterest = new Array(new Array(), new Array(),new Array());
var map = null;
//End Global variables
//init google map
function initGoogleMaps(){
map = new google.maps.Map(document.getElementById("map_canvas"));
var centerMap = new google.maps.LatLng(48.337881,14.320323);
$('#map_canvas').gmap({'zoom':7,'center': centerMap});
$('#map_canvas').gmap('option', 'zoom', 10);
//This is not working on my ios-simulator, no idea why. Anyway....
forge.geolocation.getCurrentPosition(function(position) {
alert("Your current position is: "+position);
}, function(error) {
});
}
/*End init map*/
/*Set the markers on the map*/
function setMarkers() {
/* thingsOf Interest contains:
* thingsOfInterest[i][0] -> the text that the marker should hold
* thingsOfInterest[i][1] -> the latitude
* thingsOfInterest[i][2] -> the longitude
*/
for (var i = 0; i < thingsOfInterest.length; i++) { //Iterate through all things
var item = thingsOfInterest[i]; //get thing out of array
var itemLatLng = new google.maps.LatLng(item[1], item[2]);
$('#map_canvas').gmap('addMarker', {'position': new google.maps.LatLng(item[1],item[2]) }).click(function(e) {
$('#map_canvas').gmap('openInfoWindow', {'content': 'dummyContent'}, this); ///////SET REAL CONTENT HERE
});
}
}
现在这个工程的所有伟大的,但我想是获得标记的用户点击的功能() - 事件处理程序。如果我能得到具体的标记,我可以设置它的文字。
我希望这已经够清楚了。
任何帮助非常感谢。
感谢,
enne
感谢Michal Klouda的帮助。那么,第一种方法不适用于imho,因为每个infowindow中总会有项[0]的文本。第二种方法看起来不错,如果用项目[i]替换项目[0]。不幸的是,它并没有在我的iOS iPhone模拟器中添加一个标记,我不知道为什么。 – enne87
@ enne87你试过吗?使用相同的逻辑,所有的标记将处于相同的位置。秒解决方案是我如何在我的项目中做同样的事情,并知道它的工作。而物品[i]的东西没有多大意义,因为item已经从迭代数组中取出。 –
对不起!我昨天有点厌倦了所有的编码,所以我觉得不够。你是绝对正确的,当然,项目[i]将是完全废话。但它仍然需要东西的最后一个条目的文字,这意味着当我尝试你的第一个和第二个方法时,item [0]总是相同的... – enne87