gpt4 book ai didi

google-maps - 谷歌地图检查当前边界是否在数据层的边界内

转载 作者:行者123 更新时间:2023-12-01 12:31:02 27 4
gpt4 key购买 nike

我正在谷歌地图中加载某个国家/地区的数据层(这是一个国家/地区的绘图):

map.data.addGeoJson(geoJsonObject);

我很确定没有,但是...有没有办法检查 map 的边界是否在数据层的边界内

(基本上,我想知道,当用户在 map 上导航时,当前视口(viewport)是否在数据层内);

var bounds = this.map.getBounds();
var sw = bounds.getSouthWest();

也许我可以在西南方向的位置查询数据层并检查一些 Prop 。表明我在那个数据层里面?

或者至少:

有谁知道如何以编程方式获取某个特征对象,知道纬度和经度?

这里的谷歌地图使用事件来获取要素对象:

 map.data.addListener('click', function(event) {
event.feature.setProperty('isColorful', true);
});

但我不想使用事件。 有没有一种方法可以提供一个点的坐标并获取特征对象?像这样的东西:

map.getFeature(lat, long).setProperty('isColorful', true);

最佳答案

google.maps.LatLngBounds.contains 函数可用于此目的,但由于它接受单个位置,因此建议采用以下解决方案:

1) 从 GeoJSON 坐标初始化数据层边界:

var dataLayer = map.data;
var layerBounds = new google.maps.LatLngBounds();
//1.collect all coordinates from data layer
dataLayer.forEach(function(f) {
var geometry = f.getGeometry();
processCoordinates(geometry, layerBounds.extend, layerBounds);
});

2) 判断 map 边界是否在图层边界内:

if (layerBounds.contains(map.getBounds().getNorthEast()) && layerBounds.contains(map.getBounds().getSouthWest())) {
//...
}

工作示例

In the provided example green colored area will be displayed if map bounds are within a layer bounds, and the red colored in the opposite case:

var area;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 53.349248,
lng: -6.255323
},
zoom: 6,
mapTypeId: google.maps.MapTypeId.TERRAIN
});

displayDataLayer(map);

document.getElementById("btnShow").onclick = function() {
var result = displayDataLayerBoundsArea(map);
};
}


function displayDataLayer(map) {
var dataLayer = map.data;
dataLayer.loadGeoJson('https://gist.githubusercontent.com/vgrem/440708612b574764c309/raw/2a4e2feadc204806440c51a14c2ef1f54f4fc3d8/Census2011_Province_generalised20m.json');
dataLayer.setMap(map);
}

function displayDataLayerBoundsArea(map) {
var dataLayer = map.data;
var layerBounds = new google.maps.LatLngBounds();
//1.collect all coordinates from data layer
dataLayer.forEach(function(f) {
var geometry = f.getGeometry();
processCoordinates(geometry, layerBounds.extend, layerBounds);
});

if (area != null) {
area.setMap(null);
}

//2.determine whether map bounds are contained within a layer bounds
if (layerBounds.contains(map.getBounds().getNorthEast()) && layerBounds.contains(map.getBounds().getSouthWest())) {
//map.fitBounds(bounds);
area = new google.maps.Rectangle({
strokeColor: '#00FF00',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#00FF00',
fillOpacity: 0.35,
map: map,
bounds: {
north: layerBounds.getNorthEast().lat(),
south: layerBounds.getSouthWest().lat(),
east: layerBounds.getNorthEast().lng(),
west: layerBounds.getSouthWest().lng()
}
});
} else {
//map.fitBounds(bounds);
area = new google.maps.Rectangle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
bounds: {
north: layerBounds.getNorthEast().lat(),
south: layerBounds.getSouthWest().lat(),
east: layerBounds.getNorthEast().lng(),
west: layerBounds.getSouthWest().lng()
}
});
}
}


function processCoordinates(geometry, callback, thisArg) {
if (geometry instanceof google.maps.LatLng) {
callback.call(thisArg, geometry);
} else if (geometry instanceof google.maps.Data.Point) {
callback.call(thisArg, geometry.get());
} else {
geometry.getArray().forEach(function(g) {
processCoordinates(g, callback, thisArg);
});
}
}
#map {
width: 800px;
height: 640px;
}
<button id="btnShow">Show</button>
<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

JSFiddle

关于google-maps - 谷歌地图检查当前边界是否在数据层的边界内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34154527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com