CLLocationManager Singleton - is this the way to go? - ios

I'm building an app with two simple views (in a tabbar).
first view: it should show the user's location (default blue dot) and load data from a server.
second view: it should show the user's location (my custom pin with annotation and callout). the user can tap the callout and submit data about the current location.
I started by using MKMapView's showsUserLocation.
Then I read that it's better to use a CLLocationManager singleton instance, so I followed this blog post roughly: http://jinru.wordpress.com/2010/08/15/singletons-in-objective-c-an-example-of-cllocationmanager/
Now this pretty much works, but I'm wondering if it's the correct way to do things.
Also, I couldn't find a way to show the default blue dot instead of a custom pin. I read that I should use MKMapView's showsUserLocation, but wouldn't that create another instance of CLLocationManager?
Thanks

The method of creating a CLLocationManager singleton in the blog post you mentioned looks good. As for using showsUserLocation in combination with a CLLocationManager, it should be fine. Apple designed Core Location to be used by multiple applications at once. I won't cause too many issues.

Related

Swift 4 : How to redraw route if user change the direction in Google map ios

I'm working on an iOS App which draws the path on a map using Polyline options, between source to destination.
This works fine.
What I need is, if the user starts from source, and instead of taking drawn route towards the destination, the user takes another route, the path on the map should be redrawn for user's current path.
I'm using Directions API for drawing the path.
How can I do that?
Thanks in advance.
Yes, you can do that by detecting the change in location. Use didUpdateLocations method of CLLocationManagerDelegate Protocol to detect the change of location. If user location is changed significant then check that if user is still on route or not by using GMSGeometryContainsLocation function.

swift - update directions when user moves

I am writing an app where a user can select a location and then it will display the directions to it on the map. This functionality works fine, however I need the route to update on the map as the user moves. Currently I am using the didupdatelocations method and creating a new direction line from the new user location, however this results in the polyline being completely redrawn. I am trying to find a way to update the directions as the user moves similar to how apple maps/google maps does this. I do not want to use the navigation window if possible.
I am using the Apple mapKit.
i have looked for similar problems this one with google maps but there have been no replies.
I have seen another example (sorry cannot find link again) which makes an array of all the coordinates in the polyline although im not sure where i would use this or how to use this to slice or remove the unwanted part of the polyline.
At the moment my code does remove all overlays when a new direction is called, but if the overlays are not removed then I will end up with loads of lines on the map. I have tested removing the first polyline after the second polyline is created but that doesn't seem a very efficient way of dealing with this.
would appreciate any suggestions.

Creating Custom Map based on User Input

I'm trying to create a campus navigation app for my college, and in order to do so I need to make a custom map (or use Apple's, but I'd rather make my own), how exactly would I go about it? I had an idea to just use my location and walk around, forcing my phone to draw the map for me (using lines and such) but I wasn't sure how to go about it.
Is there anyway to create a map that updates automatically when new routes are added via anyone's phone? Almost like a collaborative map?

MapKit: How to create a custom annotation view with a button on a .xib file?

I'm using Xcode 5 and the latest iOS SDK and I'm having a really hard time figuring out how to create custom annotations with a clickable button.
All I need is a custom annotation with a button, that is shown when the user taps a pin on my MKMapView. Following some tutorials, I managed to create a custom class that loads a .xib file, this already works. However, the problem is, that whenever I try to click a button placed on the annotation view, the pin gets deselected. I would love to be able to design my annotation in a .xib file and not create it 100% programmatically. Is there any way of doing so?
Thanks,
Niclas
There is a great tutorial on maps on a site I used when I first got started into this section of iOS. It can be found here: http://www.raywenderlich.com/21365/introduction-to-mapkit-in-ios-6-tutorial ( Just an FYI, this is a great resource for learning more about iOS. )
Anyways, this paragraph should sound familiar (as to what you're after):
You’ve now made it so that in the callout when a pin is tapped on, there will be a button on the right hand side. When this is tapped, the mapView:annotationView:calloutAccessoryControlTapped: method is called. In this method, you grab the MyLocation object that this tap refers to and then launch the Maps app by calling the openInMapsWithLaunchOptions: method.
Just note that it is opening up the Maps app, but you can choose to do whatever you want really.
Later on, here are some other great links if you need them:
http://www.raywenderlich.com - This is where I started, recommend you do the same. Use their search box. They sell some privatized/self-published PDF books too if you like that.
http://www.cocoacontrols.com - You could probably find a control someone has already written with maps.. use the search, filter for iOS only.
http://www.nshipster.com - Great building block site
http://objc.io - Great site for getting into more low-level development.

MKMapView. create a path on the move

I am trying to create a line on the MKMapView after the users path.
I've found this rather old post, and i'm trying to make it work as explained in the accepted answer:
Does anyone have any examples on how to create a path using MKOverlayPathView?
What is don't get is how to "extract" the array of CLLocation objects mentioned. Right now i have set the mapView setShowsUserLocations:YES, and it updates my movement perfectly fine, so i guess i can get the CLLocations objects somehow, and continuously draw the line?
Im still very now to iOS, so be gentle.
Check out the source code of Apple's BreadCrumb sample app for an example of how to draw the user's path with an MKOverlayView. It does exactly what you are asking and it is quite efficient. It's an excellent example app for getting started with core location and mapkit in iOS.
The idea is to have an object conforming to the MKOverlay protocol that has a property for an array of coordinates that the user has logged. When the user logs a new position you will add a coordinate to this array. Your MKMapViewDelegate is then responsible for providing an MKOverlayView for your overlay. Your MKOverlayView class is then responsible for drawing lines in between the points in the array of the user's logged coordinates. This occurs in the MKOverlayView's drawMapRect:zoomScale:inContext method. Note, you will have to pay special consideration to the fact that drawMapRect:zoomScale:inContext will be called by multiple threads. If you don't acquire a lock on MKOverlay's array of coordinates when adding a new coordinate you will almost definitely experience crashes. See the Breadcrumb code for an efficient way to keep you drawMapRect method thread safe. A less efficient but much simpler alternative is to not access the MKOverlay's array directly from your MKOverlayView but instead pass a copy of it.
The map will show the location pin but it won't actually notify you as your location changes. You want to add your controller as a delegate of CLLocationManager and use startUpdatingLocation. You will then get updates to location that you can save and use to add overlays to the map.

Resources