vin*_*smo 15 javascript maps google-maps google-maps-api-3 ios
您如何确定给定点是否在边界框内?
我的观点是48.847172,2.386597.
BoundingBox的:
"48.7998602295",
"48.8198640442",
"2.46138595581",
"2.48138619423"
Run Code Online (Sandbox Code Playgroud)
Mar*_*ssi 32
像往常一样:
if( bb.ix <= p.x && p.x <= bb.ax && bb.iy <= p.y && p.y <= bb.ay ) {
// Point is in bounding box
}
Run Code Online (Sandbox Code Playgroud)
bb是边界框,(ix,iy)是它的左上角坐标,还是(ax,ay)它的右下角坐标. p是点和(x,y)它的坐标.
kum*_*tix 12
此解决方案还考虑了UI发送一个跨越经度180/-180的框的情况(在低缩放级别上映射视图,您可以看到整个世界,允许无限循环水平滚动,因此有可能例如一个方框的bottomLeft.lng = 170而topRight.lng = -170(= 190),其中包括20度的范围.
def inBoundingBox(bl/*bottom left*/: Coordinates, tr/*top right*/: Coordinates, p: Coordinates): Boolean = {
// in case longitude 180 is inside the box
val isLongInRange =
if (tr.long < bl.long) {
p.long >= bl.long || p.long <= tr.long
} else
p.long >= bl.long && p.long <= tr.long
p.lat >= bl.lat && p.lat <= tr.lat && isLongInRange
}
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您使用的是传单,则可以创建一个新的LatLngBounds并使用其contains()操作:
var bounds = new L.LatLngBounds(
new L.LatLng(gc.bbox['_northEast'].lat, gc.bbox['_northEast'].lng),
new L.LatLng(gc.bbox['_southWest'].lat, gc.bbox['_southWest'].lng));
return bounds.contains(new L.LatLng(pos.latitude, pos.longitude))
Run Code Online (Sandbox Code Playgroud)
这个答案与上面 kumetix 的答案相同;然而,对于我的一生来说,我并不明白那里发生了什么,因为它被浓缩了很多。这是带有详细解释的相同答案。另请注意,答案是 Kotlin 语言,而不是原始帖子要求的 JavaScript 语言,但它具有足够的可读性,因此转换为您的语言应该不会太糟糕。
首先定义 Coordinate2D 和 BoundingBox 类:
data class Coordinate2D (val latitude: Double, val longitude: Double)
data class BoundingBox (val north: Double, val east: Double, val south: Double, val west: Double)
Run Code Online (Sandbox Code Playgroud)
这是确定一个点是否在任意边界框中的函数
fun isPointInBoundingBox(point: Coordinate2D, boundingBox: BoundingBox): Boolean {
//given the bounding box is an imaginary rectangle in a coordinate system
//bounding box has 4 sides - northLine, eastLine, southLine and westLine
//initially assume the point is not in our bounding box of interest as such:
var isPointEastOfWestLine = false
var isPointWestOfEastLine = false
var isPointSouthOfNorthLine = false
var isPointNorthOfSouthLine = false
if (boundingBox.east < boundingBox.west) {
//if west coordinate has a bigger value than the east,
//the bounding box must be crossing the dateline
//in other words, longitude 180 is inside the box
//let's see what's happening with the point
if (point.longitude >= boundingBox.west) {
//imagine a bounding box where westernmost longitude is +170 and easternmost longitude is -170
//if the point in question has a latitude of +171 as in the case expressed in the if
//statement, then we can conclude that point lies east of the west line
isPointEastOfWestLine = true
//we can also infer that the point must lie west of east line because the point's longitude is positive
//therefore, the point's position must be to the west of the easternmost longitude of the bounding box
isPointWestOfEastLine = true
}
if (point.longitude <= boundingBox.east) {
//imagine a bounding box where westernmost longitude is +170 and easternmost longitude is -170
//if the point in question has a latitude of -171 as in the case expressed in the if
//statement, then we can conclude that point lies west of the east line
isPointWestOfEastLine = true
//we can also infer that the point must lie east of the west line because the point's longitude is negative
//therefore, the point's position must be to the east of the westernmost longitude of the bounding box
isPointEastOfWestLine = true
}
} else {
//in the else case, bounding box does not cross the dateline, so comparisons are more straightforward
//longitudes range from -180 to +180; therefore, western side of a bounding box will always
//have lower value than eastern side
if (point.longitude >= boundingBox.west) {
//in this case, point's longitude has a higher value than the west side of the bounding box
//we can conclude that point lies to the east of the west line of the bounding box
isPointEastOfWestLine = true
}
if (point.longitude <= boundingBox.east) {
//in this case, point's longitude has a lower value than the east side of the bounding box
//we can conclude that point lies to the east of the west line of the bounding box
isPointWestOfEastLine = true
}
}
//comparing latitudes are little bit easier. latitude values range from -90 to +90 where
//-90 is the southern pole and +90 is the northern pole. The more north you go, higher the values.
if (point.latitude >= boundingBox.south) {
//point's latitude is higher, therefore, point must lie to the north of the south line
isPointNorthOfSouthLine = true
}
if (point.latitude <= boundingBox.north) {
//point's latitude is higher, therefore, point must lie to the north of the south line
isPointSouthOfNorthLine = true
}
return isPointEastOfWestLine &&
isPointWestOfEastLine &&
isPointNorthOfSouthLine &&
isPointSouthOfNorthLine
}
Run Code Online (Sandbox Code Playgroud)
这应该更快。
function doesPointCollide(p,box) {
return !(p.x < box.left || p.x > box.right || p.y > box.bottom || p.y < box.top)
}
Run Code Online (Sandbox Code Playgroud)
如果点在任何维度之外,我们知道它不在边界框中,否则它在边界框中,因此我们可以更快地忽略否定情况。
| 归档时间: |
|
| 查看次数: |
22858 次 |
| 最近记录: |