2013-01-09 8 views
8

मैं परिणाम प्राप्त करना चाहता हूं MKMapRect वर्तमान visibleMapRect की तुलना में सभी दिशाओं में 10-20% बड़ा है। यदि यह एक सीजीआरएक्ट था, तो मैं नकारात्मक एक्स और वाई मानों के साथ सीजीआरईटीईएफ़सेट का उपयोग करता हूं, जो मुझे एक व्यस्त इन्सेट (यानी एक बड़ा आयत) प्रदान करता है। दुर्भाग्य से, MKMapInset नकारात्मक इन्सेट मानों का समर्थन नहीं करता है, इसलिए यह इतना आसान नहीं है।मैं एक निश्चित प्रतिशत से MKMapRect का विस्तार कैसे कर सकता हूं?

नक्शा रेक्ट के मान पहचानने योग्य इकाइयां हैं, लेकिन मूल x और y मान 4.29445e + 07 के क्रम पर हैं, और चौड़ाई/ऊंचाई 2500-3000 है, तो यह आसान हो सकता है।

मैं मैन्युअल रूप से ऐसा करने के लिए एक श्रेणी लिखने से लगभग 10 सेकंड हूं लेकिन मैं यह सुनिश्चित करना चाहता था कि मैं पहले कुछ याद नहीं कर रहा था। क्या MKMapRect का विस्तार करने का कोई आसान तरीका है?

+0

आप बिल्कुल यह मदद करता है? : http://stackoverflow.com/questions/8465149/mkmaprect-zooms-too- बहुत – Firo

+0

नोट, 2017 के लिए ऐप्पल ने अब ** सभी काम किया है **, – Fattie

उत्तर

20

आईओएस 7, rectForMapRect: और mapRectForRect: में बहिष्कृत कर दिया गया है और अब MKOverlayRenderer वर्ग का हिस्सा हैं। मैं MapViewmapRectThatFits: edgePadding: विधियों का उपयोग करने की सिफारिश करता हूं।

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

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

शानदार उत्तर। उपयोगकर्ता, मैंने उस कॉल के लिए नवीनतम वाक्यविन्यास में फेंक दिया, चीयर्स को संपादित करने के लिए स्वतंत्र महसूस करें। – Fattie

5

क्या rectForMapRect: के साथ एक CGRect को visibleMapRect परिवर्तित करने, एक नया CGRectCGRectInset साथ हो रही है और फिर mapRectForRect: के साथ एक MKMapRect करने के लिए इसे वापस परिवर्तित करने के बारे में?

+0

से नीचे का जवाब देखें यह वास्तव में अब गलत है, 2017. ऐप्पल है, भगवान का शुक्र है, अब सभी काम किया है। ** mapRectThatFits # एजपैडिंग ** – Fattie

0

रिफाइंड ... और स्विफ्ट 4 के लिए user2285781's answer को सही: यहाँ एक नमूना कोड है

// 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) 
    } 
संबंधित मुद्दे

 संबंधित मुद्दे