How to add compass or rotation to MapKit Map with SwiftUI - ios

I need to add Compass or rotation with Map. This is my code. I am using Custom marker. I know we can add compass with MKMapView. But I have added many features to the Map using Map class like custom View Markers, longPress pin drop etc. Therefore, moving to MKMapView would be like redoing everything from scratch again.
Map(coordinateRegion: $mapRegion , annotationItems: viewModel.locations) { location in
MapAnnotation(coordinate: location.coordinate) {
MarkerView(location: location)
}
}
Anybody can tell me how I can do compass rotation with Map.
Thanks

You may be able to set these properties on the underlying view via the Introspect framework. It does not handle Map() out of the box, so you would have to follow the section on implementing your own selector. As the project's Read Me states, there are some caveats:
Please note that this introspection method might break in future SwiftUI releases. Future implementations might not use the same hierarchy, or might not use UIKit elements that are being looked for. Though the library is unlikely to crash, the .introspect() method will not be called in those cases.
I have also found that you need to really be careful and heavily test which property you set (or update) when and where.

Related

How to keep an MKAnnotation View Image fixed on the center of the map as the user pans and moves the map around (Swift)?

using target 8.0 MapKit, I would like to define an MKAnnotation (such as a pin, or a custom one) that remains fixed on the center of the map view as the user moves the map around. Once the user stops moving the map, I would like to be able to read the new coordinates of the annotation. How can I do this? This in swift, thank you soo much
You can follow this link to accomplish this task.They are doing something very similar to your requirement. Its quite descriptive. Link

MapKit (iOS) -- Live MKAnnotation Motion

I'm designing an app using Swift for a food truck. I want to use MapKit to show a pin on the live location of the truck so one can look at the app to see it moving in realtime. I was able to get the server-side element set up using PHP/MySQL. Currently, the app makes a HTTP request every 3 seconds.
How should I go about animating the moving pin (or other image)? I tried a few methods already (if you have others, please suggest!):
I removed the pin and quickly added a new one in my HTTP function. While this works, I would prefer a smooth animation, not a flashing, jerky pin.
I subclassed MKMapViewDelgate like with a didAddAnnotationViews delegate that animates the pin's frame using UIView.animateWithDuration as suggested here. Animation only occurred when the map was initially loaded. Also, I have no idea how this would work with coordinates, since it involves frames.
I subclassed MKAnnotation with a modifiable var coordinate. In my HTTP function, I changed the coordinate. EDIT: This now works since I properly refreshed the mapView (thanks #Paulw11). However, I still have two main issues:
There's no linear animation, which I desire to better simulate real-time movement. How would I go about animating the pin's coordinates? Should I use UIView, like in method 2, a fast NSTimer, or something else?
Due to using the setCenterCoordinate function to forcibly refresh the map, the map cancels any current touches. How do I detect if there's any touches on the MapView so I can prevent the forced update?

Adjusting map view span to annotiation pins in Swift

I wonder if Swift supports any faster way to do that without implementing arythmetics. I guess adjusting the map span to let user see all the annotation pins is not a rare problem.
I found this: Zooming MKMapView to fit annotation pins?
but it's a bit complicated in objective-c and seems to be a vulnerable solution. Did swift developers find any other way(or method) to deal with it?
Thanks in advance
If you can target iOS 7 or above, you can use showAnnotations:animated, and pass in the map view's annotation property.

MapKit overlays to add floor plans to map

I am trying to add an overlay to iOS MapKit in order to show floor plans. However, I am unsure about the best way of doing this. If I add the floor plans by using an MKOverlay, will the floor plans be loaded lazily or do I need to find out which part of the map is being displayed and update overlays thereafter? I also looked at using MKTileOverlay, as it is using lazy loading, but I have the impression that it should be used for completely covering of the map and not only to add to the existing one. Is this correct?
Yes, you are right MKTileOverlay can cover whole map with tiles and Yes it is using lazy loading.
Use MKOverlay if you are not willing to replace native look and fill of map. You can also achieve lazy loading for MKOverlay too.
Note: MKTileOverlay will not remove your already existing MKAnnotations and MKOverlay.
MKOverlay is a protocol, not a class. You want MKTileOverlay as of iOS 7. Things are lazy loaded according to what portion of the map is currently shown.
You might find this useful as a reference on how the tiles are numbered and organized:
https://www.mapbox.com/foundations/how-web-maps-work/
As for covering Apple's map, this will happen by default, but you can also adjust the MKOverlayLevel used to place it between Apple's base map and labels layer, or you can use canReplaceMapContent = YES if you want to disable Apple's maps entirely.

how to create a variable radius in map view

I have a map view and I want to add a variable radius like in the find friends app, allowing the user to drag to make the radius smaller or bigger. How do I do this? Is there any prebuilt functionality that allows you to do this easily?
MKCircle is the class you are looking for. It conforms to the MKAnnotation protocol, so you can just use it like any other annotation. Use the MKCircleView class for the actual drawing of the circle (also check the available sample code).
It can be possible by just using a custom MKAnnotationView that shows a circle? That will maintain size as you zoom around.

Resources