如何确定注释是否在MKPolygonView(iOS)中 [英] How to determine if an annotation is inside of MKPolygonView (iOS)

查看:110
本文介绍了如何确定注释是否在MKPolygonView(iOS)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算特定注释(如用户位置的蓝色圆圈)或MKPinAnnotation是否在mapview上的MKPolygon图层内。

I'm trying to calculate if a specific annotation(like the blue circle of the user location) or a MKPinAnnotation is inside an MKPolygon layer on the mapview.

有什么建议吗?

推荐答案

以下内容将坐标转换为多边形视图中的CGPoint,并使用CGPathContainsPoint测试是否point位于路径中(可能是非矩形):

The following converts the coordinate to a CGPoint in the polygon view and uses CGPathContainsPoint to test if that point is in the path (which may be non-rectangular):

CLLocationCoordinate2D mapCoordinate = ...; //user location or annot coord

MKMapPoint mapPoint = MKMapPointForCoordinate(mapCoordinate);

MKPolygonView *polygonView = 
    (MKPolygonView *)[mapView viewForOverlay:polygonOverlay];

CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];

BOOL mapCoordinateIsInPolygon = 
    CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);

这应该适用于任何作为MKOverlayPathView子类的覆盖视图。实际上,你可以用MKOverlayPathView替换MKPolygonView。

This should work with any overlay view that is a subclass of MKOverlayPathView. You can actually replace MKPolygonView with MKOverlayPathView in the example.

这篇关于如何确定注释是否在MKPolygonView(iOS)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆