Retain map zoom level while there is a change in location - ios

I am using a MKMapView application. but when zoom into the map to the current user location, the map zooms well. But when there is a change in current location, the map automatically zooms out to the original position where it all started. How can I retain the same level of zoom even when the location changes. Thank you.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let latestlocation:CLLocation = locations[locations.count-1]
let a = latestlocation.coordinate.latitude
let b = latestlocation.coordinate.longitude
let center = CLLocationCoordinate2D(latitude: a, longitude: b)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpanMake(0.05, 0.05))
self.mapView.setRegion(region, animated: false)
mapView.setUserTrackingMode(.follow, animated: true)
self.mapView.showsUserLocation = true
}

Related

Mapkit - Swift4 - How to disable Auto zooming

Apologies for the absurdely junior question but for some reason I can't grasp how to stop this from happening.
When I run the app, and I pinch to zoom into the map (or out, or navigate away from my position) the app will always pull me back to a set height and to my location. I'd like to not have this happen and just have the experience of zooming to whatever level and moving around without being interrupted.
Any ideas?
Here's the code snip from my VC:
let regionRadius: CLLocationDistance = 2000
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius, regionRadius)
mapView.setRegion(coordinateRegion, animated: true)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let center = location.coordinate
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: center, span: span)
mapView.setRegion(region, animated: true)
mapView.showsUserLocation = true
}
Thanks for any help
Location Manager delegate methods can be called very frequently and at any time. So you need to stop updating locations.
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
manager.delegate = nil
let location = locations[0]
...
}

Prevent zooming/heading update stop in iOS MKMapView

In iOS I have an MKMapView and CLLocationManager to update the heading and location. No matter what I do, I can't get the map view to stop zooming in tightly on the user's location. I have annotations on the map that I want visible at all times, and I want the heading to update as the user spins the device. This is working using mapView.setRegion, but after a bit, the map locks at the north and no longer rotates. Below is the CLLocationManager code I'm using:
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
if data != nil{
setRegion(locationManager?.location?.coordinate.latitude)!, longitude: (locationManager?.location?.coordinate.longitude)!), data: data!)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if !regionSet && data != nil{
setRegion(location: CLLocationCoordinate2D(latitude: (locationManager?.location?.coordinate.latitude)!, longitude: (locationManager?.location?.coordinate.longitude)!), data: data!)
regionSet = true
}
}
setRegion calculates the extents of the map to zoom on and sets the setRegion function appropriately.
Below is setRegion. I calculate the zoomWindowDegrees variable when I load in the annotations by finding the maximum lat/long for the annotations and the use that in the function.
private func setRegion(location:CLLocationCoordinate2D, towers:[Tower]){
let center = location
let coordinateSpan = MKCoordinateSpan(latitudeDelta: zoomWindowDegrees, longitudeDelta: zoomWindowDegrees)
let region = MKCoordinateRegion(center: center, span: coordinateSpan)
mapView.setRegion(region, animated: false)
}

ios mapkit, carrier name crash with [LogMessageLogging] 6.1 Unable to retrieve CarrierName

I have added MapKit in my app. I have connected MKmapview to my viewcontroller by IBOUTLET, it runs fine in simulator but it crashes in my device with this
[LogMessageLogging] 6.1 Unable to retrieve CarrierName. CTError: domain-2, code-5, errStr:((os/kern) failure).
I am taking user's location from CLLocationmanager and trying to add annotation in the map. Code is as below:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
let coordinate = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
MD_myCurrentLocation = coordinate
setupLocationMarker(coordinate:coordinate)
manager.stopUpdatingLocation()
}
func setupLocationMarker(coordinate: CLLocationCoordinate2D) {
iosMap.removeAnnotations(iosMap.annotations)
let span = MKCoordinateSpanMake(0.01, 0.01)
let region = MKCoordinateRegion(center: coordinate, span: span)
iosMap.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
iosMap.addAnnotation(annotation)
}
Try resetting your device's network configuration by going to >> Settings >> General >> Reset >> Reset Network Settings.
Or
import CoreTelephony in AppDelegate
Hope this helps.

Show several annotations in iOS Map

I am trying to show user's current location and users nearby the area.
I am doing to show user's
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude:location!.coordinate.latitude ,longitude : location!.coordinate.longitude )
let span = MKCoordinateSpanMake(0.01,0.01 )
let region = MKCoordinateRegion(center: center, span: span)
self.mapView.setRegion(region, animated: true)
self.manager.stopUpdatingLocation()
}
Plotting near by users
plotUsersonGraph(lat,longitude:long,title:"Apple HQ",subtitle:"Welcome to Apple")
func plotUsersonGraph(latitude:Double,longitude:Double,title:String,subtitle:String){
let HQlocation = CLLocationCoordinate2DMake(latitude, longitude)
let span = MKCoordinateSpanMake(0.01, 0.01)
let region = MKCoordinateRegion(center: HQlocation, span: span)
// self.mapView.setRegion(region, animated: true)**THIS IS CAUSING PROBLEM**
let annotation = MKPointAnnotation()
annotation.coordinate = HQlocation
annotation.title = title
annotation.subtitle = subtitle
self.mapView.addAnnotation(annotation)
}
I am able to view both the locations individually but I am unable to merge both of them together. plotUsersonGraph will add multiple users on the graph simultaneously.
This is how I achieved it
Added
self.mapView.showsUserLocation = true
inside
func plotUsersonGraph(latitude:Double,longitude:Double,title:String,subtitle:String){
}

How to Center a Map on User Location IOS 9 SWIFT

Hi I have the below code which is finding the user location the radius within 1 mile:
let initialLocation = CLLocation(latitude: 51.5001524, longitude: -0.1262362)
let regionRadius: CLLocationDistance = 1000
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
func centerMapOnLocation(location:CLLocation) {
let coordianteRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordianteRegion, animated: true)
}
// Do any additional setup after loading the view, typically from a nib.
centerMapOnLocation(initialLocation)
}
This is perfect as gets a decent radius for user location, however i want to know, how do you center map on user location?
i have tried a lot of methods but i do not seem to be able to get the map to center on user location.
Any help would be much appreciated!
Thanks
MKMapView has a built in solution by using the userTrackingMode property.
mapView.userTrackingMode = .follow
I think this might help you
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.map.setRegion(region, animated: true)
}

Resources