Centering MKMapView on spot N-pixels below pin

前端 未结 8 1393
梦毁少年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 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)
        }
    
    0 讨论(0)
  • 2020-11-30 03:11

    One easy solution is that you make the frame of your map view larger than the visible area. Then position your pin in the center of the map view and hide all the unwanted areas behind another view or outside of the screen bounds.

    Let me elaborate. If I look at your screen shot, do the following:

    The distance between you pin and the bottom is 353 pixel. So make your map views frame twice the height: 706 pixel. You screenshot has a height of 411 pixel. Position your frame at an origin of 706px - 411px = -293 pixel. Now center your map view at the coordinate of the pin and you are done.

    Update 4-March-2014:

    I created a small sample application with Xcode 5.0.2 to demo this: http://cl.ly/0e2v0u3G2q1d

    0 讨论(0)
提交回复
热议问题