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.
Related
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.
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.
Do any of you know a way in Xcode where I can save the coordinates of an annotation every time a person puts an annotation pin on the map.
Then after I save this annotation, when a person clicks a button. The map shows all of the annotations ever saved and shows their location on the map.
I think this is the most difficult problem in IOS because I don't see any tutorials or sample code. I don't think we know how to do this. Any geniuses??
Thanks
The way to do is,store annotation coordinates somewhere in the app.
You can store annotation coordinates in local xml/plist file or any another saving flat file saving mechanism and load them on fly. The other option is to use iOS CoreData (sqlite data store). Once you have them saved some where its all mater of reloading and using them..
I personally prefer coredata since it gives bit more flexibility than flat files..
I just created a map view where the user can make an annotation by dropping a pin. How is it possible to save the annotation, so the user can see it when the app is closed and re-opened? Does anyone know a good tutorial for saving map annotations? Thanks!
Map annotations are nothing different to regular data. The answer to this will depend on many things. For example:
How do you save other data in your map?
How many pins do you need to keep track of?
If you are only saving one pin and thus one lat and one long value, you could use NSUserDefaults. There are lots of tutorials for that around. Here's just one example: http://iphonedevsdk.com/forum/iphone-sdk-tutorials/106311-tutorial-1-how-to-use-nsuserdefault.html
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.