konyv12
konyv12

Reputation: 776

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
}

Upvotes: 4

Views: 358

Answers (2)

DonMag
DonMag

Reputation: 77423

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:

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]

Upvotes: 1

Khalid Afridi
Khalid Afridi

Reputation: 923

use a for loop and add each coordinate in an array

for point in points {
   // This will loop through all the points and add them to pointsArray
   pointsArray.append(point)
}

Upvotes: 1

Related Questions