如何使用Google Maps API在Swift中使我的GMSPolygon可用

时间:2017-04-26 15:12:22

标签: ios swift google-maps polygon gmsmapview

谷歌地图API工作,所有二进制库链接,-Obj在目标"其他链接器标志"包含和objc头文件构造,这个代码基本上直接从谷歌开发人员演示如何添加事件窃听。然而,由于某种原因 - 即使我的地图和多边形和标记工作 - 我无法弄清楚如何使我的单个多边形可以应用。 (我最终想让它从红色变为绿色,然后用户点击)。我是swift和xcode的初学者,使用最新的更新,可以真正使用一些帮助!

这是我的代码:

class ViewController: UIViewController, GMSMapViewDelegate {

override func loadView() {
    let camera = GMSCameraPosition.camera(withLatitude: 40.0, longitude: -75.3, zoom: 17.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.delegate = self
    self.view = mapView

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: 40.0, longitude: -75.3)
    marker.title = "University1"
    marker.isTappable = true
    marker.map = mapView

    // Create a rectangular path
    let K1 = GMSMutablePath()
    K1.add(CLLocationCoordinate2D(latitude: 40.04080470744396, longitude: -75.34219389549136))
    K1.add(CLLocationCoordinate2D(latitude: 40.04082335573317, longitude: -75.34220634745961))
    K1.add(CLLocationCoordinate2D(latitude: 40.0407983689258, longitude: -75.34226262271473))
    K1.add(CLLocationCoordinate2D(latitude: 40.04078009018794, longitude: -75.34224985832812))
    let K1polygon = GMSPolygon(path: K1)
    K1polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
    K1polygon.strokeColor = .red
    K1polygon.strokeWidth = 2
    K1polygon.isTappable = true
    K1polygon.map = mapView

}}
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
print("You did it!")
}

let geocoder = GMSGeocoder()

func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
mapView.clear()
}

func mapView(_ mapView: GMSMapView, idleAt cameraPosition: GMSCameraPosition)    {
geocoder.reverseGeocodeCoordinate(cameraPosition.target) { (response, error)    in
    guard error == nil else {
        return
    }

    if let result = response?.firstResult() {
        let marker = GMSMarker()
        marker.position = cameraPosition.target
        marker.title = result.lines?[0]
        marker.snippet = result.lines?[1]
        marker.map = mapView
    }
}}

0 个答案:

没有答案