How to Show User's Location - Swift 4.2 - Xcode 10
HTML-код
- Опубликовано: 18 сен 2024
- This is part 1 of a 3 part series on MapKit. In this first tutorial we'll walk through getting location permissions, showing the users location on a MapKit view, and updating that user's location as they move. This tutorial is in Xcode 10 (beta) and Swift 4.2.
Part 2 will be about reverse geocoding an address and part 3 will be getting directions from the users location to that address.
Source Code:
www.dropbox.co...
iOS Dev Courses:
seanallen.teac...
Twitter:
Sean Allen - / seanallen_dev
Hired.com:
hired.com/x/1n01g
Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
Paul Hudson's Hacking With Swift:
gumroad.com/a/...
Donny Wals - Combine:
gumroad.com/a/...
Mark Moeyken’s SwiftUI Books:
www.bigmountainstudio.com/swiftui-views-book/fzc51
Objc.io Books (Thinking in SwiftUI & Advanced Swift):
gumroad.com/a/...
Ray Wenderlich Books:
store.raywende...
#swift #softwaredeveloper #iosdeveloper
Watch Next - iOS Take Home Project - Job Interview Practice - Free Preview - ruclips.net/video/MSIe2y6Fee8/видео.html
I have recently got a MacBook Pro for iOS development and I was worried about a good tutorial. But now that I found your videos, I'm so happy that I can learn what I need painlessly.
Thanks for your awesome contents! ❤✌👍
Happy you like it!
Amazing tutorial! 2 years after and I'm back here to learn how to use the mapkit for an app I'm working in. Thank you Sean! :D
It's the gift that keeps on giving 😀
The best and fastest man to teach the ios
Thanks for the kind words 👍
I’ve been wondering for the past 3 days about learning more about MapKit and here you are with a video!!! 😭
It's fate.
extremely simple and not overly complicated, thank you, good sir. I have been looking for someone to explain it like you have and man you saved me hours!
Happy to help, Jay!
Thanks for creating the tutorial! I’m a beginner, so I’m not ready for that level tutorial yet. But it does give me a sense of how things are done with more complex programming in my future.
Have you considered creating a low cost course for beginners to teach them the best way to do iOS programming in your opinion? If you already have a low cost beginner swift programming course, please let me know whee to find it.
You have an interesting life story about how you invested in yourself to learn iOS programming to improve your future. You not only improved your own future, but now you have the ability to improve other people’s futures, by sharing your knowledge!
Happy to hear you found some value in the video, even though you weren't quite ready for it yet. I will be releasing a course in 2019, so stay tuned. Working on it after the holidays.
Right when I needed to learn about this API to make a project to show off for an internship, you posted this. Thanks a ton man.
Great timing! Happy to hear it helped 👍
I've seen a lot of tutorials on iOS but Sean is the best one out there. Thank you!
Thanks Firzok! Glad you liked it 😀
Thanks for great tutorial. I am trying to restart my coding at age of 45.. This is great encouragement for my professional hobby..
That's awesome to hear. I didn't start my coding career till i was 33. Best of luck!
Very interesting Sean. Thanks for a comprehensive explanation.
I am very very new to this and to see functions and rules being explained so clearly and with some common sence, syntax explanations made understanding Swift protocols so much easier to grasp.
Happy to help Steve! Glad you enjoy my teaching style. 😀
Love the way you explain it. Will watch more of your videos for sure!
Glad you liked it!
I would like to say that was a Very informal lesson. I was fully engaged in the lesson and walked away with all the necessary information plus Some. Keep up the awesome work!
Glad you liked it, Mar! I have a bunch more videos that are very similar 😀
Dude you are the bomb! Thank you so much for the knowledge!! Better than any university class I could take!! The learning never ends.
Happy to help, Kyle!
Just awesome as always, excited for next parts to come
Thanks Shankar👍. Stay tuned.
Short and sweet. Thanks Sean!!! This was a good refresher and some new ways (for me) of doing some stuff (checkLocationAuthorization). Can I request that you include searching for nearby places (cafe, restaurant, shops, etc) in the 2nd or 3rd part of the series? Thank you.
I've already got most of parts 2 and 3 built out. Maybe that could be a part 4
Absolutely Brilliant! Great pace, no lost time, very thorough with code that works! Thank you!
Great explanation as always Sean. Its been a long time since a tutorial.
Thanks Omair. Trying to get back to doing more tutorials. Just gotta find the time.
Hi there, great tutorial but I have a question. When I simulate my device it does not ask me to enable location services. I get no pop ups like you did. Any idea why? Thank you.
I'm getting the same issue, I don't know why. Were you able to figure it out?
I got this because missed "case .notDetermined:". When I put there "locationManager.requestWhenInUseAuthorization()" - popup appeared.
I really enjoy your videos!!! You are truly one of the best!!! Thanks!
Thanks for the kind words, Shawn. I appreciate that!
Great explanation Sean. Keep doing this great job of providing useful content for all of us. You are the man...
Glad to hear you got value from the tutorial. More on the way 👍
hello, I have a question as I get this message 'authorizationStatus()' was deprecated in iOS 14.0 what should I do?
Hey whoever is reading this have a great day and keep being beautiful and inspiring
super helpful. Awesome tutorial man. Please make a video of integrating google maps into iOS apps too.
Glad you enjoyed it, Sachin!
BROOOOOOOO THIS VIDEO IS 🔥 🔥 🔥 your explanation is so thorough thank you for these videos. Please keep up the great work sir!!!!!
Glad you liked it, Brian!
Sean do you only specialize in developing using swift and Xcode only? Do you develop in other languages too? Do you do freelance work?
I specialize in only Xcode and Swift. I used to do freelance work but have since stopped to build my own business and producst.
Hey Sean, I dig this video. By far the most helpful i've come across. I am having a problem with the app initializing the "checkLocationServices()" func. It worked once and show my location perfectly. Then it stopped when i uploaded to my personal device. Ive re ran the code and even created other files and used your source code directly and now ive exceeded the amount of app certificates i can make in seven days. I am green to IOS and would really like to get this down. I think xcode is the future of IDE's, If theres any advice you have I would appreciate it alot and take it seriously. If not, thanks for making a legit and clear tutorial. Also your personality is good for this, i usually feel like im falling asleep during coding tutorials.
Glad you enjoyed the tutorial 👍. I've never come across this issue, so I'm not sure. I'd have to dig into the project and do some debugging to see what's going on.
I’ll message you privately through your link in the comments. I’m in Bali till January and recently lost my wallet on the bart so it might be a while before I have a way to pay online for anything and I may figure it out before then. I’ve been using western union to counter the problem while I’m out here since it happened right before I left. Thanks for the reply. One more question tho, do you know any solid ways to find good mentors?
For mentors, I'd start following a bunch of iOS developers on Twitter and start interacting. Once you find someone you enjoy, maybe ask them? I did a video about the importance of networking on Twitter as a developer here: ruclips.net/video/QH79tyspt-w/видео.html
Thank you, @@seanallen. I also debugged and found the problem. I had my method written too far down. It was past the object initialization.
Better code for 11:39. let region = MKCoordinateRegion.init(center: location, span: MKCoordinateSpan.init(latitudeDelta: 10000, longitudeDelta: 10000))
minute 16:58 what did you do in line 77?
If you get the message 'authorizationStatus()' was deprecated in iOS 14.0 for me it worked to write:
let manager = CLLocationManager
switch manager.authorizationStatus
For me the same
It does not work for me. Am I doing it wrong? I've been looking for an hour!
func checkLocationAuthorization() {
let manager = CLLocationManager
switch manager.authorizationStatus {
case .authorizedWhenInUse:
// do map
break
case .denied:
// show how to turn it on
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
// show them they cant
break
case .authorizedAlways:
// do map
break
Amazing explanation and concise, definitely looking for new content!
Thanks for the kind words... more in the way...
BTW: I love the wrap-up part!
Thanks Qasa 👍
13 dislikes from Android guys. Brilliant tutorial, you should be a teacher.
Thanks for the great video! A quick question: for some reason, my zoom just doesn't work, and I have the exact same code you used in the video. I'm using xCode 11 Beta. Do you have any idea of what the problem might be? Thank you!
Thanks for the great video! But are your locationmanager-delegate methodes invoked when changing the location? Because mine weren't until I added the locationManager.startUpdatingLocation() call in the locationManager-setup.
what do u mean?
Thank you! I was looking for this answer.
Nice tutorial with proper explanation, when you are uploading the rest 2 videos.
Thanks. Part 2 was released a couple days ago: ruclips.net/video/2wxE8byc2FQ/видео.html
Part 3 will be released later this week.
Thanks you much for nice tutorial, please keep it up. It is really helpful.
Great explanation thank you wish new ones are coming
Hi
Im having an error and from what i can see i haven't missed anything from your video.
in both functions (locationManager and centreViewOnUserLocation) its with this line: let region = MKCoordinateRegion.init(center: center, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
i get the error on both lines saying: Cannot invoke 'MKCoordinateRegion.Type.init' with an argument list of type '(center: CLLocationCoordinate2D, latitudinalMeters: Double, longitudinalMeters: Double)'
and the "init" part is highlighted.
Many Thanks
That is because the .init version of MKCoordinateRegion has been updated in Swift 4.2. This tutorial was made with Xcode 10 and Swift 4.2 (which will be released in a couple weeks). If you're still using Xcode 9 and Swift 4.1, start typing MKCoordinateRegionMake and you should get an auto complete for a function that takes in distance using meters. Let me know if that works.
Hi
I see that explains a lot. I found an alternative way around it, i did this:
let span = MKCoordinateSpanMake(0.01, 0.01)
let region = MKCoordinateRegion(center: center, span: span)
achieved the same result.
Many Thanks for the response.
let region = MKCoordinateRegionMakeWithDistance(location, 10000, 10000)
it worked
Nice detailed tutorial! One of a kind!
hi i used your source code files, and was unable to get my location on my computer when running the app on the simulator. Do you know how i would be able to hsow my own location? thanks
Try running the app on your device and that should work. The simulator defaults to Apple HQ
This is awesome! Curious...for the didUpdateLocations part...I'm curious..is there a way to call the centerViewOnUserLocation function here so that whenever a user moves/their location updates, we reuse the code we already wrote/call our own custom function to re-center the view on the new user location? Rather than rewriting the region = MKCoordinateRegion.init line, and a new version of the center/location declaration as well. Or maybe I'm misunderstanding this, and the didUpdateLocations built-in function actually powers the centerViewOnUserLocation function that we built because it sets the locationManager.location property that we call in centerViewOnUserLocation?
Great tutorial! Got a question though, when I change the location in the simulator to Freeway Drive the map doesn't follow the user's location blue dot. I've double checked the code and it's the same as your video, but its different to the code you uploaded which is missing "let center..." from the extension. What am I doing wrong? Thanks
First time seen your videos. Great clear explanations.
Thanks Steven. Happy to have you on the channel!
Concise and straight to the point, well done. Does core location also provide sharing all users location like Waze or Find My Friends?
Glad you liked it, James. I'm not sure... I've never implemented that functionality.
Hey @Sean Allen ! I got awesome learning through this tutorial. Even though it was fast, I could match with it and understood well.
Glad to hear you enjoyed it 😀
Hi @SeanAllen, Great tutorial. And thank you for making this one. At 1.20, you were setting the MapView 'delegate'? What is a delegate? And why do you need to set it to the ViewController?
Delegates are quite a large topic. I did a video talking all about them here: ruclips.net/video/DBWu6TnhLeY/видео.html. Hopefully that helps.
Hey when I run it the simulator just says stuck in the white screen and this error comes up MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.Any help or suggestion?
Never seen that before... I have a link to download the source code in the description. Have you tried downloading that and running it? And then compare my code to yours?
Thank you, Sean. That really helped with my little student project.
Hello!
I am not able to see the blue dot even after adding that single line code. please help me through this. thanks
Did you do all the permission stuff in the code? I have my source code linked in the description. Have you downloaded that and checked against my source code?
@@seanallen Ya same here. I ran it on my phone and it worked so it must be a bug with the simulators.
@@seanallen After a little debugging, here's what I found. When run on the simulator, the location doesn't pass the if let inside the method centerViewOnUserLocation. However, when run on my personal device, the location successfully gets unwrapped which is why it works on an actual device.
Thanks for the heads up. Haven't looked at that code in a while.
great tutorial i like your style in coding keep going sean
Thanks Ahmed!
Really informative, high-quality stuff! Thank you, Sean!! :-)
Happy to help, Oleg 👍
you been very helpful thanks a lot - but when I run the app the map dosnt move even when I change the meters size
thanks a lot Sean, your style is really exciting and clear for us learners. Can I ask you is there a possibility to change the device's current location programmatically without clicking debug>location>custom location? maybe with a button?
Running your source code through my simulator shows me a location somewhere in San Francisco while I am in Germany. The console says:
User-Location[15072:589126] libMobileGestalt MobileGestalt.c:1647: Could not retrieve region info
Using XCode 11.2
Because it's default location.
For you'r exact location run the application into you'r device.
Love all your vids man!
Thanks Cavin 👍. Much appreciated.
authorisation Status was depreciated in iOS 14
Great video! I am wondering how you can get a user's location and share it with other devices so that other people can see your location in real time. Thank you in advance.
Hello Sean,
the app only runs on my iPhone device. If I run the app on the simulator then the location poin is missing.
Great video.Mapkit is not a good choice for ride sharing apps or location services becoz Mapkit have limited services .in some countries it do not show directions place info and many more .
Interesting point, Muhammad. Thanks for sharing.
the location isn't showing up, why? I clicked the Users location on map view and I did the code. before I pressed button it wasn't working with the code, can you help please
Nice video! But what is an extension and why should we use them? 🙂
Thanks for explaining. That was very helpful!
Glad you found it helpful, Charly!
Great tutorial! Though, am I right that if you'd want your app to be approved by the AppStore, you would need your plist to contain all these three privacy rules? Location Always Usage Description; Location When In Use Usage Description; Location Always and When In Use Usage Description. I understand you show here just a basic functionality, so this is just a note for others.
I'm not sure off the top of my head. I'd have to look into it. I know the current app I'm working on only using the "When in Use" one and it's approved on Test Flight. I know that's not the same as the App Store, but just a data point.
Hey Mate, So i tried changing my location but the blue dot has gone grey and doesn't blink anymore. Is there something I'm missing ??
Great job man, thanks for these videos. I really appreciate all the effort. I was wondering how many requests per day can we perform in MapKit?
I'm not sure on the limit to be honest.
blue dot is not appearing in my simulator i have tried your approach of first video and also tried your attached code
Every time I move the map it always jumps back to the user's location after a couple seconds. How would I change the code so when I move the map it stays where I put it? I gather it has something to do with didUpdateLocations.
mine always says Thread 1: "[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mapView."
What an amazing tutorial. Thank you so much!
Hi Sean! I have some problem in MapKit part 1. I did everything right in code, but there is not showing user location. And show this error in console( did fail with error: Error Domain=kCLErrorDomain Code=0"(null)"). Can you help with this?
Very helpful video, thank you for your awesome work. But I have question about requesting location services: I need to know user's current location in two view controllers. What would be the best approach? currently I am checking for location services in both of them, but I think it is not good solution. Any advice? Thanks
You only need to request permission once. If you're using location in many screens throughout your app, then it might be a good idea to create a singleton that handles your locationManager. I'm not sure if I'd do that for only two screens tho.
I'm about 10 minutes when you run the app from this point I have my code exactly as you do minus
switch CLL part xbode tells me "switch covers unknown cases" and recommends I put
" @unknown default:
break
"
It builds fine with or without but when I try and run I get
"Build succeeded" followed by an open to
Thread 1
class AppDelegate: UIResponder, UIApplicationDelegate {
And the error is "=Thread 1: signal SIGABART"
It won't run. I even used your code still get the issue
21 main
at 6:07 you make a switch statement with CLLocationManager. Why use that instead of the reference created up top 'let locationManager = CLLocationManager()'
Im not sure what's happing with my Xcode, i have the latest version, doesn't recognise MapScreen, nor can find other of your typos? well confused animi using swift...... help?
I'm having trouble with this tutorial and it's something deeper in my simulator and I"m asking for a little help. Can/would you be willing to look at it via dropbox?
Out of interest can you have the map store information. For example if a business wants to show their location to other users, the map would have that location marked for everyone else to see with maybe a small image
"MobileGestalt.c:1647: Could not retrieve region info"
This is the error I am getting, Can someone please help?
Thank you! Is there a way to get (the coordinates of) a new location that is some distance down a street from an initial location with the Google Maps SDK for iOS? For example, if I want to get the coordinates of a location 20 feet down the same street from some other location. Something like "newLocation = location.translateUpStreet(20)". Some things I've already tried:
Adjusting (incrementing/decrementing between markers) the longitude or latitude doesn't guarantee that the marker is visible from the street view.
-Making a formula to increment/decrement the address number doesn't seem like it will work either, because not all streets have a regular address number pattern.
Thank you so much for any advice
Where did that MapScreen.swift file come from? was it just a matter of changing the name and class in the regular view controller?
I create a new file. File -> New -> Then named it MapScreen, and created my view controller in there.
There is a link to the source code that you can download in the description.
Super useful! thank you for the clear explanations!
Hey @Sean Allen ,
I'm using the same code as you but in my app, it is not showing my user location. I'm in South India. I also tried in Storyboard also.
Thanks Sean!
Glad you liked it!
thanks for creating tutorial it helped me a lot
Happy to hear it helped!
Hey Sean, thanks for the video....I am in Paris but when i run the program i have the same localisation as you in san francisco i don't know why...can you help me please ?
If you're running on the simulator, you have to change your location in the simulator settings. If you're running on an actual device, you shouldn't have this issue.
Hey there, pretty new to coding and I followed you’re instructions(which are fantastic by the way) but when I run the simulator im not getting my correct location. its putting me in some other country(can't tell because its zoomed in) without the blue dot. Not sure what's going on but would very much appreciate it if someone responded. Thanks in advance!
Thank you so much so helpful! but I have a question. Why my location is in San Francisco.....?
Happy to help, Danny. If you are using the simulator, that defaults to SF. Try running it on your phone.
@@seanallen Thank you so much!!!!
im getting really annoyed with Xcode is there an update that old auto code isn't carried across, i have done every single step 5 times over and still not getting what you have?
Have you tried downloading the source code that's linked in the description and see if that works? Then compare your code to mine?
i have even copied your code exactly the same, also my app won't ask for its locations built in the privacy (info.plist) im not sure why i won't ask me to allow location in the app? what other settings does it need? @@seanallen
@@seanallen i have even copied your code exactly the same, also my app won't ask for its locations built in the privacy (info.plist) im not sure why i won't ask me to allow location in the app? what other settings does it need?
REPLY
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation). when should i use this function and may I know what this function does/when it invokes??
Mine doesn't request when the app is launched and also prints that the info isn't set in info.plist when I have set it. Any ideas?
Ignore me. Stupid mistake. Thank you for the video!
How come your class name was Map Screen automatically. Mine is View controller
That's what I named it. It wasn't here automatically.
@@seanallen Didn't catch that. But thanks for the response appreciate it. How can I get my actual location to show rather than San Francisco
Sean Allen I was wondering the same thing but when I change it I get an issue where I get the Thread 1 SIGABRT thing and I really don’t know why.
Hey Sean thanks man! When I try to pan around the map, it always jumps back to the user location region. How do I allow user location updates while able to pan around the map ? Thanks
Thanks for the nice video! However I think you did some extra work you didn't need to do by manually making the map follow the user. Why not just do this:
self.mapView.userTrackingMode = MKUserTrackingModeFollow
I'm showing Objective-C, surely it could be done in Swift. I think the movements of the map will be smoother.
Thanks for pointing that out. I'll look into that!
let region = MKCoordinateRegion.init(center: center, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
This line of code shows me an error that says " Cannot invoke 'MKCoordinateRegion.Type.init' with an argument list of type'(center: CLLocationCordinate2D, latitudinalMeters: Double, longitudinalMeters: Double)' "
does anyone know why I would get this error and how to fix it?
libMobileGestalt MobileGestalt.c:1647: Could not retrieve region info
Is your map screen extension in your class definition or outside of it? I seem to get an error either way.
I get error at line 77, how can I resolve this?
Cannot convert value of type 'CLLocation' to expected argument type 'CLLocationCoordinate2D'
I use Xcode 10.3
MKCoordinateRegion.init(center: location, span: MKCoordinateSpan.init(latitudeDelta: 10000, longitudeDelta: 10000))
Good job! thanks for this video.
inside case :auth in use
2018-08-28 11:40:14.075995+0530 MapKitDemo1[13634:5415630] [VKDefault] Tile 11.8.4 (256) in current unloaded state for 0.01 seconds - Polygons - Loading (ephemeral) (0.01 sec), Rivers - Loading (ephemeral) (0.01 sec), Roads - Loading (ephemeral) (0.01 sec), Point Labels - Loading (ephemeral) (0.01 sec), Polygon Labels - Loading (ephemeral) (0.01 sec), Road Labels - Loading (ephemeral) (0.01 sec), Resources - Unavailable (terminal) (0.34 sec), Attributes - Loading (ephemeral) (0.01 sec)
i did'nt get the blue dot in simulator
I haven't seen that error. Are you sure you did the permissions correctly? Did you download my source code (in the description) and try that?
jpst.it/1n9lr
Sir have a look in my code
Hey Sean, is there way to get the Users current location to show in the nav bar? Like say I'm say im a certain area in Florida and I want that once the apps loads it shows the area on the map and also in the Nav bar at top? I've searched but couldn't find anything.
Check out the next video in my MapKit Series about reverse geocoding location. You could get the City, State from the latitude and longitude, and then display that information wherever you like.
Thanks Sean. I’ll check it out now
Man uh r the savior
Awesome
Just wanted to know if we want other user location on our map what should be the best practice
Happy to help! I'm not sure about using another users location. You'd have to have that on a server somewhere.
Hi i want to build messaging app with location but dont know how to select the location. Help ?
It is incredible to see the "war" between the storyboard haters and the rest of humanity. It is good to see people that respect other one's opinion
Storyboards are cancer. No one in actual industry uses them.