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)
Related
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)
}
}
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.
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.
MKMapView
CoreLocation
Swift3.0
I have a requirement like this:
Creating an application in which I am getting Lat and Long for various location from webservice . Ploting these locations on map with Custom AnnotationView . Apart from this, I'm showing User Current Location with Custom Image.
Here everything working fine.
Now, when user moves then User Current Location Pin changes its position so there is an important requirement for this, I need to make this Current Location position in such a way that it stick in the bottom of map (just 100 pixel) above from bottom.
I did Some RND and find out few useful links:
Centering MKMapView on spot N-pixels below pin
How can I center my mapview so that the selected pin is not in the middle of the map but in 1/3 of it?
set current location icon lower side in MKMapView
But this not working anymore.
Kindly suggest how can I set user Current Location pin in iOS map in such a way that it always at the bottom of Map (100 pixel above from Bottom)
Required Pin position:
Currently it is showing in other position.
Note - **Dont want Google Map for such functionality.
If you have been using something like this mapView.setUserTrackingMode(.Follow, animated: true) then user location will be always displayed in the center of the map, zoom/pan will be automatically adjusted whenever user location is updated.
You need to disable the UserTrackingMode and then manually adjust the map zoom/pan using func setVisibleMapRect(_ mapRect: MKMapRect, edgePadding insets: UIEdgeInsets, animated animate: Bool) each time whenever user location is changed.
I used following code in similar situation
let annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate);
var zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 1, 1);
annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 1, 1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
mapView.setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(40, 50, 160, 50), animated: true)
p.s. edgePadding is your friend in this case.
Here is the trick to use to position the map in such a way that UserLocation pin gets adjusted automatically :
func setUserLocationOnLowerPositiononMap(coordinate: CLLocationCoordinate2D) {
var region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
region.center = coordinate
self.map.setRegion(region, animated: true)
//self.map.moveCenterByOffSet(offSet: CGPoint(x: 0, y: -130), coordinate: coordinate)
self.map.moveCenterByOffSet(offSet: CGPoint(x: 0, y: SCREEN_HEIGHT - SCREEN_HEIGHT * 4/3 + 0 ), coordinate: coordinate)
}
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?