Display map or satellite imagery from your app's interface, call out points of interest, and determine placemark information for map coordinates using MapKit.

MapKit Documentation

Posts under MapKit tag

124 Posts
Sort by:
Post not yet marked as solved
1 Replies
221 Views
The new ImageRenderer in SwiftUI is really great and should help me to remove some custom code I've had to write in the past. I've noticed that when used to capture a view that contains a map view, the map is replaced with a 'no entry' sign 🚫 Is this intentional? Here's some sample code to replicate the issue: https://gist.github.com/shaundon/282cf7ff276093681a1c246ae318c9d4
Posted
by shaundon.
Last updated
.
Post marked as solved
1 Replies
205 Views
Hi, I want to set the glyphImage property for an annotation view (MKMarkerAnnotationView) of a cluster annotation (MKClusterAnnotation). Using the below code, inside the marker I get, instead of the glyphImage, the standard cluster number. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if let cluster = annotation as? MKClusterAnnotation { var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "GroupMapClusterAnnotation") if annotationView == nil { annotationView = MKMarkerAnnotationView(annotation: cluster, reuseIdentifier: "GroupMapClusterAnnotation") } (annotationView as? MKMarkerAnnotationView)?.glyphImage = UIImage(systemName: "star.fill") return annotationView } } I think that the problem is that if you keep the glyphText property empty, MapKit automatically set it with the number of the cluster member annotations. The documentation for glyphImage says: Use this property or the glyphText property to specify the marker balloon content. If you specify both an image and text, MapKit displays the text. So, MapKit is setting glyphText with a value an therefore the glyphImage property is ignored. How can we use the glyphImage property for a cluster annotation? Thank you
Posted
by DaleOne.
Last updated
.
Post not yet marked as solved
2 Replies
262 Views
There appears to be an issue with satellite map tile appearing as blank/black when using Mapkit JS on the web. See example from duckduckgo which uses Mapkit JS below (switch to satellite view to see blank tiles):- https://duckduckgo.com/?q=CH44+5UP&t=h_&ia=maps&iaxm=maps I have the same issue when using Mapkit JS. It is only a recent issue but appears widely across the UK (not sure of other countries). Does anyone know, how to raise this as an issue to the Apple Maps or Mapkit JS people (I can't seem to see a support email address or similar)? Thanks in advance.
Posted
by gazzatb.
Last updated
.
Post not yet marked as solved
1 Replies
201 Views
I am trying to detect wether two MKOverlays (might be circles or polygons) intersect. Tried using boundingMapRect but it draws a rectangle around my overlay thus giving me inaccurate results. if polygon != nil { intersectionOverlay = polygon! } else { intersectionOverlay = circle! } for overlay: MKOverlay in mapView.overlays { let rect = overlay.boundingMapRect if rect.intersects(intersectionOverlay.boundingMapRect) { print("Intersects \(overlay.title)") } Using that piece of code, would return true for the situation in the images below. Is there any other better way to achieve the desired results? Thanks
Posted Last updated
.
Post not yet marked as solved
0 Replies
178 Views
Hi, I need to generate the image of a map at a specific resolution. I tried to use the following code: let options = MKMapSnapshotter.Options() options.size = CGSize(width: 300, height: 200) options.scale = 1.0 options.mapRect = mapRect let mapSnapshotter = MKMapSnapshotter(options: options) if let snapshot = try? await mapSnapshotter.start() { let image = snapshot.image } Since I specified the options.scale to be 1 I expect the image resolution to be exactly 300x200 pixel (and with a scale of 1). Unfortunately the resulting image has a scale factor of 3 (on an iPhone) and the actual resolution is 900x600 pixel. I also tried, instead of using the scale option, to set the traitCollection option, like this: options.traitCollection = UITraitCollection(displayScale: 1.0) but I still get a 3x scale image with a resolution of 900x600. Why is the scale option ignored? I miss something? Thank you
Posted
by DaleOne.
Last updated
.
Post not yet marked as solved
2 Replies
201 Views
I am trying to calculate the  latitudeDelta & longitudeDelta of visible map area on the phone screen. The two method used were: spanLat = map.visibleMapRect.size.width / 111000; spanLon = map.visibleMapRect.size.height / (111000 * cos(centreLat*3.14159/180)); And MKMapRect mRect = map.visibleMapRect; MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect)); MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));     MKMapPoint northMapPoint = MKMapPointMake(MKMapRectGetMinY(mRect), MKMapRectGetMidX(mRect)); MKMapPoint southMapPoint = MKMapPointMake(MKMapRectGetMaxY(mRect), MKMapRectGetMidX(mRect));     CLLocationDistance wLength = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint); CLLocationDistance hLength = MKMetersBetweenMapPoints(northMapPoint, southMapPoint);       spanLat = wLength / 111000;   spanLon = hLength / (111000 * cos(centreLat*3.14159/180)); Neither gives me right  latitudeDelta & longitudeDelta of the visible area.
Posted
by Telkitty.
Last updated
.
Post not yet marked as solved
0 Replies
212 Views
Hello everyone, how can I change the standard pin icon to another? And how is it possible when I tap on the pin that an additional popup view opens in which the name of the city is at the top and that the associated image from the PhotoView is displayed? How could I best implement this? Thanks in advance MapView import MapKit import SwiftUI struct City: Identifiable {     let id = UUID()     let name: String     let coordinate: CLLocationCoordinate2D } struct MapView: View {     @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 46.62407, longitude: 8.03434), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))          let annotations = [         City(name: "Alte Strasse(Zuhause)", coordinate: CLLocationCoordinate2D(latitude: 46.63649, longitude: 7.97512)),         City(name: "Stalden1", coordinate: CLLocationCoordinate2D(latitude: 46.63937, longitude: 7.97216)),         City(name: "Stalden2", coordinate: CLLocationCoordinate2D(latitude: 46.63873, longitude: 7.96684)),         City(name: "Scheideggstrasse(Wetterhorn)1", coordinate: CLLocationCoordinate2D(latitude: 46.63353, longitude: 8.07356)),         City(name: "Scheideggstrasse(Wetterhorn)2", coordinate: CLLocationCoordinate2D(latitude: 46.63293, longitude: 8.07380)),         City(name: "Scheideggstrasse(Wetterhorn)3", coordinate: CLLocationCoordinate2D(latitude: 46.63313, longitude: 8.07329)),         City(name: "Scheideggstrasse(Wetterhorn)4", coordinate: CLLocationCoordinate2D(latitude: 46.63456, longitude: 8.07337)),         City(name: "Obere Gletscherstrasse(Wetterhorn)", coordinate: CLLocationCoordinate2D(latitude: 46.63205, longitude: 8.07022)),         City(name: "Obere Gletscherstrasse(Hotel Blümlisalp)", coordinate: CLLocationCoordinate2D(latitude: 46.63173, longitude: 8.06699)),         City(name: "Itramenstrasse", coordinate: CLLocationCoordinate2D(latitude: 46.62060, longitude: 7.99514))     ]     var body: some View {         Map(coordinateRegion: $region, annotationItems: annotations) {             MapMarker(coordinate: $0.coordinate)             }         .onAppear {             MKMapView.appearance().mapType = .satellite         }             }     struct MapView_Previews: PreviewProvider {         static var previews: some View {             MapView()         }     } } PhotoView struct ContentView: View {     var images: [String] = ["noaa1", "noaa2", "noaa3", "noaa4", "noaa5", "noaa6", "noaa7", "noaa8", "noaa9", "noaa10", "noaa11", "noaa12", "noaa13", "noaa14", "noaa15", "noaa16", "noaa17", "noaa18"]          var columnGrid: [GridItem] = [GridItem(.fixed(180), spacing: 16), GridItem(.fixed(180), spacing: 16)]              var body: some View {         NavigationView {             ScrollView {                 LazyVGrid(columns: columnGrid, alignment: .center, spacing: 16) {                     ForEach(images, id: \.self) { image in                         NavigationLink (destination: ImageDetail(imageName: image)) {                             Image(image)                                 .resizable()                                 .scaledToFit()                                 .cornerRadius(10)                         }                     }                 }             }.navigationBarTitle(Text("Test"), displayMode: .inline)         }.navigationViewStyle(.stack)     } }
Posted
by iRIG.
Last updated
.
Post marked as solved
1 Replies
132 Views
I am trying to set contraints for MKMapView but whenever I control click I get the outlets menu instead! Works for labels and all else, but not for the map.
Posted
by leo008.
Last updated
.
Post marked as solved
4 Replies
243 Views
I have a standard UITableViewController and I have added an MKMapKit map neatly on the top half of the screen (and the table in the bottom half). During runtime, if we scroll through the table records, the map scrolls up as well. What I wanted to do was have the map static, not scrolling, while I scroll the table. Any ideas on how to accomplish this, please?
Posted
by leo008.
Last updated
.
Post not yet marked as solved
0 Replies
159 Views
when using the MapKit.Map to display multiple Annotation overlapping, everything works properly whether you use MapPin, MapMarker or MapAnnotation. The tricky part is to bring an annotation to the front once you clickOnAnnotation. Considering MapPin, MapMarker or MapAnnotation are MapAnnotationProtocol Custom Types, which respectively, defined as follow in the official documentation: // Available when SwiftUI is imported with MapKit @available... public protocol MapAnnotationProtocol {} @available... public struct MapMarker : MapAnnotationProtocol { public init(coordinate: CLLocationCoordinate2D, tint: Color? = nil) } // Available when SwiftUI is imported with MapKit @available... public struct MapPin : MapAnnotationProtocol { public init(coordinate: CLLocationCoordinate2D, tint: Color? = nil) } // Available when SwiftUI is imported with MapKit @available... public struct MapAnnotation<Content> : MapAnnotationProtocol where Content : View { public init(coordinate: CLLocationCoordinate2D, anchorPoint: CGPoint = CGPoint(x: 0.5, y: 0.5), @ViewBuilder content: () -> Content) } How can we implement a CustomView which also implements this protocol but provides us also with the needed properties which allows us to access the cooler feature of The Map (like zIndex of an annotation as a start)? I know we can still use UIViewRepresentable but that is not my goal, unless ... helpfull Links: Apple Documentation StackOverflow - Related Topic
Posted
by Hdayed.
Last updated
.
Post not yet marked as solved
0 Replies
258 Views
func zoomOne() { region.span.latitudeDelta = 10_000 region.span.longitudeDelta = 10_000 } func zoomIn() { region.span.latitudeDelta /= 2 region.span.longitudeDelta /= 2 } func zoomOut() { region.span.latitudeDelta *= 2 region.span.longitudeDelta *= 2 } My MacOS app has three methods for adjusting the zoom on a MapKit map display. Each is tied to a separate view Button. zoomIn() and zoomOut() work as expected, but when the program calls zoomOne(), Xcode breaks and reports a runtime issue in the Issue Navigator: NSHostingView is being laid out reentrantly while rendering its SwiftUI content. This is not supported and the current layout pass will be skipped. As a fix, I tried embedding the zoomOne() region update in a DispatchQueue.main.async closure, but it still failed with the same issue. Thanks, Mark
Posted Last updated
.
Post not yet marked as solved
0 Replies
118 Views
I have an app that displays a MapKit map with a Text as an a MapAnnotation. When the map is first displayed, the map is centered on the coordinates of the MapAnnotation and once things settle down, the CPU usage reported by Xcode's Debug Navigator goes to 0%. If the map is dragged or zoomed so that the MapAnnotation is not at the center, the CPU usage goes up during the motion, but does not go back to 0%, but settles down to a few per cent. If you look closely at the map, you can sometimes see that the MapAnnotation is vibrating. Other map features do not appear to move. I assume that the MapAnnotation is not tied to a fixed map location as are the map features and if the MapAnnotation is not at the center, its display position needs to be constantly recalculated.
Posted Last updated
.
Post marked as solved
2 Replies
227 Views
Hello, I would like to know if there is a way to disable the focus on a new added annotation on mapkit js. I have a loop that display annotation and every time the new annotation shows up, the map move the camera and set a super-dupper zoom on the new annotation. This is very annoying. Map initialisation : var map = new mapkit.Map("map", { region: region, showsCompass: mapkit.FeatureVisibility.Hidden, showsZoomControl: true, showsMapTypeControl: true, showsUserLocation: true, }); _ New annotation : chasseur = new mapkit.Coordinate(48.8607, 2.3421); ChasseurAnnotation = new mapkit.MarkerAnnotation(chasseur, { color: "#001788", title: "Chasseur", glyphText: "\u{1F6A8}" // POLICE }); map.showItems([ChasseurAnnotation]); _ Thanks a lot for your further responses.
Posted Last updated
.
Post not yet marked as solved
3 Replies
265 Views
I would like the MKMapItem returned from MKLocalSearch to contain an "altitude" property. In order to achieve this, I've decided to create a subclass for MKMapItem. class MapItemGeoSpatial: MKMapItem { var altitude: Measurement<UnitLength> public init(placemark: MKPlacemark, altitude: Measurement<UnitLength>) { self.altitude = altitude super.init(placemark: placemark) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } While the above implementation compiles, it results in a fatal error when attempting to initialise MapItemGeoSpatial. Fatal error: Use of unimplemented initializer 'init()' for class 'MapItemGeoSpatial' The above error occurs during the super.init(placemark:) call. The altitude property is set by me (the source of this value is not relevant to this discussion, but it is determined by extracting the coordinates from MKMapItem) via the designated initialiser. I understand that MKLocalSearch has nothing to do with altitude, but I, as the client, found it desirable to contain this information when it is passed to other areas of my project. This is the primary reason why I chose to subclass MKMapItem, in an attempt to "inherit" all it's functionality and introduce my own functionality. The flow diagram for this process is- MKLocalSearch -> MKMapItem  -> Determine altitude using coordinates from MKMapItem -> Initialise MapItemGeoSpatial. I can create an override init() for the above class, but this will require me to initialise the altitude property, which is not specified for this initialiser. Initialising altitude by specifying a dummy variable (eg. 0) overcomes this issue, but appears to be a poor workaround. Making altitude optional is another workaround, but this is not a direction I wish to take. I'm aware that MKMapItem inherits from NSObject and I'm curious if this relation has an influence on the above observation. I would like to- Understand the root cause behind this issue and Determine if subclassing MKMapItem is a viable solution to the problem mentioned at the start of this post
Posted
by ranveerm.
Last updated
.
Post marked as solved
3 Replies
311 Views
I used the Xcode template to create a Mac App. I then modified ContentView.swift import SwiftUI import MapKit struct ContentView: View {     @State private var region = MKCoordinateRegion( center: CLLocationCoordinate2D( latitude: 0.0, longitude: 0.0), span: MKCoordinateSpan( latitudeDelta: 0.5, longitudeDelta: 0.5))     var body: some View {         Map(coordinateRegion: $region) } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } When I run this code, Xcode reports a runtime error: Modifying state during view update, this will cause undefined behavior. It is reported on Thread 1. This is identified with a purple warning in the issue navigator, but there is no indication of the location of the error in the source code editor. The actual map coordinates don't matter. I get the same error with different coordinates. Xcode 13.3.1 OSX Monterey 12.3.1 Mac Mini M1 P.S. When I cut the example code above from Xcode and pasted it into this message, it added extra line breaks between each line (which I manually removed). Is there a way to have the code paste as it was in Xcode? P.P.S. When I listed the software and hardware info, If I don't make it a bulleted list, the ended up being displayed on one line. If I put blank lines between them, the blank lines are displayed. Is there any way to make a vertical list without bullets?
Posted Last updated
.
Post not yet marked as solved
0 Replies
188 Views
Are there any solutions to convert floor plans to IMDF files for Indoor Mapping (or at least plans to create a conversion tool)? Most of the tools such as ERSI and Autodesk seem to be developed more for building engineers and architects. Is there anything for content managers to ingest floor plans (provided by our facilities dept), and create and update locations in buildings as they change, and publish to an in-app Apple map implementation? Thanks in advance, Scott
Posted Last updated
.
Post not yet marked as solved
0 Replies
141 Views
I've copied a couple of tutorial Mac OS apps using MapKit. One uses SwiftUI, the other AppKit. Both initially display a map and allow the user to pan and zoom with the mouse. According to XCode's debug navigator, once the user has stopped panning or zooming, the CPU usage goes to 0%. Both apps allow the user to enter a location and add a MapAnnotation to the map. On the AppKit version, once the annotation is added, the CPU usage drops back to 0%, but on the SwiftUI version, the CPU usage remains around 5 - 15%. If you look closely at the SwiftUI version, you can see the annotation vibrating by a pixel or so on the display. It seems to me that the display is being constantly updated. If the annotation is removed, the CPU usage goes back to 0% I placed a breakpoint in the code that draws the annotation in the SwiftUI version. When adding an annotation, the breakpoint is hit twice, but then no more, so I don't think the CPU is spending time in the Swift code. Xcode 13.3.1 OSX Monterey 12.3.1 Mac Mini M1
Posted Last updated
.
Post not yet marked as solved
0 Replies
102 Views
Hi, if I use mapkit how can I show the longitude and latitude of the center of the map? I don't mean the current location. I just need the coordinates of the shown position.
Posted Last updated
.
Post marked as solved
1 Replies
216 Views
Hello, how can I turn this standard map view into a Satellite map view? Thanks //  MapView.swift //  Grindelwald View // //  Created by Roman Indermühle on 15.04.22. // import MapKit import SwiftUI struct City: Identifiable {     let id = UUID()     let name: String     let coordinate: CLLocationCoordinate2D } struct MapView: View {          @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 46.62407, longitude: 8.03434), span: MKCoordinateSpan(latitudeDelta: 0.17, longitudeDelta: 0.17))               let annotations = [         City(name: "Burglauenen", coordinate: CLLocationCoordinate2D(latitude: 46.63649, longitude: 7.97512)),         City(name: "Stalden", coordinate: CLLocationCoordinate2D(latitude: 46.63937, longitude: 7.97216)),         City(name: "Stalden2", coordinate: CLLocationCoordinate2D(latitude: 46.63873, longitude: 7.96684))                       ]               var body: some View {         Map(coordinateRegion: $region, annotationItems: annotations) {             MapMarker(coordinate: $0.coordinate)                                   }                       }      } struct MapView_Previews: PreviewProvider {     static var previews: some View {         MapView()     } }
Posted
by iRIG.
Last updated
.
Post not yet marked as solved
0 Replies
99 Views
In Maps there is a cycling option available in certain cities, however MKDirectionsTransportType only has automobile, walking, transit and any. Is there a way to get cycling routes for say London using mapKit?
Posted
by LeroyMc.
Last updated
.