New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Package not recognised #29
Comments
MKCoordinateRegion is not defined in this package but rather in MapKit - so you will need to put |
Thank you for the quick reply. It would be good to update the example code given in REEDME as this does not show importing Mapkit. |
Dear Paul,
Any ideas? Kind regards, ahartman, belgium |
Hey, sorry for responding so late.
For coordinate-fixed circles, you may want to use the following example: struct MapOverlayCircle: Identifiable {
let id = UUID()
let center: CLLocationCoordinate2D
let radius: CLLocationDistance
let strokeColor: Color
}
struct MyMapView: View {
@State private var overlays = [
MapOverlayCircle(...),
MapOverlayCircle(...),
]
@State private var region = MKCoordinateRegion()
var body: some View {
Map(
coordinateRegion: $region,
overlayItems: overlays
) { overlay in
MapCircle(center: overlay.center, radius: overlay.radius, strokeColor: overlay.strokeColor)
}
}
} This example will draw two circles on the map (the initializers would need to be filled instead of the ellipsis (...) of course).
For these cases, please make sure that MapZoomControl is only referenced in macOS code, for example using the following precompiler directives: Map(
coordinateRegion: $region,
annotationItems: mainModel.mapElementLines
) { mapLocation in
MapPin(coordinate: mapLocation.coordinate!, tint: mapLocation.color)}
)
.mapKey(1)
if os(macOS)
.overlay(alignment: .topLeading) {
MapZoomControl(key: 1)
}
#endif |
Paul, Better a later answer than no answer, thank you for getting back to me. The MapZoomControl is not visible with your code.
|
You can provide overlays and annotations in the same initializer of a map var body: some View {
Map(
coordinateRegion: $region,
annotationItems: model.mapPins,
annotationContent: { mapPin in
MapPin(coordinate: mapPin.coordinate, tint: mapPin.color)
},
overlayItems: overlays,
overlayContent: { overlay in
MapCircle(center: overlay.center, radius: overlay.radius, strokeColor: UIColor(overlay.strokeColor))
}
)
.mapKey(1)
#if os(macOS)
.overlay(alignment: .topLeading) {
MapZoomControl(key: 1)
}
#endif
} Does that work for you? 😊 |
Paul, Yes, that works. I tried the same thing but got weird errors. By comparing yours with mine, I found one single stupid typo! As for the MapZoomControl, I develop for Mac Catalyst, and I presume Catalyst does not qualify as MacOS. That is a pity but I can zoom with double-click and alt-double-click. Thanks for now and I follow you for updates in your package, good work. I would be very interested in a solution for clustering annotations; the examples that I find, are old and I cannot came these work. Regards, André Hartman |
There is unfortunately no such functionality from MapKit, you can however show the current zoom status by setting the var body: some View {
Map(
coordinateRegion: $region,
informationVisibility: .default.union(.scale), // You can also try other values such as `.all` to show all kinds of information
annotationItems: model.mapPins,
annotationContent: { mapPin in
MapPin(coordinate: mapPin.coordinate, tint: mapPin.color)
},
overlayItems: overlays,
overlayContent: { overlay in
MapCircle(center: overlay.center, radius: overlay.radius, strokeColor: UIColor(overlay.strokeColor))
}
)
.mapKey(1)
#if os(macOS)
.overlay(alignment: .topLeading) {
MapZoomControl(key: 1)
}
#endif
} Edit: Ah, I just noticed, that there should actually be support for macCatalyst - I will have a look into this - thank you for bringing it up! |
Paul, According to Apple documentation, there is something like Annotation Declustering, but it very dense for me. There are examples on Internet as well, but they are old and I can't transform these to SwiftUI. |
#32 should make MapZoomControl and MapPitchControl available for macCatalyst - have you found anything else that doesn't seem to be available for macCatalyst but actually should be? |
Yes, annotation clustering should be available, I agree, I just haven't found the time nor great ideas to implement it yet. If you happen to have an idea or suggestion, feel free to open another issue or pull request 😊 |
Thank you for that link - unfortunately, I cannot implement this right now, but I will try to find some time in the coming weeks. |
Paul, Please find my code below as as a screen image of the result.
I also have an earlier version the the map window with MapKit and UIViewRepresentable.
The image below shows the result and you can observe a marker in 'Arendonk' on the right hand side that is not in the previous image because that has a fixed scale. Is there a way to do that automatic scaling in your package as well? Regards, André Hartman |
Sure, okay, I have a couple (small) remarks:
Let me give you a helper method that might be of help: extension MKCoordinateRegion {
convenience init(containing coordinates: [CLLocationCoordinate2D], multiplyingSpanBy spanFactor: Double = 1) {
guard let minLatitude = coordinates.map(\.latitude).min(),
let maxLatitude = coordinates.map(\.latitude).max(),
let minLongitude = coordinates.map(\.longitude).min(),
let maxLongitude = coordinates.map(\.longitude).max() else {
self.init()
return
}
let latitudeDelta = (maxLatitude - minLatitude) * spanFactor // The spanFactor allows you to show a bit more than only the coordinates, e.g. put in `1.5` to see a bit more map than you would actually need to fit the coordinates
let longitudeDelta = (maxLongitude - minLongitude) * spanFactor
self.init(
center: CLLocationCoordinate2D(
latitude: minLatitude + (latitudeDelta / 2),
longitude: minLongitude + (longitudeDelta / 2)
),
span: MKCoordinateSpan(
latitudeDelta: latitudeDelta,
longitudeDelta: longitudeDelta
)
)
}
} Warning: This will only work, if the coordinates are not close to the Pacific (which I assume is the case here). An issue would arise, if you had one coordinate in California and one in Japan for example, since the average of their longitudes would be somewhere in Europe, which makes no sense. You would need to use trigonometric functions then to compute the average, which I skipped here. You can then set your region in an @State private var region = MKCoordinateRegion()
var body: some View {
Map(...)
.task { region = MKCoordinateRegion(containing: model.mapPins.map(\.coordinate), multiplyingSpanBy: 1.5) }
} If you need to have this rescaling on every change of the mapPins property, you can use the |
Paul, Thank you for the Helper, that works.
Your calculation:
My correction:
Image 1 is the Map window with spanFactor at 1.0. Regards, André Hartman |
Paul, Please find my code. below:
MapKit window
MapModel logic
|
Thank you for the correction of the code snippet - yes, there seems to have been an issue, but you seem to have found the cause correctly. On the other note, you seem to be setting the mapRegion at two different points in time. While the implementation using this library sets the region once the view is actually in the view hierarchy, your custom implementation sets it before the view enters the view hierarchy with a frame of In general, I can see that a lot of data is generated while calculating the |
Since your original question and hopefully all further questions are solved, I will close this thread. If you happen to have follow-up questions, feel free to reopen / add comments and for unrelated questions feel free to open new issues. |
Dear Paul,
I added your package 'Map' to my project as explained by Apple.
My project has a working map based on MapKit but I want to use extended facilities based on your package.
It is visible as a Package dependency as well as listed under'Frameworks, Libraries and embedded Content'.
However it seems not to be recognised by Xcode as I get 'Cannot find type 'MKCoordinateRegion' in scope and similar errors for MyLocation, MKDirections and UserTrackingMode.
Any ideas how to resolve this.
Kind regards, ahartman, belgium
The text was updated successfully, but these errors were encountered: