显示信息窗口后阻止Google Maps移动

Burak Erdem尚未活跃在Medium上。

我正在尝试在Google地图上显示信息窗口。它显示完美,当您将鼠标悬停在标记上时,它会加载一个信息窗口,但地图会跳转以适合窗口。我不希望地图移动,而是根据窗口设置信息窗口的位置。Booking.com有这样的东西。

编辑:添加了我的代码

这是我的代码的精简版。我从AJAX服务获取所有信息,并且该服务返回response(也包含更多信息)。

$.ajax({
    url: 'URL',
    dataType: "json",
    type: "GET",
    success: function(response) {
        // delete all markers
        clearOverlays();

        var infowindow = new google.maps.InfoWindow();

        for (var i = 0; i < response.length; i++) {
            item = response[i];

            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(item.lat, item.lng),
                map: map,
                url: item.detail_url
            });

            markersArray.push(marker);

            // display infowindow
            google.maps.event.addListener(marker, "mouseover", (function(marker, item) {
                return function() {
                    infowindow.setOptions({
                        content: 'SOME CONTENT HERE FOR INFOWINDOW'
                    });

                    infowindow.open(map, marker);
                }
            })(marker, item));

            // remove infowindow
            google.maps.event.addListener(marker, 'mouseout', function() {
                infowindow.close();
            });

            // marker click
            google.maps.event.addListener(marker, 'click', function() {
                window.location.href = marker.url;
            });
        }
    }
});

如下图所示,第一个图像显示在标记底部的信息窗口,第二个图像显示在标记顶部的信息窗口。地图不会移动,但信息窗口会在地图边界内设置其位置。

标记的信息窗口底部 标记顶部的信息窗口

苏维·维加纳拉(Suvi vignarajah)

在用于设置信息窗口选项的声明中infowindow.setOptions,添加以下选项以在显示信息窗口时禁用地图的平移disableAutoPan : true

但是,这也意味着您需要计算可用于在地图上显示信息窗口并使用该position选项为其指定位置的不动产否则,不能保证您的信息窗口将在地图上完全可见。

https://developers.google.com/maps/documentation/javascript/reference#InfoWindowOptions

EDIT: How to calculate screen real estate

I realize I was pretty vague with my suggestion to calculate the screen real estate available to set the position of your infowindow, when part of your question was to actually set the infowindow's position. So I've provided an update to my answer on how you can calculate the screen real estate and adjust your infowindow's position.

The key is to first convert your points from LatLng to pixels, and find out the pixel coordinate of the marker on the map with relation to the pixel coordinate of the map's center. The following snippet demonstrates how this can be done.

getPixelFromLatLng: function (latLng) {
    var projection = this.map.getProjection();
    //refer to the google.maps.Projection object in the Maps API reference
    var point = projection.fromLatLngToPoint(latLng);
    return point;
}

Once you've got your pixel coordinates, you'll need to find out which quadrant of the map canvas the marker is currently residing in. This is achieved by comparing the X and Y coordinates of the marker to the map, like so:

quadrant += (point.y > center.y) ? "b" : "t";
quadrant += (point.x < center.x) ? "l" : "r";

Here, I'm determining if the point is in the bottom right, bottom left, top right or top left quadrant of the map based on it's relative position to the map's center. Keep in mind this is why we use pixels as opposed to LatLng values, because LatLng values will always yield the same result - but reality is the marker can be in any quadrant of the map canvas depending on panning.

Once you know which quadrant the marker is in, you can give offset values to the infowindow to position it so it's visible on the map - like so:

if (quadrant == "tr") {
    offset = new google.maps.Size(-70, 185);
} else if (quadrant == "tl") {
    offset = new google.maps.Size(70, 185);
} else if (quadrant == "br") {
    offset = new google.maps.Size(-70, 20);
} else if (quadrant == "bl") {
    offset = new google.maps.Size(70, 20);
}
//these values are subject to change based on map canvas size, infowindow size

Once the offset value is determined you can just adjust the pixelOffset of your infowindow on whatever listener you have invoking the infoWindow.open() method. This is done by using the setOptions() method for infowindows:

infowindow.setOptions({pixelOffset : self.getInfowindowOffset(self.map, marker)}); 

Here is a working JSFiddle example of the solution described above.

Note: You will notice the annoying "arrow" on the infowindow displaying desbite the position of your infowindow, this is part of the default Google Map's setting for infowindows and I could not find a proper way of getting rid of it. There was a suggestion here, but I couldn't get it to work. Alternatively you can use the infobox library or the infobubble library - which gives you more styling options.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Maps信息窗口显示/隐藏操作

来自分类Dev

Android Google Maps移动相机

来自分类Dev

鼠标悬停时未显示Google Maps信息窗口

来自分类Dev

如何在iOS右侧显示Google Maps信息窗口

来自分类Dev

通过ClusterManager Google Maps Android显示的隐藏信息窗口

来自分类Dev

在Google Maps标记的信息窗口中显示链接

来自分类Dev

在Google Maps API中移动圆

来自分类Dev

Google Maps CameraUpdate移动到错误的坐标

来自分类Dev

在Google Maps API中移动圆

来自分类Dev

删除/移动Google Maps标记(jquery-ui-maps)

来自分类Dev

Google Maps API信息窗口

来自分类Dev

异步Google Maps信息窗口

来自分类Dev

在Google Maps的“信息”窗口内单击按钮

来自分类Dev

Angular Google Maps信息窗口HTML

来自分类Dev

在Google Maps的信息窗口中添加图像?

来自分类Dev

Google Maps中的信息窗口无法加载

来自分类Dev

信息窗口的Google Maps外部链接

来自分类Dev

多个Google Maps折线和信息窗口

来自分类Dev

angularjs-google-maps 信息窗口位置

来自分类Dev

Google Maps API-悬停时的信息窗口显示相同的文本

来自分类Dev

显示/隐藏标记并使用信息窗口Google Maps API v3

来自分类Dev

angular-google-maps在鼠标悬停事件上未显示信息窗口

来自分类Dev

Google Maps自定义叠加层位置移动

来自分类Dev

Google Maps iframe禁用移动设备上的滚动触摸

来自分类Dev

使相机随着位置的变化而移动(Google Maps Api)

来自分类Dev

与标记Google Maps javascript一起移动圆

来自分类Dev

Android Google Maps在运行时移动地图

来自分类Dev

无法在iOS版Google Maps的GMSMapView上移动相机

来自分类Dev

Google Maps iframe在移动设备上没有圆形边框

Related 相关文章

热门标签

归档