gpt4 book ai didi

ios - 获取注释的边界

转载 作者:行者123 更新时间:2023-11-28 07:52:49 25 4
gpt4 key购买 nike

我试图找到我在 MapView 上的注释列表的边界。

我看到在 javascript 中,您可以将注释添加到层的 FeatureGroup,然后获取该组的边界。

但是,我在 Swift SDK 中找不到它。我怎样才能获得一组注释的界限?

最佳答案

您需要使用MKMapView.annotations 属性,并获取minXminYma​​xXma​​xY,之后您可以将这些点作为坐标返回,并根据需要添加叠加层

func getCordinatesOfRectAnnotations(annotationsArray:[MKAnnotation]) ->[CLLocationCoordinate2D]{
let minX : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).x}).min()!
let minY : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).y}).min()!
let maxX : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).x}).max()!
let maxY : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).y}).max()!

var result : [CLLocationCoordinate2D] = []
result.append(MKCoordinateForMapPoint(MKMapPoint(x: minX, y: minY)))
result.append(MKCoordinateForMapPoint(MKMapPoint(x: maxX, y: minY)))
result.append(MKCoordinateForMapPoint(MKMapPoint(x: maxX, y: maxY)))
result.append(MKCoordinateForMapPoint(MKMapPoint(x: minX, y: maxY)))
return result
}

完整示例代码

import UIKit
import MapKit

class ViewController: UIViewController {

@IBOutlet weak var mapView: MKMapView!
var moscow: [Camera] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let coordinates = [CLLocationCoordinate2D(latitude: 40.741895, longitude: -73.989308),CLLocationCoordinate2D(latitude: 38.80495938042258, longitude: -77.0273450631467),CLLocationCoordinate2D(latitude: 40.14404110473566, longitude: -3.487181320896525),CLLocationCoordinate2D(latitude: 41.3425986835203, longitude: 2.093873366603475),CLLocationCoordinate2D(latitude: -15.70239476628682, longitude: 27.486982547083358)]

for (index,coordinate) in coordinates.enumerated() {
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "coordinate\(index)"
self.mapView.addAnnotation(annotation)
}

self.mapView.delegate = self

let overlay = MKPolygon(coordinates: self.getCordinatesOfRectAnnotations(annotationsArray: self.mapView.annotations), count: self.getCordinatesOfRectAnnotations(annotationsArray: self.mapView.annotations).count)
self.mapView.add(overlay)
}

func getCordinatesOfRectAnnotations(annotationsArray:[MKAnnotation]) ->[CLLocationCoordinate2D]{
let minX : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).x}).min()!
let minY : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).y}).min()!
let maxX : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).x}).max()!
let maxY : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).y}).max()!

var result : [CLLocationCoordinate2D] = []
result.append(MKCoordinateForMapPoint(MKMapPoint(x: minX, y: minY)))
result.append(MKCoordinateForMapPoint(MKMapPoint(x: maxX, y: minY)))
result.append(MKCoordinateForMapPoint(MKMapPoint(x: maxX, y: maxY)))
result.append(MKCoordinateForMapPoint(MKMapPoint(x: minX, y: maxY)))
return result
}

}

extension ViewController : MKMapViewDelegate
{
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolygonRenderer(overlay: overlay)
renderer.lineWidth = 1
renderer.strokeColor = UIColor.red
return renderer
}

// Called when the annotation was added
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}

let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.animatesDrop = true
pinView?.canShowCallout = true
pinView?.isDraggable = true
pinView?.pinColor = .purple

let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
pinView?.rightCalloutAccessoryView = rightButton as? UIView
} else {
pinView?.annotation = annotation
}

return pinView
}

}

结果

enter image description here

关于ios - 获取注释的边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49249719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com