How to move MKMapView based on selected annotation

后端 未结 4 1769
甜味超标
甜味超标 2021-01-02 03:43

I have an MKMapView which populates the entirety of my view, but when a pin is selected, I am sliding up another view on top of the map. I want to move the map so the pin wi

相关标签:
4条回答
  • 2021-01-02 04:01

    I built it on Swift 4, based on answers here

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    
            // center the mapView on the selected pin
            var dest = view.annotation!.coordinate
            if view.annotation?.title == "Name of Annotation View"{//This is for move a little down depending on the MKAnnotationView size
                dest.latitude = dest.latitude + 0.002
            }
            let span = MKCoordinateSpan.init(latitudeDelta: 0.01, longitudeDelta: 0.01)
            let region = MKCoordinateRegion(center: dest, span: span)
            mapView.setRegion(region, animated: true)
    
    
        }
    
    0 讨论(0)
  • 2021-01-02 04:10

    Using MKMapViewDelegate method:

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    
        // center the mapView on the selected pin
        let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
        mapView.setRegion(region, animated: true)
    }
    
    0 讨论(0)
  • 2021-01-02 04:18

    You could try getting the MKMapRect from visibleMapRect for the map view, converting the annotation's coordinate to an MKMapPoint, resetting the MKMapRect's origin so the MKMapPoint is in the appropriate position, and then using setVisibleMapRect:animated: to set the visible region to the new MKMapRect.

    For example, if you wanted to move the map so the annotation is centered horizontally and 25% of the way down vertically, you could do something like this:

    MKMapRect r = [mapView visibleMapRect];
    MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
    r.origin.x = pt.x - r.size.width * 0.5;
    r.origin.y = pt.y - r.size.height * 0.25;
    [mapView setVisibleMapRect:r animated:YES]; 
    
    0 讨论(0)
  • 2021-01-02 04:25

    I ended up with following procedures:

    ** Centering on annotation:**

    - (void) centerOnSelection:(id<MKAnnotation>)annotation
    {
        MKCoordinateRegion region = self.mapView.region;
        region.center = annotation.coordinate;
    
        CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
        region.center.latitude -= self.mapView.region.span.latitudeDelta * per;
    
        [self.mapView setRegion:region animated:YES];
    }
    

    ** Zooming on annotation:**

    - (void) zoomAndCenterOnSelection:(id<MKAnnotation>)annotation
    {
        DLog(@"zoomAndCenterOnSelection");
    
        MKCoordinateRegion region = self.mapView.region;
        MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);
    
        region.center = annotation.coordinate;
    
        CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
        region.center.latitude -= self.mapView.region.span.latitudeDelta * span.latitudeDelta / region.span.latitudeDelta * per;
    
        region.span = span;
    
        [self.mapView setRegion:region animated:YES];
    }
    

    -(CGFloat) sizeOfBottom and -(CGFloat) sizeOfTop both return height of panels covering the mapview from the layout guides

    0 讨论(0)
提交回复
热议问题