पोजिशनिंग MKMapView ही बार में कई टिप्पणियां दिखाने का

वोट
89

मैं कई एनोटेशन मैं अपने MKMapView में जोड़ना चाहते हैं मिल गया है (यह हो सकता 0-एन आइटम, जहां n आम तौर पर लगभग 5 है)। मैं एनोटेशन जोड़ सकते हैं ठीक है, लेकिन मैं परदे पर एक ही बार में सभी एनोटेशन फिट करने के लिए नक्शा आकार बदलने के लिए चाहते हैं, और मैं ऐसा करने के तरीके यकीन नहीं है।

मैं को देखकर किया गया है -regionThatFits:, लेकिन मैं काफी यह क्या से कोई लेना देना यकीन नहीं है। मैं कुछ कोड पोस्ट दिखाने के लिए मैं अब तक क्या मिल गया है जाएगा। मुझे लगता है कि यह एक आम तौर पर काम बिल्कुल आसान हो, लेकिन मैं अब तक MapKit के साथ एक सा अभिभूत महसूस कर रहा हूँ।

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

location = newLocation.coordinate;
//One location is obtained.. just zoom to that location

MKCoordinateRegion region;
region.center = location;

//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta = 0.015;
span.longitudeDelta = 0.015;
region.span = span;
// Set the region here... but I want this to be a dynamic size
// Obviously this should be set after I've added my annotations
[mapView setRegion:region animated:YES];

// Test data, using these as annotations for now
NSArray *arr = [NSArray arrayWithObjects:@one, @two, @three, @four, nil];
float ex = 0.01;
for (NSString *s in arr) {
    JBAnnotation *placemark = [[JBAnnotation alloc] initWithLat:(location.latitude + ex) lon:location.longitude];
    [mapView addAnnotation:placemark];
    ex = ex + 0.005;
}
    // What do I do here?
    [mapView setRegion:[mapView regionThatFits:region] animated:YES];
}

सूचना, यह सब होता है के रूप में मैं कोई स्थान अपडेट प्राप्त ... कि अगर यह करने के लिए एक उपयुक्त जगह है मैं नहीं जानता। यदि नहीं, तो जहां एक बेहतर जगह हो सकता है? -viewDidLoad?

अग्रिम में धन्यवाद।

26/08/2009 को 18:35
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


23 जवाब

वोट
133

लिंक जिम द्वारा पोस्ट की गई अब मर चुका है, लेकिन मैं कोड की खोज करने में सक्षम था (जो मैंने कहीं बुकमार्क किया है)। उम्मीद है की यह मदद करेगा।

- (void)zoomToFitMapAnnotations:(MKMapView *)mapView { 
    if ([mapView.annotations count] == 0) return; 

    CLLocationCoordinate2D topLeftCoord; 
    topLeftCoord.latitude = -90; 
    topLeftCoord.longitude = 180; 

    CLLocationCoordinate2D bottomRightCoord; 
    bottomRightCoord.latitude = 90; 
    bottomRightCoord.longitude = -180; 

    for(id<MKAnnotation> annotation in mapView.annotations) { 
        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); 
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); 
        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); 
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); 
    } 

    MKCoordinateRegion region; 
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; 
    region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;      

    // Add a little extra space on the sides
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; 

    region = [mapView regionThatFits:region]; 
    [mapView setRegion:region animated:YES]; 
}
26/08/2011 को 07:22
का स्रोत उपयोगकर्ता

वोट
132

क्यों इतने जटिल?

MKCoordinateRegion coordinateRegionForCoordinates(CLLocationCoordinate2D *coords, NSUInteger coordCount) {
    MKMapRect r = MKMapRectNull;
    for (NSUInteger i=0; i < coordCount; ++i) {
        MKMapPoint p = MKMapPointForCoordinate(coords[i]);
        r = MKMapRectUnion(r, MKMapRectMake(p.x, p.y, 0, 0));
    }
    return MKCoordinateRegionForMapRect(r);
}
08/08/2012 को 11:41
का स्रोत उपयोगकर्ता

वोट
43

मैं एक क्षेत्र है कि एक बिंदु एनोटेशन और वर्तमान स्थान शामिल करने के लिए कुछ इस के समान जूम आउट करने (या में) किया है। आप अपनी टिप्पणियों को के माध्यम से पाशन द्वारा इस विस्तार कर सकता है।

बुनियादी कदम हैं:

  • अक्षां मिनट की गणना / लंबे
  • गणना अधिकतम अक्षांश / देशांतर
  • इन दो बिंदुओं के लिए CLLocation वस्तुओं बनाएं
  • अंक के बीच दूरी की गणना
  • क्षेत्र बनाएं अंक और दूरी डिग्री करने के लिए परिवर्तित के बीच केंद्र बिंदु का उपयोग कर
  • MapView में क्षेत्र दर्रा समायोजित करने के लिए
  • MapView क्षेत्र स्थापित करने के लिए समायोजित क्षेत्र का उपयोग करें
    -(IBAction)zoomOut:(id)sender {

        CLLocationCoordinate2D southWest = _newLocation.coordinate;
        CLLocationCoordinate2D northEast = southWest;

        southWest.latitude = MIN(southWest.latitude, _annotation.coordinate.latitude);
        southWest.longitude = MIN(southWest.longitude, _annotation.coordinate.longitude);

        northEast.latitude = MAX(northEast.latitude, _annotation.coordinate.latitude);
        northEast.longitude = MAX(northEast.longitude, _annotation.coordinate.longitude);

        CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
        CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];

        // This is a diag distance (if you wanted tighter you could do NE-NW or NE-SE)
        CLLocationDistance meters = [locSouthWest getDistanceFrom:locNorthEast];

        MKCoordinateRegion region;
        region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
        region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
        region.span.latitudeDelta = meters / 111319.5;
        region.span.longitudeDelta = 0.0;

        _savedRegion = [_mapView regionThatFits:region];
        [_mapView setRegion:_savedRegion animated:YES];

        [locSouthWest release];
        [locNorthEast release];
    }
27/08/2009 को 20:56
का स्रोत उपयोगकर्ता

वोट
36

IOS7 के रूप में आप का उपयोग कर सकते showAnnotations: एनिमेटेड:

[mapView showAnnotations:annotations animated:YES];
22/03/2014 को 02:27
का स्रोत उपयोगकर्ता

वोट
21

मैं एक अलग जवाब दिया है। मैं ज़ूम करने के लिए फिट एल्गोरिथ्म अपने आप को लागू करने के लिए जा रहा था, लेकिन मैं समझ एप्पल कि चाहिए कि हम क्या इतना काम के बिना चाहते थे करने के लिए एक रास्ता है। का उपयोग करते हुए एपीआई doco जल्दी से पता चला है कि मैं क्या जरूरत थी करने के लिए MKPolygon इस्तेमाल कर सकते हैं:

/* this simply adds a single pin and zooms in on it nicely */
- (void) zoomToAnnotation:(MapAnnotation*)annotation {
    MKCoordinateSpan span = {0.027, 0.027};
    MKCoordinateRegion region = {[annotation coordinate], span};
    [mapView setRegion:region animated:YES];
}

/* This returns a rectangle bounding all of the pins within the supplied
   array */
- (MKMapRect) getMapRectUsingAnnotations:(NSArray*)theAnnotations {
    MKMapPoint points`theAnnotations count`;

    for (int i = 0; i < [theAnnotations count]; i++) {
        MapAnnotation *annotation = [theAnnotations objectAtIndex:i];
        points[i] = MKMapPointForCoordinate(annotation.coordinate);
    }

    MKPolygon *poly = [MKPolygon polygonWithPoints:points count:[theAnnotations count]];

    return [poly boundingMapRect];
}

/* this adds the provided annotation to the mapview object, zooming 
   as appropriate */
- (void) addMapAnnotationToMapView:(MapAnnotation*)annotation {
    if ([annotations count] == 1) {
        // If there is only one annotation then zoom into it.
        [self zoomToAnnotation:annotation];
    } else {
        // If there are several, then the default behaviour is to show all of them
        //
        MKCoordinateRegion region = MKCoordinateRegionForMapRect([self getMapRectUsingAnnotations:annotations]);

        if (region.span.latitudeDelta < 0.027) {
            region.span.latitudeDelta = 0.027;
        }

        if (region.span.longitudeDelta < 0.027) {
            region.span.longitudeDelta = 0.027;
        }
        [mapView setRegion:region];
    }

    [mapView addAnnotation:annotation];
    [mapView selectAnnotation:annotation animated:YES];
}

उम्मीद है की यह मदद करेगा।

04/10/2011 को 02:50
का स्रोत उपयोगकर्ता

वोट
14

आप भी इसे इस तरह से कर सकते हैं ..

// Position the map so that all overlays and annotations are visible on screen.
MKMapRect regionToDisplay = [self mapRectForAnnotations:annotationsToDisplay];
if (!MKMapRectIsNull(regionToDisplay)) myMapView.visibleMapRect = regionToDisplay;

- (MKMapRect) mapRectForAnnotations:(NSArray*)annotationsArray
{
    MKMapRect mapRect = MKMapRectNull;

    //annotations is an array with all the annotations I want to display on the map
    for (id<MKAnnotation> annotation in annotations) { 

        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);

        if (MKMapRectIsNull(mapRect)) 
        {
            mapRect = pointRect;
        } else 
        {
            mapRect = MKMapRectUnion(mapRect, pointRect);
        }
    }

     return mapRect;
}
12/09/2011 को 05:59
का स्रोत उपयोगकर्ता

वोट
12

मैं निम्नलिखित के साथ आया था और हर किसी को जानकारी से सुझाव के आधार पर। :) योगदान यह है कि MapView शामिल दृश्य नियंत्रक में जाना होगा के लिए इस चर्चा में हर किसी के लिए धन्यवाद।

- (void)zoomToFitMapAnnotations { 

if ([self.mapView.annotations count] == 0) return; 

int i = 0;
MKMapPoint points`self`.`mapView`.`annotations count`;

//build array of annotation points
for (id<MKAnnotation> annotation in [self.mapView annotations])
        points[i++] = MKMapPointForCoordinate(annotation.coordinate);

MKPolygon *poly = [MKPolygon polygonWithPoints:points count:i];

[self.mapView setRegion:MKCoordinateRegionForMapRect([poly boundingMapRect]) animated:YES]; 
}
20/10/2011 को 21:00
का स्रोत उपयोगकर्ता

वोट
5

मेरे मामले में, मैं CLLocation वस्तुओं के साथ शुरू कर रहा हूँ और उनमें से प्रत्येक के लिए एनोटेशन का निर्माण।
मैं केवल दो एनोटेशन जगह की जरूरत है, तो मैं अंकों की सरणी के निर्माण के लिए एक सरल दृष्टिकोण है, लेकिन यह आसानी से CLLocations का एक सेट दिया एक मनमाना लंबाई के साथ एक सरणी का निर्माण करने के लिए विस्तारित किया जा सकता है।

यहाँ मेरी कार्यान्वयन (MKMapPoints बनाने की आवश्यकता नहीं है) है:

//start with a couple of locations
CLLocation *storeLocation = store.address.location.clLocation;
CLLocation *userLocation = [LBLocationController sharedController].currentLocation;

//build an array of points however you want
CLLocationCoordinate2D points[2] = {storeLocation.coordinate, userLocation.coordinate};

//the magic part
MKPolygon *poly = [MKPolygon polygonWithCoordinates:points count:2];
[self.mapView setRegion:MKCoordinateRegionForMapRect([poly boundingMapRect])];
25/01/2012 को 21:11
का स्रोत उपयोगकर्ता

वोट
4

स्विफ्ट, एक बहुभुज, और कुछ अतिरिक्त गद्दी मैं निम्नलिखित प्रयोग किया जाता का उपयोग करना:

func zoomToFit() {
    var allLocations:[CLLocationCoordinate2D] = [
        CLLocationCoordinate2D(latitude: 32.768805, longitude: -117.167119),
        CLLocationCoordinate2D(latitude: 32.770480, longitude: -117.148385),
        CLLocationCoordinate2D(latitude: 32.869675, longitude: -117.212929)
    ]

    var poly:MKPolygon = MKPolygon(coordinates: &allLocations, count: allLocations.count)

    self.mapView.setVisibleMapRect(poly.boundingMapRect, edgePadding: UIEdgeInsetsMake(40.0, 40.0, 40.0, 40.0), animated: false)
}

06/04/2015 को 15:46
का स्रोत उपयोगकर्ता

वोट
3

IOS के 7 के रूप में है कि आप उपयोग कर सकते हैं 'MKMapView' में एक नई विधि है

घोषणा

स्विफ्ट

func showAnnotations(_ annotations: [AnyObject]!,
            animated animated: Bool)

उद्देश्य सी

- (void)showAnnotations:(NSArray *)annotations
               animated:(BOOL)animated

पैरामीटर

एनोटेशन हैं जिन्हें आप नक्शे में दिखाई देना चाहते हैं अपनी टिप्पणियों। एनिमेटेड हाँ अगर आप चाहते हैं नक्शा क्षेत्र परिवर्तन एनिमेटेड किया जा करने के लिए, या नहीं आप नक्शे एनिमेशन के बिना तुरंत नए क्षेत्र प्रदर्शित करने के लिए चाहते हैं।

विचार-विमर्श

इस विधि अद्यतन कॉलिंग क्षेत्र संपत्ति में मूल्य और संभवत: अन्य गुण नया नक्शा क्षेत्र प्रतिबिंबित करने के लिए।

26/02/2015 को 04:39
का स्रोत उपयोगकर्ता

वोट
3

यहाँ स्विफ्ट समतुल्य है (पुष्टि में कार्य करना: Xcode6.1, एसडीके 8.2) मुस्तफा के जवाब के लिए:

    func zoomToFitMapAnnotations() {
    if self.annotations.count == 0 {return}

    var topLeftCoordinate = CLLocationCoordinate2D(latitude: -90, longitude: 180)
    var bottomRightCoordinate = CLLocationCoordinate2D(latitude: 90, longitude: -180)

    var i = 1
    for object in self.annotations {
        if let annotation = object as? MKAnnotation {
            topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, annotation.coordinate.longitude)
            topLeftCoordinate.latitude = fmin(topLeftCoordinate.latitude, annotation.coordinate.latitude)
            bottomRightCoordinate.longitude = fmin(bottomRightCoordinate.longitude, annotation.coordinate.longitude)
            bottomRightCoordinate.latitude = fmin(bottomRightCoordinate.latitude, annotation.coordinate.latitude)
        }
    }

    var center = CLLocationCoordinate2D(latitude: topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 0.5, longitude: topLeftCoordinate.longitude - (topLeftCoordinate.longitude - bottomRightCoordinate.longitude) * 0.5)

    print("\ncenter:\(center.latitude) \(center.longitude)")
    // Add a little extra space on the sides
    var span = MKCoordinateSpanMake(fabs(topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 1.01, fabs(bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 1.01)
    print("\nspan:\(span.latitudeDelta) \(span.longitudeDelta)")

    var region = MKCoordinateRegion(center: center, span: span)


    region = self.regionThatFits(region)

    self.setRegion(region, animated: true)

}
23/01/2015 को 11:19
का स्रोत उपयोगकर्ता

वोट
2

द्वारा उत्कृष्ट उत्तर के आधार पर me2(अब स्विफ्ट में)

func coordinateRegionForCoordinates(coords: [CLLocationCoordinate2D]) -> MKCoordinateRegion {
    var rect: MKMapRect = MKMapRectNull
    for coord in coords {
        let point: MKMapPoint = MKMapPointForCoordinate(coord)
        rect = MKMapRectUnion(rect, MKMapRectMake(point.x, point.y, 0, 0))
    }
    return MKCoordinateRegionForMapRect(rect)
}
18/05/2015 को 14:05
का स्रोत उपयोगकर्ता

वोट
2
- (void)zoomToFitMapAnnotations {

if ([self.mapview.annotations count] == 0) return;

int i = 0;
MKMapPoint points`self`.`mapview`.`annotations count`;

//build array of annotation points
for (id<MKAnnotation> annotation in [self.mapview annotations])
    points[i++] = MKMapPointForCoordinate(annotation.coordinate);

MKPolygon *poly = [MKPolygon polygonWithPoints:points count:i];

[self.mapview setRegion:MKCoordinateRegionForMapRect([poly boundingMapRect]) animated:YES];
}
03/12/2014 को 09:56
का स्रोत उपयोगकर्ता

वोट
2

एक संभव समाधान वर्तमान स्थान और सभी एनोटेशन के बीच की दूरी को मापने के किया जा सकता है और एक क्षेत्र दूर एनोटेशन की तुलना में एक से थोड़ा अधिक दूरी है कि बनाने के लिए MKCoordinateRegionMakeWithDistance पद्धति का उपयोग करके।

इस कोर्स की धीमी और अधिक टिप्पणियां आप हालांकि जोड़ा मिलेगा।

26/08/2009 को 21:13
का स्रोत उपयोगकर्ता

वोट
1

मैं जानता हूँ कि यह एक पुराने सवाल है, लेकिन, अगर आप सभी एनोटेशन पर पहले से ही नक्शा इस का उपयोग प्रदर्शित करना चाहते हैं:

 mapView.showAnnotations(mapView.annotations, animated: true)
23/12/2016 को 20:35
का स्रोत उपयोगकर्ता

वोट
1

एक छोटे से अगर खंड mustufa के cound कोड स्निपेट में जोड़ने के लिए 1 स्थान के संभालने के लिए जोड़ा गया। उस के लिए pkclSoft के zoomToAnnotation समारोह उपयोग किया:

if ([mapView.annotations count] == 1){
    MKCoordinateSpan span = {0.027, 0.027};
    region.span = span;
    CLLocationCoordinate2D singleCoordinate = [[mapView.annotations objectAtIndex:0] coordinate];
    region.center.latitude = singleCoordinate.latitude;
    region.center.longitude = singleCoordinate.longitude;
}
else
{
    // mustufa's code
}
31/01/2012 को 05:31
का स्रोत उपयोगकर्ता

वोट
0

एक तेजी से 5 संस्करण:

   func regionFor(coordinates coords: [CLLocationCoordinate2D]) -> MKCoordinateRegion {
        var r = MKMapRect.null

        for i in 0 ..< coords.count {
            let p = MKMapPoint(coords[i])

            r = r.union(MKMapRect(x: p.x, y: p.y, width: 0, height: 0))
        }

        return MKCoordinateRegion(r)
    }
28/08/2019 को 12:40
का स्रोत उपयोगकर्ता

वोट
0

इस विस्तार पर विचार करें:

extension MKCoordinateRegion {
    init(locations: [CLLocationCoordinate2D], marginMultiplier: Double = 1.1) {
        let mapRect = locations.reduce(MKMapRect(), {
            let point = MKMapPointForCoordinate($1)
            let rect = MKMapRect(origin: point, size: MKMapSize(width: 0.0, height: 0.0))
            return MKMapRectUnion($0, rect)
        })

        var coordinateRegion = MKCoordinateRegionForMapRect(mapRect)
        coordinateRegion.span.latitudeDelta *= marginMultiplier
        coordinateRegion.span.longitudeDelta *= marginMultiplier
        self = coordinateRegion
    }
}
30/09/2017 को 13:21
का स्रोत उपयोगकर्ता

वोट
0

इस कोड को मेरे लिए काम करता है, यह वर्तमान स्थान के साथ सभी पिन पता चलता है, उम्मीद है कि इस मदद करता है,

func setCenterForMap() {
    var mapRect: MKMapRect = MKMapRectNull
    for loc in mapView.annotations {
        let point: MKMapPoint = MKMapPointForCoordinate(loc.coordinate)
        print( "location is : \(loc.coordinate)");
        mapRect = MKMapRectUnion(mapRect, MKMapRectMake(point.x,point.y,0,0))
    }
    if (locationManager.location != nil) {
        let point: MKMapPoint = MKMapPointForCoordinate(locationManager.location!.coordinate)
        print( "Cur location is : \(locationManager.location!.coordinate)");
        mapRect = MKMapRectUnion(mapRect, MKMapRectMake(point.x,point.y,0,0))
    }

    mapView.setVisibleMapRect(mapRect, edgePadding: UIEdgeInsetsMake(40.0, 40.0, 40.0, 40.0), animated: true)

}
08/04/2016 को 08:35
का स्रोत उपयोगकर्ता

वोट
0

जब से मैं एक जवाब पर टिप्पणी नहीं कर सकता, मैं @ me2 के जवाब में सुविधा के अपने बिट जोड़ना चाहते हैं (क्योंकि मैं सोचा था कि यह सबसे सुंदर दृष्टिकोण यहाँ मिला था)।

मेरे निजी परियोजना के लिए मैं बस एक ver आम आपरेशन के लिए "दृश्य क्षेत्र" कार्यक्षमता को संपुटित करने के लिए MKMapView वर्ग पर एक वर्ग कहा: MKMapView उदाहरण पर सभी वर्तमान-लोडेड एनोटेशन देखने के लिए सक्षम होने के लिए की स्थापना। परिणाम यह था:

ज फ़ाइल

#import <MapKit/MapKit.h>

@interface MKMapView (Extensions)

-(void)ij_setVisibleRectToFitAllLoadedAnnotationsAnimated:(BOOL)animated;
-(void)ij_setVisibleRectToFitAnnotations:(NSArray *)annotations animated:(BOOL)animated;


@end

.m फ़ाइल

#import "MKMapView+Extensions.h"

@implementation MKMapView (Extensions)

/**
 *  Changes the currently visible portion of the map to a region that best fits all the currently loadded annotations on the map, and it optionally animates the change.
 *
 *  @param animated is the change should be perfomed with an animation.
 */
-(void)ij_setVisibleRectToFitAllLoadedAnnotationsAnimated:(BOOL)animated
{
    MKMapView * mapView = self;

    NSArray * annotations = mapView.annotations;

    [self ij_setVisibleRectToFitAnnotations:annotations animated:animated];

}


/**
 *  Changes the currently visible portion of the map to a region that best fits the provided annotations array, and it optionally animates the change.
    All elements from the array must conform to the <MKAnnotation> protocol in order to fetch the coordinates to compute the visible region of the map.
 *
 *  @param annotations an array of elements conforming to the <MKAnnotation> protocol, holding the locations for which the visible portion of the map will be set.
 *  @param animated    wether or not the change should be perfomed with an animation.
 */
-(void)ij_setVisibleRectToFitAnnotations:(NSArray *)annotations animated:(BOOL)animated
{
    MKMapView * mapView = self;

    MKMapRect r = MKMapRectNull;
    for (id<MKAnnotation> a in annotations) {
        ZAssert([a conformsToProtocol:@protocol(MKAnnotation)], @"ERROR: All elements of the array MUST conform to the MKAnnotation protocol. Element (%@) did not fulfill this requirement", a);
        MKMapPoint p = MKMapPointForCoordinate(a.coordinate);
        //MKMapRectUnion performs the union between 2 rects, returning a bigger rect containing both (or just one if the other is null). here we do it for rects without a size (points)
        r = MKMapRectUnion(r, MKMapRectMake(p.x, p.y, 0, 0));
    }

    [mapView setVisibleMapRect:r animated:animated];

}

@end

एक है कि MKMapView उदाहरण पर सभी वर्तमान-लोडेड एनोटेशन फिट बैठता है, और वस्तुओं में से किसी सरणी के लिए यह निर्धारित करने के लिए एक और तरीका करने के लिए नक्शे के दृश्यमान क्षेत्र स्थापित करने के लिए एक: जैसा कि आप देख सकते हैं, मैं 2 विधियां जोड़ दी हैं अब तक। तो MapView के दृश्यमान क्षेत्र कोड तो के रूप में सरल हो जाएगा स्थापित करने के लिए:

   //the mapView instance  
    [self.mapView ij_setVisibleRectToFitAllLoadedAnnotationsAnimated:animated]; 

मुझे आशा है कि यह मदद करता है =)

10/06/2014 को 14:16
का स्रोत उपयोगकर्ता

वोट
0

me2 प्रतिक्रिया के आधार पर मैं MKMapView कुछ मार्जिन जोड़ सकते हैं और उपयोगकर्ता स्थान एनोटेशन को छोड़ने के लिए के लिए एक वर्ग ने लिखा है:

@interface MKMapView (ZoomToFitAnnotations)
- (void)zoomToFitAnnotations:(BOOL)animated;
@end

@implementation MKMapView (ZoomToFitAnnotations)
- (void)zoomToFitAnnotations:(BOOL)animated {
    if (self.annotations.count == 0)
        return;

    MKMapRect rect = MKMapRectNull;
    for (id<MKAnnotation> annotation in self.annotations) {
        if ([annotation isKindOfClass:[MKUserLocation class]] == false) {
            MKMapPoint point = MKMapPointForCoordinate(annotation.coordinate);
            rect = MKMapRectUnion(rect, MKMapRectMake(point.x, point.y, 0, 0));
        }
    }

    MKCoordinateRegion region = MKCoordinateRegionForMapRect(rect);
    region.span.longitudeDelta *= 2; // Margin
    region.span.latitudeDelta *= 2; // Margin
    [self setRegion:region animated:animated];
}
@end
15/04/2014 को 06:39
का स्रोत उपयोगकर्ता

वोट
0
CLLocationCoordinate2D min = CLLocationCoordinate2DMake(99999.0, 99999.0);
CLLocationCoordinate2D max = CLLocationCoordinate2DMake(-99999.0, -99999.0);

// find max/min....

// zoom to cover area
// TODO: Maybe better using a MKPolygon which can calculate its own fitting region.
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((max.latitude + min.latitude) / 2.0, (max.longitude + min.longitude) / 2.0);
MKCoordinateSpan span = MKCoordinateSpanMake(max.latitude - min.latitude, max.longitude - min.longitude);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);

[_mapView setRegion:[_mapView regionThatFits:region] animated:YES];
27/07/2012 को 13:08
का स्रोत उपयोगकर्ता

वोट
0

मुझे आशा है कि यह कम से कम प्रासंगिक है, यह क्या मैं मोनो (pkclSoft के जवाब के आधार पर) के लिए एक साथ रखा है:

void ZoomMap (MKMapView map)
{
    var annotations = map.Annotations;

    if (annotations == null || annotations.Length == 0) 
        return;

    var points = annotations.OfType<MapAnnotation> ()
                            .Select (s => MKMapPoint.FromCoordinate (s.Coordinate))
                            .ToArray ();            

    map.SetVisibleMapRect(MKPolygon.FromPoints (points).BoundingMapRect, true); 
}
06/03/2012 को 07:13
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more