I am building an app with Swift. I have successfully added a google map to my app as a subview on my main view controller with the following code:
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)
I now want to do something somewhat tricky sounding but that I am sure is possible. I want to be able to cover different regions of the map in my mapView, specified by name or zipcode, with different colored tints that would be tappable. This seems like it would be a common way to make maps pretty and I am sure there is an existing solution to do it easily. How should I go about doing this?
I have found a way to do this! Google Maps API provides! Once you have your map set up like in my original question you can do this to add a circle to the map:
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
You can also draw more complex shapes that include a series of lat/longs like so:
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
I still have not figured out a seamless way to cover any zipcode with an overlay but this can be done manually with the above snippets of code by looking up/estimating the lat/long edges of zipcode areas.