SDK谷歌地图中的Swift Draw多边形/形状

时间:2017-07-12 14:52:08

标签: ios swift google-maps polygon shape

我正在使用Google Maps SDK在swift 3.0中开发应用程序。我不得不说这是我第一次使用这个SDK,所以我需要一些帮助。我想我问你的事情对你来说很容易。我想要做的是当用户进入地图编辑模式时(如此页http://www.birdtheme.org/useful/v3tool.html)。他可以做两件事:

  • 首先,他可以在地图上绘制多边形(以标记农民田地的边界)。我希望用户按下地图上的某个点并生成一条直到下一个点。通过这种方式,他将生成直到多边形关闭的直线。 我在下面绘制多边形附加的代码。但问题是它永远不会关闭多边形。但问题是我想要它在线的开头和末端被触摸或线交叉,关闭多边形。我该怎么做它?正如你在图片中看到的那样,我可以画出无穷无尽的线条。

    let pathEditField = GMSMutablePath()
    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    
        if modeEditing {
            pathEditField.add(coordinate)
            let polyline = GMSPolyline(path: pathEditField)
            polyline.map = mapView
    
        }
    }
    
  • 其次,绘制多边形后,顶点会以某种方式突出显示,然后您可以移动该顶点以完成调整。

我给你留个截图,这样你就可以知道用户可以在哪里创建多边形以及在哪里调整顶点。

问题是我不知道怎么做,除了我找不到它的文档。我的问题是,你知道任何类似的教程吗?或者你可以在不编写代码的情况下启动代码吗?

enter image description here

用户可以通过创建多边形并在之后进行调整来确定其地块的限制。

非常感谢。

3 个答案:

答案 0 :(得分:3)

使用此代码创建多边形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

答案 1 :(得分:0)

适用于iOS的Google Maps sdk提供了一个名为

的类
  

GMSPolygon

提供您要搜索的功能。

只需为初始化程序提供

  

GMSMutablePath

包含多边形的坐标。

let polygon = GMSPolygon(path: path)
polygon.strokeColor = .red
polygon.fillColor = .blue
polygon.strokeWidth = 1.0
polygon.map = mapView

答案 2 :(得分:0)

在Swift 5中,您可以像下面这样使用波纹管。 您的图像看起来像波纹管 enter image description here

  1. 将pod添加到您的项目

    pod“ GoogleMaps”   pod'GooglePlaces'

  2. 像波纹管一样在AppDelegate中从Google添加api密钥

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
      GMSServices.provideAPIKey("Your api key")
      GMSPlacesClient.provideAPIKey("your api key")
    
     return true
    }
    
  3. 将GMSMapView添加到您的视图并连接到viewController

  4. 将下面的代码粘贴到viewCotroller

    导入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
    
      }
    
     }
    

    您可以从GitHub下载完整源代码:GitHub链接:https://github.com/enamul95/GoogleMapPolyLine