2013-01-09 9 views
8

Tôi muốn có được kết quả MKMapRect lớn hơn 10-20% theo mọi hướng so với visibleMapRect hiện tại. Nếu đây là một CGRect tôi sẽ sử dụng CGRectInset với các giá trị âm và x âm, cung cấp cho tôi một inset nghịch đảo (tức là một rect lớn hơn). Thật không may, MKMapInset không hỗ trợ giá trị inset tiêu cực vì vậy nó không phải là khá dễ dàng. Điều này có thể dễ dàng hơn nếu các giá trị cho bản đồ rect là các đơn vị có thể nhận biết được nhưng giá trị gốc x và y là theo thứ tự của 4.29445e + 07 và chiều rộng/chiều cao là 2500-3000.Làm cách nào để mở rộng MKMapRect theo tỷ lệ cố định?

Tôi mất khoảng 10 giây để viết một danh mục để thực hiện việc này theo cách thủ công nhưng tôi muốn đảm bảo rằng tôi không bỏ lỡ điều gì trước tiên. Có cách nào dễ dàng hơn để mở rộng MKMapRect?

+0

giúp đỡ này bạn ở tất cả? : http://stackoverflow.com/questions/8465149/mkmaprect-zooms-too-much – Firo

+0

lưu ý, trong năm 2017, Apple hiện đã ** hoàn thành tất cả công việc **, xem câu trả lời bên dưới – Fattie

Trả lời

20

Trong iOS7, rectForMapRect:mapRectForRect: không được dùng nữa và hiện là một phần của lớp MKOverlayRenderer. Tôi muốn khuyên bạn nên sử dụng các phương pháp MapViewmapRectThatFits: edgePadding:. Dưới đây là một số mẫu mã:

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

Swift mới nhất cho 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

câu trả lời tuyệt vời. người dùng, tôi đã ném cú pháp mới nhất cho cuộc gọi đó, cảm thấy tự do để chỉnh sửa vv. – Fattie

5

gì về chuyển đổi visibleMapRect đến một CGRect với rectForMapRect:, nhận được một mới CGRect với CGRectInset và sau đó chuyển đổi nó trở lại một MKMapRect với mapRectForRect:?

+0

điều này thực sự không chính xác ngay bây giờ, 2017. Apple có, cảm ơn lòng tốt, đã làm tất cả công việc ngay bây giờ. ** mapRectThatFits # edgePadding ** – Fattie

0

tinh và chỉnh user2285781's answer cho 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) 
    }