问题
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?
The user can determine the limits of his land parcel by creating a polygon and adjusting it afterwards.
Thank you very much.
回答1:
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
回答2:
Google Maps sdk for iOS provides a class named
GMSPolygon
which provides the functionality you are searching for.
Just provide the initializer with a
GMSMutablePath
that contains the coordinates for the polygon.
let polygon = GMSPolygon(path: path)
polygon.strokeColor = .red
polygon.fillColor = .blue
polygon.strokeWidth = 1.0
polygon.map = mapView
回答3:
In swift 5 you can use bellow like. Your image look like bellow
Add pod to your project
pod 'GoogleMaps' pod 'GooglePlaces'
Add api key from google in AppDelegate like bellow
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { GMSServices.provideAPIKey("Your api key") GMSPlacesClient.provideAPIKey("your api key") return true }
Add GMSMapView to your view & connect to viewController
Paste the bellow code to your viewCotroller
import UIKit
import GoogleMaps import GooglePlaces class ViewController: UIViewController { @IBOutlet weak var mapView: GMSMapView! override func viewDidLoad() { super.viewDidLoad() let camera = GMSCameraPosition.camera(withLatitude: 37.4, longitude:-122.0, zoom: 10) let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) mapView.isMyLocationEnabled = true //mapView.myLocationEnabled = true mapView.settings.myLocationButton = true; self.view = mapView // Creates a marker in the center of the map. let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0) marker.title = "Bangladesh" //marker.snippet = "Malaysia" marker.map = mapView // Create a rectangular path let rect = GMSMutablePath() rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0)) rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0)) rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2)) rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2)) // 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 = UIColor.init(hue: 210, saturation: 88, brightness: 84, alpha: 1) //polygon.strokeColor = .black polygon.strokeWidth = 2 polygon.map = mapView } }
You can download full source from GitHub: GitHub Link: https://github.com/enamul95/GoogleMapPolyLine
来源:https://stackoverflow.com/questions/45061084/swift-draw-polygons-shape-in-sdk-google-maps