Equivalent of GMSCoordinateBounds in MapKit - ios

After choosing a location, a pin is dropped in a mapView.
mapView.addAnnotation(annotation)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegionMake(placemark.coordinate, span)
mapView.setRegion(region, animated: true)
I want to zoom to the bounding region of the placemark using Apple's MapKit. i.e I want the span to depend on the bounding region of the place.
This is doable using Google Maps SDK,
but using Google's Places pod adds too much to the file size of the app (+ increases the app load time)
how to do This using MapKit and Swift?

Related

How do i restrict panning and restrict zooming out of a certain area of apple maps in MapKit?

I am currently making an app with a map that should focus on a certain location only. I would like the user to not be able to zoom out or pan out of this area so they can keep their focus on the image overlay that i have put over this area.
In order to get the app to start off from the location that i want and not some random map, I used a tutorial from Ray Wenderlich: https://www.raywenderlich.com/425-mapkit-tutorial-overlay-views
How would I acoomplish my task based on the code that is written in the tutorial above? I have completed the tutorial, so I am looking for help in adding any code and identifying where and what kind of code to put.
I found other tutorials on this topic unhelpful because they were for other map types like Google maps or MapBox. The apple website about MapKit and MaximumZ does not help me very much either.
I am a beginner in XCode and Swift, and have only had little bit of experience in Python previously. I was hoping limiting the zoom and user access to parts of the maps would be easier...
override func viewDidLoad() {
super.viewDidLoad()
let latDelta = park.overlayTopLeftCoordinate.latitude -
park.overlayBottomRightCoordinate.latitude
// Think of a span as a tv size, measure from one corner to another
let span = MKCoordinateSpanMake(fabs(latDelta), 0.0)
let region = MKCoordinateRegionMake(park.midCoordinate, span)
mapView.region = region
}
This is what I have so far for getting the app to startup on the location that I want, using a rectangle that bounds the area that I am looking to restrict the user to.
The MKMapView has a delegate MKMapViewDelegate. This protocol has a function called:
func mapViewDidChangeVisibleRegion(_:)
This method is called whenever the user scrolls or zooms the map. In this method you can specify the behavior of the map. You can, for instance set a specific region that you want the map to zoom into and specify the maximum level of zoom allowed.
In the function mapViewDidChangeVisibleRegion(_:) you can then check to what latitudeDelta and longitudeDelta the map can zoom into. If the delta's go below or above a certain level you can lock the zooming by setting the region with something like this:
func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
if mapView.region.span.latitudeDelta < 0.4 {
regionSpan = MKCoordinateSpan(latitudeDelta: 0.4, longitudeDelta: 0.5)
let mapRegion = MKCoordinateRegion(center: self.centerCoordinate, span: self.regionSpan)
self.trackMapView.setRegion(mapRegion, animated: true)
}
}

Change MKMapView's Zoom - Swift 4

I using Apple's MapKit and MKMapView to show a location on screen. The function I am using is:
func displayLocation() {
locationMap.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(userConnected.siteConnectedLat!, userConnected.siteConnectedLong!), span: MKCoordinateSpanMake(0.05, 0.05)), animated: true)
let locationPin = CLLocationCoordinate2D(latitude: userConnected.siteConnectedLat!, longitude: userConnected.siteConnectedLong!)
let annotation = MKPointAnnotation()
annotation.coordinate = locationPin
locationMap.addAnnotation(annotation)
locationMap.showAnnotations([annotation], animated: true)
}
Is is then called in the viewDidLoad method of the View Controller. Apple's documentation says that the span should change the zoom but even changing the span is putting minimum effect on the map. I want it to be zoomed out enough so that we can clearly see like 3-4 European countries, i.e, a significant amount of zoom-out.
This is what my map looks like in simulator:
This is how it looks like after I press enlarge:
You need to set the region of the map in viewDidAppear, not viewDidLoad. When viewDidLoad is called, the map view has just been loaded - the map hasn't been rendered yet, so you can't set its region.
Another thing to change is the span. (0.05, 0.05) seems too small a span to show 3-4 countries. You should try something bigger, like (5, 5). Remember that these numbers represent in degrees the width and height of the map region.

Trying to get 2 or more annotations visible in the same region in MapKit

I'm trying to get my MapKit to zoom to the centre 2 or more points on the map and have them all visible. I managed to successfully get the centre correctly but I can't figure out how to get to fit them all into my region.
This is what I have so far
let center = middlePointOfListMarkers(listCoords: [location.coordinate,location2.coordinate)
center.latitude
let region = MKCoordinateRegionMake(center, MKCoordinateSpan(latitudeDelta: 0.025, longitudeDelta: 0.025))
self.mapView.setRegion(region, animated: true)
I'm trying to use MKCoordinateRegionMake but there might be an easier way to do this. Please let me know if you guys have any ideas.

How do I achieve the maximum zoom level in iOS mapKit?

I'm aware of this post: How to get to the max zoomlevel on iOS MKMapView
And it seems to be exactly what I need to solve the problem, however, the technique does not work for me.
If I set the eyeAltitude at 50, or even 100, I get the same map zoom returned. However, once the map is displayed, I can pinch to zoom to the desired zoom level. is there a solution to programmatically achieve the pinched in zoom level at the initial load of the map view?
EDIT: I've tried using setRegion but the problem seems to be related to using .satelliteFlyover specifically. I need to use .satelliteFlyover due to the difference in image quality.
Further EDIT: I've also discovered that the simulator seems to give a different 'native zoom' than when it runs on my device. I hope someone reads this that can provide insight!!
the code I'm using is:
let greenLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(CLLocationDegrees(greenX),CLLocationDegrees(greenY))
let altitude:CLLocationDistance = 80
self.greenMap.mapType = .satelliteFlyover
let camera = MKMapCamera(lookingAtCenter: greenLocation, fromEyeCoordinate: greenLocation, eyeAltitude: altitude)
self.greenMap.setCamera(camera, animated: true)
You can set region to make map view scale to fit. Use this function.
open func setRegion(_ region: MKCoordinateRegion, animated: Bool)
Here is the sample code.
let map = MKMapView()
let center = CLLocationCoordinate2D(latitude: 115, longitude: 24)
let span = MKCoordinateSpan(latitudeDelta: 0.003, longitudeDelta: 0.003)
let region = MKCoordinateRegion(center: center, span: span)
map.setRegion(region, animated: false)
I solved this by... using a different SDK for mapping. What I found through continued testing is that appleMaps doesn't always have the imagery down to the specific zoom level, and seems to get tripped up when you zoom in ultra close. on some locations it would zoom perfectly, and others it would not - an inconsistency that as a developer you may want to be weary of. googleMap had higher quality imagery and no issues with zoom consistency.

Keep user location center in Google Maps - Swift

I have implemented Google maps in to a swift project and using locationManager to track and currently print out the users location.
I am also changing the camera position as the user moves around however this seems a bit jerky as the map moves then the blue dot (user location) moves.
Is there a way to keep the user location centered at all times and just have the map move?
you can easily enable
mapView.myLocationEnabled = true
and see this answer for more info to camera updating -> as it shows how to update camera with 2 positions or center a location
Plaese, see if this answer helps you. The user location is centered all the time in the example code. There is no camera adustment though, but that shouldn't make a difference.
Hope it helps.
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