将GMSMarker添加到Polygon - Google Map iOS Swift

时间:2016-09-24 11:49:49

标签: ios swift google-maps

我有一个多边形:

import UIKit
import GoogleMaps

class ViewController: UIViewController {

    // You don't need to modify the default init(nibName:bundle:) method.

    override func loadView() {
        let camera = GMSCameraPosition.cameraWithLatitude(37.0902, longitude: -95.7129, zoom: 3.0)
        let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
        mapView.myLocationEnabled = true
        view = mapView

        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView

        // Create a rectangular path
        let rect = GMSMutablePath()
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.4757, longitude: -86.41182))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.46599, longitude: -86.41177))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.45054, longitude: -86.41167))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.44245, longitude: -86.41157))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.43993, longitude: -86.41154))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.42573, longitude: -86.41138))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.42417, longitude: -86.41135))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.42185, longitude: -86.41128))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.41017, longitude: -86.41117))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40994, longitude: -86.41117))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.4072, longitude: -86.41615))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40132, longitude: -86.43178))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40025, longitude: -86.43926))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40036, longitude: -86.44653))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40573, longitude: -86.45876))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40285, longitude: -86.4612))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.38769, longitude: -86.46247))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.37729, longitude: -86.46356))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.37368, longitude: -86.46836))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.37136, longitude: -86.47092))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36874, longitude: -86.47306))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36588, longitude: -86.47476))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36418, longitude: -86.47777))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36497, longitude: -86.48023))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36667, longitude: -86.48342))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36674, longitude: -86.4871))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36532, longitude: -86.49047))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36286, longitude: -86.49265))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36032, longitude: -86.49263))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.35787, longitude: -86.49181))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.35513, longitude: -86.4908))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.35264, longitude: -86.48994))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.34985, longitude: -86.4899))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.34705, longitude: -86.49071))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.34509, longitude: -86.49308))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.34451, longitude: -86.49637))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.34444, longitude: -86.49677))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.34441, longitude: -86.49697))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.34039, longitude: -86.51978))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36286, longitude: -86.54242))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.37296, longitude: -86.56658))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36775, longitude: -86.58826))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.38072, longitude: -86.61646))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40307, longitude: -86.61548))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40372, longitude: -86.62671))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40055, longitude: -86.64956))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.37904, longitude: -86.65426))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36937, longitude: -86.66333))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.35659, longitude: -86.67726))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.35356, longitude: -86.68276))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.35677, longitude: -86.69448))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36186, longitude: -86.71309))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36921, longitude: -86.71624))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.3884, longitude: -86.71982))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40129, longitude: -86.71721))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.40061, longitude: -86.73083))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.38914, longitude: -86.75543))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.39047, longitude: -86.78214))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.37337, longitude: -86.78238))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.32449, longitude: -86.7843))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.30819, longitude: -86.80352))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.3184, longitude: -86.82039))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.3402, longitude: -86.81563))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.3408, longitude: -86.81491))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.34299, longitude: -86.81229))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.36582, longitude: -86.81294))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.38605, longitude: -86.8318))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.41849, longitude: -86.8411))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.4304, longitude: -86.83652))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.43893, longitude: -86.85357))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.43869, longitude: -86.86242))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.44592, longitude: -86.86415))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.4606, longitude: -86.86997))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.47188, longitude: -86.87364))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.48191, longitude: -86.88497))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.48886, longitude: -86.88087))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.49656, longitude: -86.88465))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.5124, longitude: -86.89084))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.52588, longitude: -86.89979))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.53685, longitude: -86.90667))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.54915, longitude: -86.90521))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.55933, longitude: -86.90023))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.57819, longitude: -86.89905))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.59242, longitude: -86.90385))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.60835, longitude: -86.90759))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.62085, longitude: -86.91163))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.63178, longitude: -86.9119))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.64452, longitude: -86.9185))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.65322, longitude: -86.91827))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66411, longitude: -86.91767))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66417, longitude: -86.91759))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66417, longitude: -86.91757))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66425, longitude: -86.91241))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66343, longitude: -86.8929))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66258, longitude: -86.87358))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66137, longitude: -86.84557))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66082, longitude: -86.83382))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66016, longitude: -86.81764))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66017, longitude: -86.81319))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66052, longitude: -86.78767))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66098, longitude: -86.74998))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66159, longitude: -86.72508))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.66738, longitude: -86.71349))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70574, longitude: -86.71257))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70609, longitude: -86.66704))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70633, longitude: -86.63189))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70629, longitude: -86.61778))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70632, longitude: -86.60783))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70653, longitude: -86.59515))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70678, longitude: -86.57015))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70704, longitude: -86.52944))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.7073, longitude: -86.4963))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.7076, longitude: -86.48468))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70778, longitude: -86.47197))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70786, longitude: -86.46803))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70809, longitude: -86.45035))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70747, longitude: -86.41583))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.70739, longitude: -86.41312))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.67818, longitude: -86.413))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.67406, longitude: -86.41298))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.61636, longitude: -86.41261))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.58942, longitude: -86.4125))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.57816, longitude: -86.41244))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.55023, longitude: -86.41234))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.53344, longitude: -86.41226))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.52876, longitude: -86.41223))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.52668, longitude: -86.4122))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.52082, longitude: -86.4123))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.51273, longitude: -86.41231))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.50446, longitude: -86.41234))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.50006, longitude: -86.4122))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.49626, longitude: -86.41212))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.49065, longitude: -86.41197))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.48577, longitude: -86.41197))
        rect.addCoordinate(CLLocationCoordinate2D(latitude: 32.48112, longitude: -86.41187))

        // 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.blackColor()
        polygon.strokeWidth = 2
        polygon.map = mapView

    }
}

我需要在多边形的中心添加一个标记。点击标记,我需要显示infowindow。这可能吗?

1 个答案:

答案 0 :(得分:0)

可以在多边形的中心添加标记,但您必须自己计算。正如Daniel's answer中所述,使用创建矩形路径的坐标,您可以使用getCenter()类中的LatLngBounds方法来获取地图的中心。另请查看LatLngBounds class

至于在点击标记时显示InfoWindow,已在the documentation中讨论过。

let position = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.127)
let london = GMSMarker(position: position)
london.title = "London"
london.map = mapView

请注意,一次只会显示1个InfoWindow,如果titlesnippet都为空或nil,则不会显示。

快乐的编码!