Centering MKMapView on spot N-pixels below pin

前端 未结 8 1449
梦毁少年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条回答
  •  余生分开走
    2020-11-30 02:49

    For Swift:

    import MapKit
    
    extension MKMapView {
    
    func moveCenterByOffSet(offSet: CGPoint, coordinate: CLLocationCoordinate2D) {
        var point = self.convert(coordinate, toPointTo: self)
    
        point.x += offSet.x
        point.y += offSet.y
    
        let center = self.convert(point, toCoordinateFrom: self)
        self.setCenter(center, animated: true)
    }
    
    func centerCoordinateByOffSet(offSet: CGPoint) -> CLLocationCoordinate2D {
        var point = self.center
    
        point.x += offSet.x
        point.y += offSet.y
    
        return self.convert(point, toCoordinateFrom: self)
    }
    }
    

提交回复
热议问题