简体   繁体   English

检查坐标矩形是否包含CLLocationCoordinate2D

[英]Check if coordinate rectangle contains CLLocationCoordinate2D

I am using a special Map SDK for iOS and I am adding a custom shape to the map. 我正在使用iOS专用的Map SDK,并且正在向地图添加自定义形状。 The shape is always a different size and it could be a circle, square, star etc. the point being it is always dynamic whenever the app is run. 形状始终是不同的大小,可以是圆形,正方形,星形等。要点是,只要运行应用程序,它就始终是动态的。

After adding this shape to the map, I can access it's property called overlayBounds which is described as: This property contains the smallest rectangle that completely encompasses the overlay. 在将此形状添加到地图后,我可以访问它的名为overlayBounds的属性, This property contains the smallest rectangle that completely encompasses the overlay.描述为: This property contains the smallest rectangle that completely encompasses the overlay.

The overlay is my shape that I'm adding to the map. overlay是我要添加到地图的形状。

Whenever a location update is generated by CLLocationManager , I want to check and see if the most recent coordinate is inside of that overlayBounds property of the shape. 每当CLLocationManager生成位置更新时,我都要检查并查看最新坐标是否在该形状的overlayBounds属性内。

When accessing overlayBounds , it has an ne property and a sw property. 访问overlayBounds ,它具有ne属性和sw属性。 Both of these are just CLLocationCoordinate2D's 这两个都是CLLocationCoordinate2D's

So, if the overlayBounds is made up of two CLLocationCoordinate2D's and the CLLocationManager is always updating the user's location and giving me the most recent coordinate( CLLocationCoordinate2D ), how can I check if that most recent coordinate is within the overlayBounds ? 因此,如果overlayBounds由两个CLLocationCoordinate2D's组成,并且CLLocationManager始终在更新用户的位置并为我提供最新的坐标( CLLocationCoordinate2D ),如何检查最近的坐标是否在overlayBounds

After doing a lot of research I have only found one potential solution to go off of which is this: https://stackoverflow.com/a/30434618/3344977 经过大量研究后,我仅发现一个可行的解决方案: https : //stackoverflow.com/a/30434618/3344977

But that answer assumes that my overlayBounds property has 4 coordinates( CLLocationCoordinate2D's ), when I only have 2. 但是这个答案假设我的overlayBounds属性只有4个坐标( CLLocationCoordinate2D's ),而我只有2个。

Your description seems much harder then the actual question. 您的描述似乎比实际问题难得多。 So if I am getting this correctly your question is only to check if the point is inside the rectangle described in overlayBounds . 因此,如果我正确地获得了此信息,那么您的问题只是检查该点是否位于overlayBounds描述的矩形内。

You have only 2 points as it is enough to define a rectangle. 您只有2个点,因为它足以定义一个矩形。 So NE and SW are the two points where the other two are received as (NE.x, SE.y) and (SE.x, NE.y) . 因此NESW是接收其他两个点的两个点,分别是(NE.x, SE.y)(SE.x, NE.y) With this you may use the answer you linked or you may simply construct a MKMapRect where origin is NE and size is SE-NE . 这样,您可以使用链接的答案,也可以简单地构造MKMapRect ,其来源为NE ,大小为SE-NE So in this case you may simply use MKMapRectMake and then use MKMapRectContainsPoint . 因此,在这种情况下,您可以简单地使用MKMapRectMake然后使用MKMapRectContainsPoint BUT watch out when computing size as SE-NE might produce negative results in which cases you need to add degrees to the size. 但是,当按SE-NE计算尺寸时,请SE-NE在这种情况下,您需要为尺寸增加度数。 That is 180 to x (latitude) and 360 to y (longitude)... 那是180到x (纬度)和360到y (经度)...

MKMapRect rect = MKMapRectMake(NE.latitude, NE.longitude, SE.latitude-NE.latitude, SE.longitude-NE.longitude);
if(rect.width < .0) rect.width += 180.0;
if(rect.height < .0) rect.height += 360.0;
BOOL pointInside = MKMapRectContainsPoint(rect, pointOnMap);

Something like this should do the trick. 这样的事情应该可以解决问题。

Now if you are trying to check if the point is inside the shape itself it really depends on how your shape is defined. 现在,如果您要检查点是否在形状本身内部,则实际上取决于形状的定义方式。 If this is some form of analytic representation you might find some method already made for you to return the value but if not then your best shot would most likely be drawing the shape to some canvas and checking the color of canvas at the location you need to check. 如果这是某种形式的解析表示形式,则可能会发现已经为您返回值的某种方法,但是如果没有,那么最好的拍摄方法很可能是将形状绘制到某些画布上并在您需要的位置检查画布的颜色校验。 In any case the bigger problem here is converting the point and the rect to a Cartesian coordinate system. 无论如何,这里最大的问题是将点和矩形转换为笛卡尔坐标系。 If that is the case then just add a comment and I will try to help you on that... 如果是这样,那么只需添加一条评论,我会尽力帮助您...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM