How do I create an image overlay and add to MKMapView?

后端 未结 4 1378
甜味超标
甜味超标 2020-12-02 21:22

I am trying to learn MapKit and am now trying to add an image as an overlay to the map view. I can\'t seem to find any sample code to help explain how to do this.

Ca

4条回答
  •  庸人自扰
    2020-12-02 22:07

    In the search for a solution for my problem, I keep coming back to this question, and here and also here

    They were all answering the question I needed answered but in reverse.

    I needed to determine if a UIView, i.e. a draw square on my MapView contains a map pin, if it does let me know so I can remove it or select it etc.

    Finally after trying to convert my UIView into some MKCoordinateRegion or alike, I thought work backwards. Instead of turning my UIView or square into map madness, I turned the pins into simple CGPoints and did a simple Rect contains point. Which ended up being my solution.. after about 2 hrs!

    Hope this help someone down the line

     func findMapPinsWithinBox() {
    
        guard let myBox = myBox else { return } // some UIView you have drawn or placed on map
    
        let visibleMapRect = mapView.visibleMapRect
        let visibleAnnotations =  mapView.annotationsInMapRect(visibleMapRect)
    
        for pin in visibleAnnotations {
    
            // depending on design you may need to do some casting here
            // ie let pin = pin as! MyMapAnnotation
    
            let pinsCGPoint = mapView.convertCoordinate(pin.coordinate, toPointToView: self.view)
    
            if myBox.frame.contains(pinsCGPoint) {
                print("Pin Found within the Box")
                // select pin or delete etc
            }
        }
    }
    

提交回复
热议问题