iOS MKMapView Center on User Location and then allow unlimited Scrolling in Swift 3.0 -
i don't believe question has been asked yet in swift 3.0 - 3 goals:
upon
viewdidloadmap centers user location @ zoom level can set (for examplelet span: mkcoordinatespan = mkcoordinatespanmake(40.0, 40.0))once map loads , centers on user location, user can move , scroll map other location without map automatically snapping original user location
allow user zoom in level allow user zoom out view entire global map (no restrictions on zoom out level)
here code far:
import uikit import mapkit import corelocation class viewcontroller: uiviewcontroller, cllocationmanagerdelegate { @iboutlet weak var mapview: mkmapview! let locationmanager = cllocationmanager() func locationmanager(_ manager: cllocationmanager, didupdatelocations locations: [cllocation]) { let location = locations[0] let span: mkcoordinatespan = mkcoordinatespanmake(40.0, 40.0) let userlocation: cllocationcoordinate2d = cllocationcoordinate2dmake(location.coordinate.latitude, location.coordinate.longitude) let region: mkcoordinateregion = mkcoordinateregionmake(userlocation, span) mapview.setregion(region, animated: true) self.mapview.showsuserlocation = true } override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. locationmanager.delegate = self locationmanager.desiredaccuracy = kcllocationaccuracybest locationmanager.requestwheninuseauthorization() locationmanager.startupdatinglocation() } }
1. should work code have now.
2. add check subsequent location updates
in didupdatelocations method, add bool check whether region centered on user or not.
var regionhasbeencentered = false func locationmanager(_ manager: cllocationmanager, didupdatelocations locations: [cllocation]) { let location = locations[0] if !regionhasbeencentered { let span: mkcoordinatespan = mkcoordinatespanmake(40.0, 40.0) let userlocation: cllocationcoordinate2d = cllocationcoordinate2dmake(location.coordinate.latitude, location.coordinate.longitude) let region: mkcoordinateregion = mkcoordinateregionmake(userlocation, span) mapview.setregion(region, animated: true) regionhasbeencentered = true } self.mapview.showsuserlocation = true } now map no longer center on user after first update, until change regionhasbeencentered false. allow user scroll , zoom freely.
3. implement mkmapviewdelegate method detect map region changes
implement mkmapviewdelegate on view controller can check region changes.
class viewcontroller: uiviewcontroller, cllocationmanagerdelegate, mkmapviewdelegate { …and set view controller delegate:
override func viewdidload() { // other things… mapview.delegate = self } then implement following method called right before region changes. here can check see if span's dimensions small, , set them minimum appropriate.
func mapview(_ mapview: mkmapview, regionwillchangeanimated animated: bool) { if mapview.region.span.latitudedelta <= 40 && mapview.region.span.longitudedelta <= 40 { let minimumspan = mkcoordinatespan(latitudedelta: 40, longitudedelta: 40) let minimumregion= mkcoordinateregion(center: mapview.centercoordinate, span: minimumspan) mapview.setregion(minimumregion, animated: false) } } important note: mkcoordinatespan documentation, longitudedelta change move toward/away equator.
longitudedelta
the amount of east-to-west distance (measured in degrees) display map region. number of kilometers spanned longitude range varies based on current latitude. example, 1 degree of longitude spans distance of approximately 111 kilometers (69 miles) @ equator shrinks 0 kilometers @ poles.
furthermore, mkcoordinatespan's dimensions measured in degrees, , 40 degrees quite bit want change these values, otherwise user not able zoom in @ all.
Comments
Post a Comment