如何检查坐标是否包含在当前视图的地图?



我在地图上显示了一个路由(坐标数组)(react-native-maps)。我想检查路由的坐标是否在视图中以触发一些后续操作(或不)。

const route = [
{
latitude: 41.38145,
longitude: 2.17182,
},
{
latitude: 41.38154,
longitude: 2.17203,
},
{
latitude: 41.38155,
longitude: 2.17205,
},
];
const MapDisplay = () => {
const onRegionChange = (region) => {
//searched function
routeIsInViewport(region, route);
};
return <MapView onRegionChange={onRegionChange} />;
};

您可以在补全中找到该地区的最小和最大纬度/经度,然后将该值与路由数组进行比较,以确定它是否在。

const onRegionChange = (region) => {
let lat_min = region.latitude - (region.latitudeDelta / 2);
let lat_max = region.latitude + (region.latitudeDelta / 2);
let lng_min = region.longitude - (region.longitudeDelta / 2);
let lng_max = region.longitude + (region.longitudeDelta / 2);
routes.map((route)=>{
if((route.latitude>=lat_min && route.latitude<=lat_max) && (route.longitude>=lng_min && route.longitude<=lng_max)){
//Within region do your stuff
}
})
};

希望有帮助,干杯。

最新更新