我是一名初学者程序员。
我正在开发一个谷歌地图应用,当点击一个按钮时,我希望所有点击的坐标(我已经有了这个)停止记录,并开始填充新的数组。这是为了保存地图上的坐标。
最好的方法是什么?
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
}