gpt4 book ai didi

swift - 如何在 Swift 中制作没有交点的多边形

转载 作者:搜寻专家 更新时间:2023-11-01 07:01:56 25 4
gpt4 key购买 nike

在 IOS Swift 和 Google map sdk 中,我想要每个顶点都是标记的矩形区域。所以,我不希望多边形有交集,如果有交集,我会自动重新制作成矩形。我可以找到如何检测交叉点,但无法重新制作。我怎样才能做到这一点?..请帮助..

我想做之前到之后。

之前:

before

之后:

after

View Controller ..

import UIKit
import GoogleMaps

class CreateLandmarkLocationVC: UIViewController {
var counterMarker: Int = 0
var allMarkers:[GMSMarker] = []


// You don't need to modify the default init(nibName:bundle:) method.

override func loadView() {
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.

let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

mapView.delegate = self
mapView.isMyLocationEnabled = true
view = mapView

}
}

extension CreateLandmarkLocationVC: GMSMapViewDelegate {

func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
// Custom logic here
if counterMarker < 4 {
let marker = GMSMarker()
marker.position = coordinate
marker.title = "I added this with a long tap"
marker.snippet = ""
allMarkers.append(marker)
counterMarker += 1
// Create the polygon, and assign it to the map.
mapView.clear()
let rect = GMSMutablePath()
for mark in allMarkers {
rect.add(mark.position)
mark.map = mapView
}
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView

}
}

func mapView(_ mapView: GMSMapView, didLongPressInfoWindowOf marker: GMSMarker) {
marker.map = nil
for (index, cmark) in allMarkers.enumerated() {
if cmark.position.latitude == marker.position.latitude, cmark.position.longitude == marker.position.longitude {
allMarkers.remove(at: index)
break;
}
}
counterMarker -= 1

mapView.clear()
let rect = GMSMutablePath()
for mark in allMarkers {
rect.add(mark.position)
mark.map = mapView
}

// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView
}
}

最佳答案

为了解决您的问题,您应该按顺时针方向重新排列 GMSMutablePath 的坐标。我修改了您的示例并添加了执行此任务的函数 reorderMarkersClockwise()、isLess() 和 getCenterPointOfPoints()。代码片段如下

import UIKit
import GoogleMaps

class ViewController: UIViewController {
var counterMarker: Int = 0
var allMarkers:[GMSMarker] = []

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func loadView() {
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

mapView.delegate = self
mapView.isMyLocationEnabled = true
view = mapView
}
}

extension ViewController: GMSMapViewDelegate {

func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
// Custom logic here
if counterMarker < 4 {
let marker = GMSMarker()
marker.position = coordinate
marker.title = "I added this with a long tap"
marker.snippet = ""
allMarkers.append(marker)
counterMarker += 1
// Create the polygon, and assign it to the map.
mapView.clear()
let rect = reorderMarkersClockwise(mapView)
for mark in allMarkers {
mark.map = mapView
}
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView

}
}

func mapView(_ mapView: GMSMapView, didLongPressInfoWindowOf marker: GMSMarker) {
marker.map = nil
for (index, cmark) in allMarkers.enumerated() {
if cmark.position.latitude == marker.position.latitude, cmark.position.longitude == marker.position.longitude {
allMarkers.remove(at: index)
break;
}
}
counterMarker -= 1

mapView.clear()
let rect = reorderMarkersClockwise(mapView)
for mark in allMarkers {
mark.map = mapView
}

// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView
}

func reorderMarkersClockwise(_ mapView: GMSMapView) -> GMSMutablePath {
let rect = GMSMutablePath()
if (counterMarker > 1) {
let arr = allMarkers.map{$0.position}.sorted(by: isLess)
for pos in arr {
rect.add(pos)
}
} else {
for mark in allMarkers {
rect.add(mark.position)
}
}
return rect
}

func isLess(_ a: CLLocationCoordinate2D, _ b: CLLocationCoordinate2D) -> Bool {
let center = getCenterPointOfPoints()

if (a.latitude >= 0 && b.latitude < 0) {
return true
} else if (a.latitude == 0 && b.latitude == 0) {
return a.longitude > b.longitude
}

let det = (a.latitude - center.latitude) * (b.longitude - center.longitude) - (b.latitude - center.latitude) * (a.longitude - center.longitude)
if (det < 0) {
return true
} else if (det > 0) {
return false
}

let d1 = (a.latitude - center.latitude) * (a.latitude - center.latitude) + (a.longitude - center.longitude) * (a.longitude - center.longitude)
let d2 = (b.latitude - center.latitude) * (b.latitude - center.latitude) + (b.longitude - center.longitude) * (b.longitude - center.longitude)
return d1 > d2
}

func getCenterPointOfPoints() -> CLLocationCoordinate2D {
let arr = allMarkers.map {$0.position}
let s1: Double = arr.map{$0.latitude}.reduce(0, +)
let s2: Double = arr.map{$0.longitude}.reduce(0, +)
let c_lat = arr.count > 0 ? s1 / Double(arr.count) : 0.0
let c_lng = arr.count > 0 ? s2 / Double(arr.count) : 0.0
return CLLocationCoordinate2D.init(latitude: c_lat, longitude: c_lng)
}
}

您还可以在 GitHub 上找到示例项目:https://github.com/xomena-so/so50679742 .使用您的 API key 运行示例项目。

enter image description here

希望对您有所帮助!

关于swift - 如何在 Swift 中制作没有交点的多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679742/

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