2017-04-08 14 views
1

我正在用Swift構建一個應用程序。我已經成功將谷歌地圖,以我的應用程序作爲一個子視圖用下面的代碼我的主視圖控制器:如何在不同的郊區覆蓋帶有淺色的ta GMSMapView?

let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0) 
var mapView = GMSMapView.map(withFrame: CGRect(x: screenWidth*0.03, y: 245, width: screenWidth*0.94, height: screenHeight*0.45), camera: camera) 
self.view.addSubview(mapView) 

我現在想要做的事情有點棘手探測,但我肯定是可能的。我希望能夠在我的mapView中覆蓋地圖的不同區域,通過名稱或郵政編碼進行指定,並使用不同顏色的可調色彩。這似乎是製作漂亮地圖的常用方式,我相信現有解決方案可以輕鬆實現。我應該如何去做這件事?

回答

0

我找到了一種方法來做到這一點! Google Maps API提供!一旦你有你的地圖在我原來的問題設置了像你可以做到這一點的圓添加到地圖:

let circleCenter = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20) 
let circ = GMSCircle(position: circleCenter, radius: 2000) 
circ.fillColor = UIColor.green.withAlphaComponent(0.5) 
circ.map = mapView 

還可以繪製更復雜的形狀,其中包括一系列像這樣的緯度/多頭:

let rect = GMSMutablePath() 
rect.add(CLLocationCoordinate2D(latitude: 37.886080, longitude: -122.137585)) 
rect.add(CLLocationCoordinate2D(latitude: 37.899356, longitude: -122.130203)) 
rect.add(CLLocationCoordinate2D(latitude: 37.900101, longitude: -122.104282)) 
rect.add(CLLocationCoordinate2D(latitude: 37.879644, longitude: -122.101192)) 
let polygon = GMSPolygon(path: rect) 
polygon.fillColor = UIColor.flatWatermelonDark.withAlphaComponent(0.5) 
polygon.strokeColor = .black 
polygon.strokeWidth = 2 
polygon.isTappable = true 
polygon.map = mapView 

我還沒有想出了一個無縫的方式來覆蓋與覆蓋任何郵政編碼但是這可以手動上面的代碼段通過查找/估計郵政編碼區域的緯度/經度的邊緣來進行。