Using IBAction Buttons to Zoom MapView

前端 未结 6 1549
谎友^
谎友^ 2021-01-02 17:17

I have an issue. My current location is displayed and centered in a map view however the map region doesn\'t get zoomed in to. I tried taking Rob\'s advice by taking span an

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-02 18:09

    Swift 3:

    import GoogleMaps
    import GooglePlaces
    import CoreLocation
    import UIKit
    
    class LocationMapView: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
    
         @IBOutlet weak var mapView: GMSMapView!
    
         var mapViewZoomStepperValue: Float = 0.0
         @IBOutlet weak var mapViewZoomStepper: UIStepper!
    
         override func viewDidLoad() {
                super.viewDidLoad()
    
                mapViewZoomStepper.minimumValue = -100
    
            }
    
            @IBAction func mapViewZoomStepperValueChanged(_ sender: Any) {
    
                if (Float(mapViewZoomStepper.value) > mapViewZoomStepperValue){
    
                    mapViewZoomStepperValue = Float(mapViewZoomStepper.value)
                    zoomIn()
    
                }else{
    
                    mapViewZoomStepperValue = Float(mapViewZoomStepper.value)
                    zoomOut()
    
                }
    
            }
    
            func zoomIn() {
    
                print("Zoom in!!")
                if mapView.camera.zoom == mapView.maxZoom {
                    return
                }
    
                let currentZoom = mapView.camera.zoom + 1
                mapViewZoomController(currentZoom)
    
            }
    
            func zoomOut() {
    
                print("Zoom out!!")
                if mapView.camera.zoom == mapView.minZoom {
                    return
                }
    
                let currentZoom = mapView.camera.zoom - 1
                mapViewZoomController(currentZoom)
    
            }
    
            func mapViewZoomController(_ level: Float) {
    
                let point: CGPoint = mapView.center
                let coor: CLLocationCoordinate2D = mapView.projection.coordinate(for: point)
                let camera = GMSCameraPosition.camera(withLatitude: coor.latitude, longitude: coor.longitude, zoom: Float(level))
                mapView.animate(to: camera)
    
            }
    
    }
    

提交回复
热议问题