此时,我正试图弄清楚MKMapView上的坐标是否在提前绘制的具有纬度/经度的MKPolygon内。
我使用CGPathContainsPoint来确定坐标是否在地图上的多边形内,但无论我选择的坐标如何,它总是返回false。
谁能解释一下到底是哪里出了问题?下面是我在Swift中的代码。class ViewController: UIViewController, MKMapViewDelegate
@IBOutlet weak var mapView: MKMapView!
let initialLocation = CLLocation(latitude: 43.656734, longitude: -79.381576)
let point = CGPointMake(43.656734, -79.381576)
let regionRadius: CLLocationDistance = 500
let point1 = CLLocationCoordinate2D(latitude: 43.656734, longitude: -79.381576)
var points = [CLLocationCoordinate2DMake(43.655782, -79.382094),
CLLocationCoordinate2DMake(43.657499, -79.382310),
CLLocationCoordinate2DMake(43.656656, -79.380497),
CLLocationCoordinate2DMake(43.655782, -79.382094)]
override func viewDidLoad() {
super.viewDidLoad()
centerMapOnLocation(initialLocation)
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.addOverlay(polygon)
var annotation = MKPointAnnotation()
annotation.coordinate = point1
annotation.title = "Test"
annotation.subtitle = "Test"
mapView.addAnnotation(annotation)
self.mapView.delegate = self
}
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.strokeColor = UIColor.redColor()
if CGPathContainsPoint(polygonView.path, nil, CGPointMake(43.656734, -79.381576), true) {
print("True!!!!!")
} else {
println("False")
}
return polygonView
}
return nil
}
已更新为SWIFT 4
你混淆了CGPoints,它是x和y坐标对,指向视图中的位置,与cllocationcoordinate2d,这是地球上的坐标。
对于CGPathContainsPoint,你想传递你的polygonRenderer路径(从你的多边形在viewDidLoad()中创建)和当前位置,像这样:
let polygonRenderer = MKPolygonRenderer(polygon: polygon)
let mapPoint: MKMapPoint = MKMapPointForCoordinate(coordinate)
let polygonViewPoint: CGPoint = polygonRenderer.pointForMapPoint(mapPoint)
if polygonRenderer.path.contains(polygonViewPoint) {
print("Your location was inside your polygon.")
}
所以你要考虑你应该把这段代码放在哪里,因为你现在有它在函数中,当MKOverlay被绘制到屏幕上时被调用,这是完全无关的。
已更新为Swift 3
let polygonRenderer = MKPolygonRenderer(polygon: polygon)
let mapPoint: MKMapPoint = MKMapPointForCoordinate(coordinate)
let polygonViewPoint: CGPoint = polygonRenderer.point(for: mapPoint)
if polygonRenderer.path.contains(polygonViewPoint)
{
print("Your location was inside your polygon.")
}
Swift 5解决方案:
因为渲染器。路径的包含方法需要点作为CGPoint,我们需要通过MKMapPoint将它从地理坐标转换为rederer的点,否则结果将是意想不到的。
@objc
func mapTapped(_ tap: UILongPressGestureRecognizer) {
if tap.state == .recognized {
let touchPoint = tap.location(in: mapView)
let coord = mapView.convert(touchPoint, toCoordinateFrom: mapView)
for overlay: MKOverlay in mapView.overlays {
if let polygon = overlay as? MKPolygon {
let renderer = MKPolygonRenderer(polygon: polygon)
let mapPoint = MKMapPoint(coord)
let rendererPoint = renderer.point(for: mapPoint)
if renderer.path.contains(rendererPoint) {
// here comes your code
}
}
}
}
}