Centering MKMapView on spot N-pixels below pin

前端 未结 8 1428
梦毁少年i
梦毁少年i 2020-11-30 02:34

Want to center MKMapView on a point N-pixels below a given pin (which may or may not be visible in the current MapRect).

I\'ve been trying

8条回答
  •  Happy的楠姐
    2020-11-30 03:09

    SWIFT 3 UPDATED

    Updated the function with Zoom

    func zoomToPos() {
    
            let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta:  0.1)
    
            // Create a new MKMapRegion with the new span, using the center we want.
            let coordinate = moveCenterByOffset(offset: CGPoint(x: 0, y: 100), coordinate: (officeDetail?.coordinate)!)
            let region = MKCoordinateRegion(center: coordinate, span: span)
    
            mapView.setRegion(region, animated: true)
    
    
        }
    
        func moveCenterByOffset (offset: CGPoint, coordinate: CLLocationCoordinate2D) -> CLLocationCoordinate2D {
            var point = self.mapView.convert(coordinate, toPointTo: self.mapView)
            point.x += offset.x
            point.y += offset.y
            return self.mapView.convert(point, toCoordinateFrom: self.mapView)
        }
    

提交回复
热议问题