如何识别图钉在 bingMaps 的当前视图/缩放中是否可见?如果不可见 setView 到图钉的位置?

问题描述

我正在使用 Bing Maps V8,并且根据要求,我必须移动/平移 bing 地图以设置图钉的可见性。根据图钉位置,我们可以直接设置视图,如下所示,但只有当 bing 地图的当前视图没有肉眼可见的图钉时,我才会调整/移动 bing 地图。

this.map.setView({
     center: new Microsoft.Maps.Location(pin.lat,pin.long),});

您能否分享您对如何识别当前视图中是否可见该图钉的想法?

提前致以问候和感谢。

解决方法

获取地图的边界框,然后检查它是否包含您的图钉位置。如果它不更新地图视图。这是执行此操作的代码块。

var map = new Microsoft.Maps.Map(document.getElementById('myMap'),{});
var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(45,-110),null);
map.entities.push(pushpin);

//Get the bounding box of the current map view.
var bounds = map.getBounds();

//Check to see if the bounding box contains the pushpins location. 
//And if it doesn't,center the map.
if(!bounds.contains(pushpin.getLocation())) {
    map.setView({
        center: pushpin.getLocation()
    });
}