[英]How do I achieve this in iOS (swift)?
I am a beginner programmer. 我是初学程序员。
I am developing a Google Maps app and on a click of a button, I would like all of the tapped coordinates (I already have this) to stop recording, and instead, start to populate new arrays. 我正在开发一个谷歌地图应用程序,只需点击一下按钮,我希望所有的点击坐标(我已经有了这个)停止录制,而是开始填充新的数组。 This is in order to save coordinates for a map.
这是为了保存地图的坐标。
What would be the best way to go about it? 最好的方法是什么?
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
var lat: CLLocationDegrees = coordinate.latitude
var lng: CLLocationDegrees = coordinate.longitude
var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
markersArray.append(formattedCoordinate)
mapView.clear()
addMarker()
drawPolygon()
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
var lat: CLLocationDegrees = marker.position.latitude
var lng: CLLocationDegrees = marker.position.longitude
var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
markersArray = markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude)) })
mapView.clear()
drawPolygon()
addMarker()
return true
}
func mapView(_ mapView: GMSMapView, didBeginDragging marker: GMSMarker){
var lat: CLLocationDegrees = marker.position.latitude
var lng: CLLocationDegrees = marker.position.longitude
var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
markersArray = markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude))})
}
func mapView(_ mapView: GMSMapView, didDrag marker: GMSMarker){
var lat: CLLocationDegrees = marker.position.latitude
var lng: CLLocationDegrees = marker.position.longitude
var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
markersArray.append(formattedCoordinate)
mapView.clear()
drawPolygon()
addMarker()
markersArray = markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude))})
}
func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker){
var lat: CLLocationDegrees = marker.position.latitude
var lng: CLLocationDegrees = marker.position.longitude
var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
markersArray.append(formattedCoordinate)
mapView.clear()
drawPolygon()
addMarker()
}
func addMarker(){
for coordinate in markersArray {
let marker = GMSMarker()
marker.position = coordinate
marker.map = mapView
marker.isDraggable = true
marker.icon = GMSMarker.markerImage(with: UIColor.blue)
}
}
func drawPolygon(){
let path = GMSMutablePath()
var i = 0
// get path for polygon
for coordinate in markersArray {
path.add(coordinate)
i = i+1
}
// build polygon
let polygon = GMSPolygon(path:path)
polygon.map = nil;//not sure if this line is redundant
polygon.fillColor = UIColor.green
polygon.strokeColor = UIColor.black
polygon.strokeWidth = 1
polygon.map = mapView
}
use a for loop and add each coordinate in an array 使用for循环并在数组中添加每个坐标
for point in points {
// This will loop through all the points and add them to pointsArray
pointsArray.append(point)
}
I'm assuming you have somewhere in your code: 我假设你的代码中有某个地方:
var markersArray = [CLLocationCoordinate2D]()
which defines your array of coordinates. 它定义了你的坐标数组。
Your code is adding points to that array as follows: 您的代码是向该数组添加点,如下所示:
markersArray.append(formattedCoordinate)
If you want to save "sets" of coordinates, add this where you define your markersArray: 如果要保存坐标的“集合”,请在定义markersArray的位置添加:
var arrayOfMarkersArrays = Array<Array<CLLocationCoordinate2D>>()
Then, on a button tap, you can do this: 然后,在按钮上点按,您可以这样做:
// add the current array of points to the "array of arrays"
arrayOfMarkersArrays.append(markersArray)
// "reset" your tracking array
markersArray.removeAll()
You can now add points to a new "set" ... add that set to the "array of arrays" ... and then refer back to them in this way: 您现在可以将点添加到新的“集合”...将该集添加到“数组数组”...然后以这种方式返回它们:
// get the first "set" of coordinates
markersArray = arrayOfMarkersArrays[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.