Swift Draw polygons/shape in SDK Google Maps

2019-07-24 22:13发布

I'm developing an application in swift 3.0 with the Google Maps SDK. I have to say this is the first time I work with this SDK and it is so I need a little help. I think what I ask you will be easy for you. What I want to do is when the user enters a map editing mode (like this page http://www.birdtheme.org/useful/v3tool.html ). He can do two things:

  • First he could draw polygons on the map (to mark the boundaries of a farmer field). I want the user to press a point on the map and generate a line until the next point. In this way, he will generate lines until the polygon is closed. My code for drawing polygon attach below. But the problem is that it never closes the polygon.But the problem is that I want it when the beginning of the line and the end are touched or the lines cross, close the polygon.How do I do it? As you see in the picture, I can draw infinite lines.

    let pathEditField = GMSMutablePath()
    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    
        if modeEditing {
            pathEditField.add(coordinate)
            let polyline = GMSPolyline(path: pathEditField)
            polyline.map = mapView
    
        }
    }
    
  • Second, once the polygon is drawn, the vertices are highlighted in some way, then you can move that vertex to finish adjusting.

I leave you a screenshot, so that you get an idea where the user can create the polygon and where to adjust the vertices.

The problem is that I do not know how to do it besides I can not find documentation for it. My question is, do you know any tutorial where something similar is done? Or can you start the code a bit without writing it?

enter image description here

The user can determine the limits of his land parcel by creating a polygon and adjusting it afterwards.

Thank you very much.

1条回答
霸刀☆藐视天下
2楼-- · 2019-07-24 22:26

Use this code to create polygon GoogleMaps documentaion

 let camera = GMSCameraPosition.camera(withLatitude: 76.0,
                                          longitude: 87.0,
                                          zoom: 18)
    let mapView = GMSMapView.map(withFrame: .zero, camera: camera)

    let path = GMSMutablePath()
    //Add vertex's to Path like as shown bellow
    //get vertices from map
   // https://developers.google.com/maps/documentation/ios-sdk/shapes

let path = GMSMutablePath()
path.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
path.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
path.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
path.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))
path.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))

    let polyline = GMSPolyline(path: path)
    polyline.strokeColor = .blue
    polyline.strokeWidth = 5.0
    polyline.map = mapView
查看更多
登录 后发表回答