【问题标题】:infoWindow on google maps v3 doesn't work谷歌地图 v3 上的 infoWindow 不起作用
【发布时间】:2015-03-20 08:25:31
【问题描述】:

对不起我的英语不好:-S 我有一个小问题。 我从外部 json 文件加载 json 数据。 这里是文件的内容:

{
"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 代码中出错了,但我没有找到错误。 有人有想法吗? 非常感谢。 :-)

【问题讨论】:

  • 发布的代码包含 javascript 错误。您能否通过 jslint 运行它或将其放入显示问题的小提琴中。
  • @geocodezip:我在代码中看不到错误(至少没有会停止执行的语法错误)

标签: javascript json google-maps-api-3 google-maps-markers infowindow


【解决方案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-callback),当您将标记变量设为本地时,它不会被覆盖:

        $.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);
            });

        });

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 2012-09-21
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2013-07-04
    • 2011-08-23
    • 2011-02-22
    • 1970-01-01
    相关资源
    最近更新 更多