swift - update directions when user moves - ios

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.

Related

Detect tap on a label in Mapbox map on iOS

I'd like to know if there is a way to best detect a users tap on a label?
The new iOS15 Maps app allows a tap on e.g. a cities name and then shows informations about that city.
I am now wondering if something similar can be done with mapbox?
I know that there is a mapView.visibleFeatures(in: myRect) function that can somehow help here. So I can convert my finger location to a rect and then get all features there.
BUT... my city e.g. might have a label that is let's say 200 px wide. So I would need to have a quite large rect to find the point of my city label. And then I will also get all kinds of other labels that might be there. Maybe even not visible, but in the dataset.
Is there no way to ask the map what the frontmost element was when I tapped? So that when I tap on the far end of the label, I still get that ONE feature?
I am still using Mapbox V6.3... the latest before their last major update.
But if it's not possible with that version, an answer about the latest V10.something would also be great.
For v10, this example demonstrates how to identify features near a click. While the overall example is to a different end, the onMapClick functions shows the method to find a feature and then build an annotation.
https://docs.mapbox.com/ios/maps/examples/view-annotation-marker/

Apple Maps Location Marker like in Find Friends App

I have an app where I want the user to place a marker on a map and drag a circle to define a range around that notification, exactly as in Apple's Find Friends App (when you pick one of your friends and subscribe for notifications when he arrives or leaves a certain location).
Looks like this:
The marker can be dragged and moved like always, but the solid circle on the right side is used to define a range around the marker.
Does anyone have an idea on how to implement that? Maybe someone has already implemented something like this and wants to share his code :-).
Thanks.
Ok, I found some very useful sourcecode. Here it is, in case somebody else needs it as well: https://github.com/d0ping/DBMapSelectorViewController

Apple Watch Maps Animation

We are working on an app that requires an animation overtop of a map. this is a challenge because the map is a dynamic element and there are obvious limitations to the watch. Does anyone know a way to programatically take a snapshot of the mapview. I figure if we take the screenshot and then create an animation on the phone pushed to the watch this might solve this problem. Open to any other ideas. Thank you!
If you use something other than Apple's mapview you can draw the map yourself and then draw animations over it. Here is an example of where I do this Is it possible to position views on top of each other (the animations in this example transferred too slowly over bluetooth and so I turned them off for the final product). You could look at using something like MapBox (www.mapbox.com) to get map tiles and draw them.

Long-tap/drag/drop Skobbler skannotation to new position

I'm looking to allow my users to drag & drop annotations on my maps, initiated by a long-press on the annotation pin. I can't find anything in the documentation that would handle this.
Can someone point me in the right direction?
Edit: I realize I may not have been as clear as I thought I was.
Currently, my users are able to add new annotations to the map by long-pressing at a location on the map. I'd like for them to be able to move those annotations to a different location.
For adding a new PIN, the SKMapViewDelegate protocol is used to receive map-related update messages.
Check: http://developer.skobbler.com/docs/ios/2.4.0/Protocols/SKMapViewDelegate.html

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