Replace its current TODO implementation with: The last step standing between you and seeing your newly implemented overlay on the map is mapView(_:rendererFor:), mentioned earlier. This is not necessarily efficient, but it’s a simple approach to clear previous items from the map. Notice that updateMapOverlayViews() also removes any annotations and overlays that may be present so you don’t end up with duplicate renderings. Now that you’ve added the necessary code, the overlay displays. You’ll see when a user taps the button in the navigation bar to show the map overlay, addOverlay() is called. This method adds an ParkMapOverlay to the map view. In ContentView.swift, find addOverlay() and change its TODO content to: Great! Now that you have both an MKOverlay and MKOverlayRenderer, add them to your map view. To learn more about Core Graphics, check out the Core Graphics tutorial series. However, you can see the code above uses the passed MKMapRect to get a CGRect in which to draw the image in the provided context. Note: The details of Core Graphics drawing are outside the scope of this tutorial. It defines how MapKit should render this view when given a specific MKMapRect, MKZoomScale and the CGContext of the graphic context, with the intent to draw the overlay image onto the context at the appropriate scale. draw(_:zoomScale:in:) is the real meat of this class.init(overlay:overlayImage:) overrides the base method init(overlay:) by providing a second argument.Let rect = self.rect(for: overlay.boundingMapRect)ĬanslateBy(x: 0.0, y: ) Then replace its contents with:Ĭlass ParkMapOverlay: NSObject, MKOverlay Your task is to create an object that represents an overlay for the entire park.įirst, select the Overlays group and create a new Swift file named ParkMapOverlay.swift. Now that you’ve covered the theory, it’s time to put these concepts to use! Adding Your Information to the MapĬurrently, the map doesn’t provide enough information about the park. Instead, you tell the map about MKOverlay objects to display and return MKOverlayRenderers when the delegate method requests them. To sum up, you don’t add MKOverlayRenderer objects directly to the map view. MapKit calls this method when it realizes there’s an MKOverlay object in the region the map view is displaying. In ContentView.swift, you’ll see a delegate method that lets you return an overlay view: MapKit expects to present a MKMapView object, and this class defines the drawing infrastructure used by the map view. Subclassing it lets you set up what you want to display in each spot.įor example, in this app, you’ll draw an image of the roller coaster or restaurant. This is where MKOverlayRenderer comes in. But it doesn’t know what to display in each region. Finally, add the overlays to your map view.Īt this point, the map knows where it’s supposed to display the overlays.In this app, for example, you’ll create an instance for a roller coaster overlay and another for a restaurant overlay. Then, create an instance of your class for each area where you want to display an overlay.These properties define where the overlay resides on the map and its size. First, create your custom class that implements the MKOverlay protocol, which has two required properties: coordinate and boundingMapRect.There are three steps for using this class: MKOverlay tells MapKit where you want it to draw the overlays. All About Overlay Viewsīefore you start creating overlay views, you need to understand two key classes: MKOverlay and MKOverlayRenderer. Once you feel ready, dive right into overlay views. Note: If you feel like you need a refresher on how to add a map to your app, or want to dive deeper on the basics of working with MapKit, visit MapKit Tutorial: Getting Started.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |