在iOS Swift的Google Maps中以特定的相机缩放隐藏和显示自定义标记

时间:2018-07-23 06:15:04

标签: ios swift google-maps google-maps-markers

如何在特定的相机变焦下隐藏和显示我的自定义标记?

例如,如果我的相机变焦超过13,则隐藏所有自定义标记;如果低于13,则显示标记。

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    let camera = GMSCameraPosition.camera(withLatitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!, zoom: 15)

    // Map initiation code
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView
    mapView.mapType = .normal
    mapView.isMyLocationEnabled = true
    mapView.settings.myLocationButton = true
    mapView.settings.compassButton = true
    mapView.setMinZoom(6, maxZoom: 19)

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.icon = self.imageWithImage(image: UIImage(named: "marker_64")!, scaledToSize: CGSize(width: 24.0, height: 24.0))
    marker.position = CLLocationCoordinate2D(latitude: 25.034265, longitude: 121.564537)
    marker.title = "Taipei 101"
    marker.snippet = "101"
    marker.map = mapView
    marker.tracksViewChanges = true
}

    func imageWithImage(image:UIImage, scaledToSize newSize:CGSize) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
        image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

    return newImage 
} 

0 个答案:

没有答案