Need simple coding for the span to zoom on the users location when the button is pressed. It follows the user but need span coding help.
i use this atm
#IBAction func Refreshbutton(sender: AnyObject) {
//Navigationsknappen
Mapview.userTrackingMode = .Follow
self.Locationmanager.stopUpdatingLocation()
}
This code may help you.
let span = MKCoordinateSpanMake(0.075, 0.075)
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude:lat, longitude: long), span: span)
mapView.setRegion(region, animated: true)
My understanding is, Its not that simple -
You need to create a region - MKCoordinateRegion
Get long and lat using MKCoordinateSpan
example:
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
//Get the location you need the span zoom
let location = CLLocationCoordinate2D(
//getPropertyLocation.latitude/longitude already set to CLLocationDegrees in a prior process.
latitude: self.getPropertyLocation.latitude ,
longitude: self.getPropertyLocation.longitude
)
// Get the span that the mapView is set to by the user. "propertyMapView" is the MKMapView in this example.
let span = self.propertyMapView.region.span
// Setup the region based on the lat/lon of the property and retain the span that already exists.
let region = MKCoordinateRegion(center: location, span: span)
//Center the view with some animation.
mapView.setRegion(region, animated: true)
}
Related
I don't believe this question has been asked yet in Swift 3.0 - three goals:
Upon viewDidLoad the Map Centers to the User Location at a certain zoom level that can be set (for example let span: MKCoordinateSpan = MKCoordinateSpanMake(40.0, 40.0))
Once the map loads and centers on the User Location, the User can then move and scroll the map to any other location WITHOUT the map automatically snapping back to the original User Location
Allow the User to ONLY zoom in to a certain level but allow the User to zoom out fully to view the entire global map (no restrictions on the zoom out level)
Here is my code thus 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()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
1. Should work with the code you have now.
2. Add check for subsequent location updates
In the didUpdateLocations method, add a Bool to check whether the region was centered on the user already 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 the map will no longer center on the user after the first update, until you change regionHasBeenCentered back to false. This will allow the user to scroll and zoom freely.
3. Implement MKMapViewDelegate method to detect map region changes
Implement MKMapViewDelegate on your view controller so that you can check for region changes.
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
…and set the view controller as the delegate:
override func viewDidLoad() {
// other things…
mapView.delegate = self
}
Then implement the following method which will be called right before the region changes. Here you can check to see if the span's dimensions are too small, and set them to a 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: From the MKCoordinateSpan documentation, the longitudeDelta will change as you move toward/away from the equator.
longitudeDelta
The amount of east-to-west distance (measured in degrees) to display for the map region. The number of kilometers spanned by a longitude range varies based on the current latitude. For example, one degree of longitude spans a distance of approximately 111 kilometers (69 miles) at the equator but shrinks to 0 kilometers at the poles.
Furthermore, MKCoordinateSpan's dimensions are measured in degrees, and 40 degrees is quite a bit so you probably want to change these values, otherwise the user will not be able to zoom in much at all.
I'm having issues showing a map annotation within xCode. I am able to show a pin on the correct coordinates, however I cannot seem to figure out how to show an annotation with a title and subtitle.
I'm a newbie to xCode and Swift, so apologies if I use incorrect terminology (or if this is a really simple problem to solve!) I've been searching the web for about 2 hours now, trying different variations to my code, but I can't get it working!
I'm developing a simple app where users can browse locations and then see it pinned on a map. Once people click through from a location to the MapViewController, I'm trying to just show a map with the location pinned and an annotation with title and subtitle. I've figured everything out except for the title and subtitle, so I'd appreciate any help!
override func viewDidLoad() {
super.viewDidLoad()
// Location pin
let initialLocation = CLLocation(latitude: location.latitude, longitude: location.longitude)
self.centerMapOnLocation(location: initialLocation)
// Annotation
let annotation = MKPointAnnotation()
annotation.title = self.location.name
annotation.subtitle = self.location.type
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
mapView.addAnnotation(location)
}
// Map Center on Location
func centerMapOnLocation(location: CLLocation) {
let regionRadius: CLLocationDistance = 1000
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
I wonder if you should add annotation instead of location into the mapView, i.e.:
mapView.addAnnotation(annotation)
And if you want to show the callout programmatically, you can call like this:
mapView.selectAnnotation(annotation, animated: true)
Make sure your class conforms to the MGLMapViewDelegate and use the following method.
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}
I have a mapKit on a view controller. I want the map to show the users current location. it seems to work but the users location is stuck on the right hand side of the screen. when you try scrolling it away from the right it just pops back there. Cannot fathom what I am doing wrong? Any help would be appreciated.
override func viewDidLoad() {
super.viewDidLoad()
clientAddressTextField.text = clientAddress
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
Kind regards
Wayne
Ensure that you have set constraints for your map view, otherwise it will have some default size & location, which may well be offscreen
Use the mapView's delegate method mapView:didUpdateUserLocation:
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
mapView.setRegion(MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800), animated: true)
}
I have a problem with my MapKit in Swift. I want it to center on a specific coordinate, but the default center doesn't change at all. The first (and most important part of) my viewController.swift file looks like this:
import UIKit
import Parse
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var map : MKMapView! = MKMapView()
var locManager:CLLocationManager!
#IBOutlet var txtUsername: UITextField!
#IBOutlet var txtPassword: UITextField!
override func viewDidLoad() {
var centerCoordinate = CLLocationCoordinate2DMake(41.8796677, -87.6198131)
var mapSpan = MKCoordinateSpanMake(0.01, 0.01)
var mapRegion = MKCoordinateRegionMake(centerCoordinate, mapSpan)
self.map.setRegion(mapRegion, animated: true)
locManager = CLLocationManager()
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.requestAlwaysAuthorization()
map.showsUserLocation = true
super.viewDidLoad()
}
What could be the reason that the center of the map is not changed by the variable 'centerCoordinate'?
There are func's that are called automatically when the map is in use. One of them is
func mapViewDidFinishLoadingMap(mapView: MKMapView)
Try doing this.
func mapViewDidFinishLoadingMap(mapView: MKMapView) {
map.setRegion(mapRegion, animated: true)
}
If this doesn't work type func mapView and look in the list and find one you think might but I feel this is where it needs to be.
In ViewController.swift, find viewDidLoad and add the following to the end of the method:
// set initial location in Honolulu
let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)
You’ll use this to set the the starting coordinates of the map view to a point in Honolulu.
When you are trying to tell the map what to display, you can’t just give a latitude and longitude. That’s enough to center the map, but you need to specify the rectangular region to display to get a correct zoom level too.
Add the following constant and helper method to the class:
let regionRadius: CLLocationDistance = 1000
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
The location argument is the center point. The region will be have north-south and east-west spans based on a distance of regionRadius – you set this to 1000 meters (1 kilometer), which is a little more than half a mile. You then use regionRadius * 2.0 here, because that works well for plotting the public artwork data in the JSON file.
setRegion tells mapView to display the region. The map view automatically transitions the current view to the desired region with a neat zoom animation, with no extra code required!
Back in viewDidLoad, add the following line to the end of the method:
centerMapOnLocation(initialLocation)
This will call the helper method to zoom into initialLocation on startup.
Build and run the app, and now it should zoom in to the heart of Waikiki :]
Font: http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial
Swift 5.
In the MKMapViewDelegate, I recommend using the regionDidChangeAnimated method and setting the region of the mapView based on the user's coordinate. Change the span according to your use case.
extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
mapView.setRegion(MKCoordinateRegion(center: userLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)), animated: true)
}
}
I'm trying to find some code that will have the following effect. I currently have a map setup in my view, and a button simply named "Zoom Out". Upon clicking this button, I would like the MKCoordinateSpanMake to go from (0.008, 0.008) to (0.05, 0.05), for example.
Here is the code I have so far:
AboutMeViewController.swift
import MapKit
class AboutMeViewController: UIViewController, MKMapViewDelegate {
#IBOutlet weak var mapOfMySchool: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let location = CLLocationCoordinate2D(
latitude: XX.4890669,
longitude: -XX.6993226
)
let span = MKCoordinateSpanMake(0.008, 0.008)
let region = MKCoordinateRegion(center: location, span: span)
mapOfMySchool.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "School Name"
annotation.subtitle = "Subtitle"
mapOfMySchool.addAnnotation(annotation)
}
#IBAction func zoomInMap(sender: UIButton) {
// Zoom code here
}
}
Have you tried:
#IBAction func zoomInMap(sender: UIButton) {
let location = CLLocationCoordinate2D(
latitude: XX.4890669,
longitude: -XX.6993226
)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location, span: span)
mapOfMySchool.setRegion(region, animated: true)
}
I would also suggest changing the call in viewDidLoad to be unanimated since it won't be visible to the user anyway.