关于街景标记的InfoWindows [英] InfoWindows on Markers in StreetView

查看:91
本文介绍了关于街景标记的InfoWindows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Google文档,当您在gmap上创建标记时,标记也会复制到地图的StreetView版本上。

According to the Google documentation, when you create a marker on a gmap the marker is also 'copied' onto the StreetView version of the map.

但是,onClick事件绑定不会被复制(或者至少看起来不是),所以在StreetView中我无法在标记上打开InfoWindow。

However, onClick event binding are not copied (or at least don't appear to be), so I can't open the InfoWindow on the marker when in StreetView.

理想情况下我是' d实际上可以为InfoWindow的StreetView版本定义不同的内容,但是现在我甚至无法打开相同的InfoWindow。

Ideally I'd actually be able to define different content for the StreetView version of the InfoWindow, but right now I can't even get the same InfoWindow to open.

我正在使用来自Google示例的代码在主地图标记上创建InfoWindow绑定,如下所示(包含在函数中):

I'm using code from the Google examples to create the InfoWindow binding on the main map markers as follows (wrapped in a function):

google.maps.event.clearListeners(marker,'click');
google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
//      infoWindow.open(map.getStreetView(), marker);
});

该注释行是我尝试为该标记的StreetView版本添加开启者,但它没有什么都不做。

That commented line was my attempt to add the opener for the StreetView version of the marker, but it doesn't do anything.

注意: map 是地图对象, marker 是标记对象, html 包含要放入InfoWindow的HTML字符串。所有这些都适用于主地图标记,所以它是传递空变量或任何东西的问题。问题是只有让StreetView标记在点击时弹出它的InfoWindow。

Note: map is the map object, marker is the marker object and html contains the HTML string to be put into the InfoWindow. All of this works for the main map marker so it's mot a problem of null variables being passed or anything. The problem is only with getting the StreetView marker to pop it's InfoWindow when clicked.

推荐答案

我的第一个错误是我试图在地图副本和标记的StreetView副本上显示相同的InfoWindow。

My first error was that I was trying to display the same InfoWindow on both the map copy and the StreetView copy of the marker.

这是不允许的,因此创建InfoWindow的第二个实例可以解决该问题。

This is not allowed, and so creating a second instance of the InfoWindow fixed that problem.

为了创建两个单独的InfoWindows并将它们附加到同一个标记的两个副本,我必须稍微修改一下我的代码。

In order to create two separate InfoWindows and attach them to the two copies of the same marker I had to modify my code somewhat.

上面的代码来自一个名为 bindInfoWindow()的函数,该函数基于Google文档中的代码。我将其修改为包含一个参数来指定 map streetView 对象。

The code above was from a function called bindInfoWindow() based on the code in the Google documentation. I modified this to include a parameter to specify either the map or the streetView object.

下一个问题是每次 bindInfoWindow()时都会调用 clearListeners 方法被调用,有效地删除了 onClick 绑定标记的地图副本。

The next problem was that the clearListeners method was being called every time bindInfoWindow() was being called, effectively removing the onClick binding for the map copy of the marker.

因此我移动了 clearListeners 调用函数外部并在调用binInfoWindow()之前调用它。

I therefore moved the clearListeners call to outside of the function and call it before calling binInfoWindow().

最终函数如下所示:

function bindInfoWindow(marker, mapOrStreetViewObject, infoWindowObject, html) {
    google.maps.event.addListener(marker, 'click', function() {
        infoWindowObject.setContent(html);
        infoWindowObject.open(mapOrStreetViewObject, marker);
    });
}

然后按顺序调用:

// Note that the mapObject and streetViewObject variables are defined elsewheer to point to the map nd streetView instances in use.

//Define the local variables that we'll use in the calls
var myMapInfoWindow = new google.maps.InfoWindow;
var mapInfoWindowHTML = 'some stuff';
var myStreetViewInfoWindow = new google.maps.InfoWindow;
var streetViewInfoWindowHTML = 'some stuff';

// Remove any existing listeners from the marker
google.maps.event.clearListeners(marker,'click');

// Bind the event for the map marker click
bindInfoWindow(markerObject, mapObject, myMapInfoWindow, mapInfoWindowHTML);
//Bind the event for the StreetView marker click
bindInfoWindow(markerObject, streetViewObjectObject, myStreetViewInfoWindow, streetViewInfoWindowHTML);

这有什么好处,如果你在地图上打开一个InfoWindow,那么打开StreetView也一样InfoWindow已经在街景上开放了!

What is nice about this is that if you open an InfoWindow on the map, then open StreetView the same InfoWindow is already open on the Street View!

这篇关于关于街景标记的InfoWindows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆