点击以绘制折线和多边形Google地图

时间:2018-06-21 18:29:59

标签: ios swift google-maps

我做了几个按钮,以便可以通过点击地图来绘制折线和多边形。我不知道该怎么做。感谢您的帮助。

点击地图上的两个或多个位置,然后将它们与折线合并,或者将其与多边形封闭

这是我的代码:

import UIKit
import GoogleMaps
import Floaty

class CroquiViewController: UIViewController, FloatyDelegate, GMSMapViewDelegate {

var locationManager = CLLocationManager()
var floaty = Floaty()
var flag = Int()

@IBOutlet weak var mapView: GMSMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    mapView.delegate = self
    // Do any additional setup after loading the view.
    layoutFAB()
}
func layoutFAB() {
    let item = FloatyItem()
    item.hasShadow = false

    floaty.hasShadow = false
    floaty.buttonColor = #colorLiteral(red: 0.2558874488, green: 0.5951498747, blue: 0.8528174758, alpha: 1)
    floaty.plusColor = UIColor.white
    floaty.itemButtonColor = #colorLiteral(red: 0.2558874488, green: 0.5951498747, blue: 0.8528174758, alpha: 1)
    floaty.addItem("Ponto", icon: UIImage(named: "icons8-marker")){ item in
        self.flag = 1
    }
    floaty.addItem("Linhas", icon: UIImage(named: "icons8-polyline")){ item in
        self.flag = 2
    }
    floaty.addItem("Polígonos", icon: UIImage(named: "icons8-polygone")) { item in
        self.flag = 3
    }
    floaty.paddingX = self.view.frame.width/2 - floaty.frame.width/0.32
    floaty.fabDelegate = self

    self.view.addSubview(floaty)

}


func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
    if self.flag == 1{
    print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
    //mapView.clear() // clearing Pin before adding new
    let marker = GMSMarker(position: coordinate)
    marker.map = mapView
    }else if self.flag == 2{
     print(flag)
    }else if self.flag == 3{
        print(flag)
    }
}}

1 个答案:

答案 0 :(得分:0)

这些功能可以为您提供帮助。

mapView()->将点击的坐标插入数组并在其中创建标记

createMarker()->在该坐标上创建标记

createPolygon()->使用该数组创建多边形(在需要的任何按钮点击时调用此函数)

@IBOutlet weak var mapView: GMSMapView!
var coordinates: [CLLocationCoordinate2D] = []
var polygonPath = GMSPolyline();

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
    let destination = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)

    coordinates.append(destination)
    createMarker(point: coordinate)
}

func createMarker(point: CLLocationCoordinate2D){
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: point.latitude, longitude: point.longitude)
    marker.map = mapView
}

func createPolygon(){

    let path = GMSMutablePath()
    for coordinate in coordinates{
        path.add(CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude))
    }
    path.add(CLLocationCoordinate2D(latitude: coordinates[0].latitude, longitude: coordinates[0].longitude))

    polygonPath = GMSPolyline(path: path)
    polygonPath.strokeColor = UIColor.red
    polygonPath.strokeWidth = 2.0
    polygonPath.map = mapView
}