{
"Pizza Dach": {
"Kategorie": "Pizza",
"Position": {
"Longitude": 13.456106,
"Latitude": 52.51024
}
},
"Burgeramt": {
"Kategorie": "Burger",
"Position": {
"Longitude": 13.459539,
"Latitude": 52.510299
}
},
"DUDU": {
"Kategorie": "Pizza",
"Position": {
"Longitude": 13.415165,
"Latitude": 52.493024
}
}
}
这是我的js代码:
var karte;
var marker;
var meineLongitude, meineLatitude;
/* Karte */
var positionsAusgabe = function(position){
width = document.getElementById('karte').offsetWidth;
height = document.getElementById('karte').offsetHeight;
meineLongitude = position.coords.longitude;
meineLatitude = position.coords.latitude;
var optionen = {
zoom: 13,
center: new google.maps.LatLng(meineLatitude, meineLongitude),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
karte = new google.maps.Map(document.getElementById('karte'), optionen);
window.setTimeout(function(){
$.getJSON('daten/restaurants.json?='+Date.now(), function(data){
$.each(data, function(restaurant, daten){
marker = new google.maps.Marker({
map: karte,
icon: 'bilder/'+daten.Kategorie+'.png',
position: new google.maps.LatLng(daten.Position.Latitude,daten.Position.Longitude)
});
(function(marker, data) {
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent('+daten.Kategorie+');
infoWindow.open(map, marker);
});
});
});
// window.scrollTo(0,1);
});
},1);
};
嗯,问题是,标记正在设置,右图标和正确位置,但我不知道为什么infoWindow不会出现。我确定我在js代码中出错了,但我没有发现错误。 有人有想法吗? 非常感谢。 : - )
答案 0 :(得分:1)
这部分代码无效:
(function(marker, data) {
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent('+data.Kategorie+');
infoWindow.open(map, marker);
});
});
你定义了一个函数,但你没有执行它。
试试这个:
(function(marker, data) {
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(daten.Kategorie);
infoWindow.open(karte, marker);
});
}(marker,daten));
但是,不需要此功能。您已经在函数内部($.each
- 回调),当您创建标记变量local时,它不会被覆盖:
$.each(data, function(restaurant, daten){
var marker = new google.maps.Marker({
map: karte,
icon: 'bilder/'+daten.Kategorie+'.png',
position: new google.maps.LatLng(daten.Position.Latitude,daten.Position.Longitude)
});
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(daten.Kategorie);
infoWindow.open(karte, marker);
});
});