2013-01-09 7 views
8

Desidero ottenere un risultato MKMapRect più grande del 10-20% in tutte le direzioni rispetto all'attuale visibleMapRect. Se questo fosse un CGRect, userei CGRectInset con valori xey negativi, fornendomi un inserto inverso (cioè un rect più grande). Sfortunatamente, MKMapInset non supporta valori di inset negativi, quindi non è così facile.Come posso espandere un MKMapRect di una percentuale fissa?

Questo potrebbe essere più semplice se i valori per il rect mappa erano unità riconoscibili ma i valori di origine x e y sono nell'ordine di 4.29445e + 07 e la larghezza/altezza è 2500-3000.

Sono a circa 10 secondi dalla scrittura di una categoria per farlo manualmente ma volevo essere sicuro che prima non mi mancasse qualcosa. C'è un modo più semplice per espandere MKMapRect?

+0

Questo aiuto a tutti? : http://stackoverflow.com/questions/8465149/mkmaprect-zooms-too-much – Firo

+0

note, per il 2017 Apple ha ora ** fatto tutto il lavoro **, vedere la risposta sotto – Fattie

risposta

20

In iOS7, rectForMapRect: e mapRectForRect: è stato deprecato e ora fa parte della classe MKOverlayRenderer. Preferisco raccomandare di utilizzare i metodi MapViewmapRectThatFits: edgePadding:. Ecco un esempio di codice:

MKMapRect visibleRect = self.mapView.visibleMapRect; 
UIEdgeInsets insets = UIEdgeInsetsMake(50, 50, 50, 50); 
MKMapRect biggerRect = [self.mapView mapRectThatFits:visibleRect edgePadding:insets]; 

ultima Swift per il 2017 ...

func updateMap() { 

    mkMap.removeAnnotations(mkMap.annotations) 
    mkMap.addAnnotations(yourAnnotationsArray) 

    var union = MKMapRectNull 

    for p in yourAnnotationsArray { 
     // make a small, say, 50meter square for each 
     let pReg = MKCoordinateRegionMakeWithDistance(pa.coordinate, 50, 50) 
     // convert it to a MKMapRect 
     let r = mkMapRect(forMKCoordinateRegion: pReg) 

     // union all of those 
     union = MKMapRectUnion(union, r) 

     // probably want to turn on the "sign" for each 
     mkMap.selectAnnotation(pa, animated: false) 
    } 

    // expand the union, using the new #edgePadding call. T,L,B,R 
    let f = mkMap.mapRectThatFits(union, edgePadding: UIEdgeInsetsMake(70, 0, 10, 35)) 

    // NOTE you want the TOP padding much bigger than the BOTTOM padding 
    // because the pins/signs are actually very tall 

    mkMap.setVisibleMapRect(f, animated: false) 

} 
+0

ottima risposta. utente, ho inserito l'ultima sintassi per quella chiamata, sentitevi liberi di modificare ecc. – Fattie

5

Che dire conversione del visibleMapRect ad un CGRect con rectForMapRect:, ottenendo un nuovo CGRect con CGRectInset e poi convertirlo di nuovo ad un MKMapRect con mapRectForRect:?

+0

questo è in realtà non corretto ora, 2017. Apple avere, grazie al cielo, fatto tutto il lavoro ora. ** mapRectThatFits # edgePadding ** – Fattie

0

raffinato e corretto user2285781's answer per Swift 4:

// reference: https://stackoverflow.com/a/15683034/347339 
    func MKMapRectForCoordinateRegion(region:MKCoordinateRegion) -> MKMapRect { 
     let topLeft = CLLocationCoordinate2D(latitude: region.center.latitude + (region.span.latitudeDelta/2), longitude: region.center.longitude - (region.span.longitudeDelta/2)) 
     let bottomRight = CLLocationCoordinate2D(latitude: region.center.latitude - (region.span.latitudeDelta/2), longitude: region.center.longitude + (region.span.longitudeDelta/2)) 

     let a = MKMapPointForCoordinate(topLeft) 
     let b = MKMapPointForCoordinate(bottomRight) 

     return MKMapRect(origin: MKMapPoint(x:min(a.x,b.x), y:min(a.y,b.y)), size: MKMapSize(width: abs(a.x-b.x), height: abs(a.y-b.y))) 
    } 

    // reference: https://stackoverflow.com/a/19307286/347339 
    // assuming coordinates that create a polyline as well as a destination annotation 
    func updateMap(coordinates: [CLLocationCoordinate2D], annotation: MKAnnotation) { 

     var union = MKMapRectNull 
     var coordinateArray = coordinates 
     coordinateArray.append(annotation.coordinate) 

     for coordinate in coordinateArray { 
      // make a small, say, 50meter square for each 
      let pReg = MKCoordinateRegionMakeWithDistance(coordinate, 50, 50) 
      // convert it to a MKMapRect 
      let r = MKMapRectForCoordinateRegion(region: pReg) 

      // union all of those 
      union = MKMapRectUnion(union, r) 
     } 

     // expand the union, using the new #edgePadding call. T,L,B,R 
     let f = mapView.mapRectThatFits(union, edgePadding: UIEdgeInsetsMake(70, 35, 10, 35)) 

     // NOTE you want the TOP padding much bigger than the BOTTOM padding 
     // because the pins/signs are actually very tall 

     mapView.setVisibleMapRect(f, animated: false) 
    } 
Problemi correlati