Właściwie kod keithbhunter jest powolny, ponieważ oprócz aktualizowania region szybciej niż może go załadować, mapa zmienia się także wysokość, która powoduje dodatkowe obciążenie!
Zaktualizowałem kod tak, aby działał płynnie.
Tym kodem, co robię, jest utrzymanie mapy w tym samym rozmiarze, ale zamiast tego przesuwaj punkt środkowy, aby zrekompensować wysokość przesuwanego widoku.
Aby ten kod zadziałał, musisz zmodyfikować konfigurację keithbhuntera tak, aby dolne ograniczenie mapView zostało całkowicie przypięte do dołu widoku (a nie do przesuwnego widoku (aby mapView zawsze miał taki sam rozmiar jak super widok) . dla reszty konfiguracja jest taka sama.
również możliwe jest dostosowanie ilości zoom ze zmienną maxMetersDistance
ja tu jestem, zawsze skupiając się na wieżą Eifla
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var slidingView: UIView!
@IBOutlet weak var slidingViewHeight: NSLayoutConstraint!
var maxMetersDistance:CGFloat = 10000.0; // customize this to set how far the map zooms out of the interest area
override func viewDidLoad() {
super.viewDidLoad()
let pan = UIPanGestureRecognizer(target: self, action: "viewDidPan:")
self.slidingView.addGestureRecognizer(pan)
firstTimeCenter()
}
func firstTimeCenter(){
var coordinate = CLLocationCoordinate2D(latitude: 48.8582, longitude: 2.2945)
let region = MKCoordinateRegionMakeWithDistance(coordinate, Double(maxMetersDistance), Double(maxMetersDistance))
self.mapView.setRegion(region, animated: true)
}
func reloadMap() {
let height = CGFloat(self.slidingViewHeight.constant)
var regionDistance = (maxMetersDistance/self.view.frame.height) * height
regionDistance = maxMetersDistance - regionDistance
var coordinate = CLLocationCoordinate2D(latitude: 48.8582, longitude: 2.2945)
var mapRect = mapView.visibleMapRect;
var metersPerMapPoint = MKMetersPerMapPointAtLatitude(coordinate.latitude);
var metersPerPixel = CGFloat(metersPerMapPoint) * CGFloat(mapRect.size.width)/CGFloat(mapView.bounds.size.width);
var totalMeters = Double(metersPerPixel) * Double(height/2)
coordinate = self.translateCoord(coordinate, MetersLat: -totalMeters, MetersLong: 0.0)
let region = MKCoordinateRegionMakeWithDistance(coordinate, Double(regionDistance), Double(regionDistance))
self.mapView.setRegion(region, animated: false)
}
func viewDidPan(panGesture: UIPanGestureRecognizer) {
let location = panGesture.locationInView(self.view)
self.slidingViewHeight.constant = self.view.frame.size.height - location.y
self.reloadMap()
}
func translateCoord(coord:CLLocationCoordinate2D, MetersLat:Double, MetersLong:Double)->CLLocationCoordinate2D{
var tempCoord = CLLocationCoordinate2D()
var tempRegion = MKCoordinateRegionMakeWithDistance(coord, MetersLat, MetersLong);
var tempSpan = tempRegion.span;
tempCoord.latitude = coord.latitude + tempSpan.latitudeDelta;
tempCoord.longitude = coord.longitude + tempSpan.longitudeDelta;
return tempCoord;
}
}
próbowałeś 'setCenterCoordinate' podejście? –
Czy mapa ma być powiększana i zmniejszana podczas przesuwania nakładki? – keithbhunter
@keithbhunter Tak, do wewnątrz, gdy patrzysz w górę i na zewnątrz, gdy przesuwasz się w dół .. – Aodh