【问题标题】:Unable to show the latitude and longitude in the InfoWindow无法在 InfoWindow 中显示经纬度
【发布时间】:2012-09-04 07:54:11
【问题描述】:

我有一个 java 脚本函数,用于在地图的选定位置上显示标记,并在 InfoWindow 中显示标记位置的纬度和经度。

我可以在任何位置显示标记,但无法显示带有坐标的信息窗口。

这是函数:

function init()
{
 var mapoptions=
 {
    center: new google.maps.LatLng(17.379064211298, 78.478946685791),
    zoom: 8,
    mapTypeId: google.maps.MapTypeId.ROADMAP
 }
 map=new  google.maps.Map(document.getElementById("map_can"), mapoptions);
 var marker;
 google.maps.event.addListener(map,'click',function(event)
     {
        marker= new google.maps.Marker({position:event.latLng,map:map});
     });
 var iwindow= new google.maps.InfoWindow();
 google.maps.event.addListener(marker,'click',function(event)
    {
       iwindow.setContent(event.latLng.lat()+","+event.latLng.lng());
       iwindow.open(map,marker);
    });
}

我哪里错了?请提出建议。

【问题讨论】:

  • 您确定iwindow.open(map,marker); 中的marker 在监听器范围内可用吗?我建议您尝试在侦听器函数内创建一个隐藏的标记对象并使用它来设置 iwindow。编辑:当然,新的隐藏标记将使用event.latLng 正确设置。

标签: google-maps-api-3 latitude-longitude infowindow


【解决方案1】:

这是因为您将事件附加到一个空的标记对象(在您调用时它是未分配的

google.maps.event.addListener(marker,'click',function(event) { ... });

尝试在创建标记后将点击事件附加到标记,例如:

google.maps.event.addListener(map,'click',function(event)
 {
    marker= new google.maps.Marker({position:event.latLng,map:map});
    google.maps.event.addListener(marker,'click',function(event)
    {
        iwindow.setContent(event.latLng.lat()+","+event.latLng.lng());
        iwindow.open(map,marker);
    });
 });

【讨论】:

  • +1 这是正确的。我得出了同样的结论。 jsfiddle.net/yV6xv/582
  • 哦,是的,我把那里弄得一团糟。谢谢。
【解决方案2】:

你可以试试这段代码:

function addMarkerWithTimeout(position, timeout, id) {
                window.setTimeout(function () {
                    markers.push(new google.maps.Marker({
                        position: position,
                        map: map,
                        icon: image1,
                        title: "whatever!",
                        draggable: true,
                        animation: google.maps.Animation.ROUTE
                    }));

                    google.maps.event.addListener(map, 'click', function (event)
                    {
                        google.maps.event.addListener(markers[id], 'click', function (event)
                        {
                            infoWindow.setContent(event.latLng.lat() + "," + event.latLng.lng());
                            infoWindow.open(map, markers[id]);
                        });
                    });

                }, timeout);
            }

【讨论】:

    猜你喜欢
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多