Poziționarea MKMapView pentru a afișa mai multe adnotări simultan

voturi
89

Am mai multe adnotări Vreau să adaug la MKMapView mea (poate 0-n elemente, unde n este, în general, în jurul valorii de 5). Pot adăuga adnotările bine, dar vreau să redimensiona harta pentru a se potrivi cu toate notațiile de pe ecran dintr-o dată, și eu nu sunt sigur cum să facă acest lucru.

M - am uitat la , -regionThatFits:dar nu sunt destul de sigur ce să facă cu ea. Voi posta un cod pentru a arăta ceea ce am până acum. Cred că acest lucru ar trebui să fie o sarcină în general simplă , dar mă simt un pic copleșit cu MapKit până în prezent.

- (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];
}

Observați, acest lucru se întâmplă tot ca am primit o actualizare de locație ... Nu știu dacă asta e un loc potrivit pentru a face acest lucru. Dacă nu, în cazul în care ar fi un loc mai bun? -viewDidLoad?

Mulțumesc anticipat.

Întrebat 26/08/2009 la 18:35
sursa de către utilizator
În alte limbi...                            


23 răspunsuri

voturi
133

Link - ul postat de Jim este acum mort, dar am fost în stare să găsească codul ( pe care am bookmarked undeva). Sper că acest lucru vă ajută.

- (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]; 
}
Publicat 26/08/2011 la 07:22
sursa de către utilizator

voturi
132

De ce atât de complicat?

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);
}
Publicat 08/08/2012 la 11:41
sursa de către utilizator

voturi
43

Am făcut ceva similar cu acest lucru pentru a micșora (sau în), într-o zonă care a inclus o adnotare punct și locația curentă. Ai putea extinde acest lucru prin looping prin adnotări tale.

Pașii de bază sunt:

  • Calculați min lat / lung
  • Calculați maxim latitudinea / longitudinea
  • Crearea de obiecte CLLocation pentru aceste două puncte
  • Calculați distanța dintre punctele
  • Creați regiune utilizând un punct central între puncte și distanța convertite în grade
  • Se trece în regiunea MapView pentru a regla
  • Utilizați regiunea ajustată pentru a seta regiunea 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];
    }
Publicat 27/08/2009 la 20:56
sursa de către utilizator

voturi
36

Ca de iOS7 puteți utiliza showAnnotations: animat:

[mapView showAnnotations:annotations animated:YES];
Publicat 22/03/2014 la 02:27
sursa de către utilizator

voturi
21

Am un răspuns diferit. Am fost de gând să pună în aplicare face algoritmul de zoom-to-fit mine, dar am dat seama ca Apple trebuie să aibă o modalitate de a face ceea ce ne - am dorit , fără atât de mult de lucru. Folosind DOCO API -a arătat repede că am putea folosi MKPolygon pentru a face ceea ce era necesar:

/* 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];
}

Sper că acest lucru vă ajută.

Publicat 04/10/2011 la 02:50
sursa de către utilizator

voturi
14

De asemenea, puteți face în acest fel ..

// 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;
}
Publicat 12/09/2011 la 05:59
sursa de către utilizator

voturi
12

Pe baza informațiilor și sugestiile din toată lumea am venit cu următorul text. Vă mulțumim pentru toată lumea în această discuție pentru a contribui :) Acest lucru ar merge în controlerul vizualizare care conține 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]; 
}
Publicat 20/10/2011 la 21:00
sursa de către utilizator

voturi
5

În cazul meu, am început cu obiecte CLLocation și crearea de adnotări pentru fiecare dintre ele.
Am nevoie doar de a plasa două adnotări, așa că am o abordare simplă de a construi matrice de puncte, dar ar putea fi extins cu ușurință pentru a construi o matrice cu o lungime arbitrară dată un set de CLLocations.

Iată punerea în aplicare a mea (nu necesită crearea 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])];
Publicat 25/01/2012 la 21:11
sursa de către utilizator

voturi
4

Folosind Swift, un poligon, iar unele extra padding am folosit următoarele:

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)
}

Publicat 06/04/2015 la 15:46
sursa de către utilizator

voturi
3

Există o nouă metodă în „MKMapView“ ca iOS 7 pe care le puteți utiliza

Declaraţie

RAPID

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

OBIECTIV-C

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

Parametrii

adnotărilor Adnotările pe care doriți să fie vizibile pe hartă. animat pe DA dacă doriți schimbarea hartă regiunea care urmează să fie animată sau NO dacă doriți harta pentru a afișa noua regiune imediat, fără animații.

Discuţie

Apelarea acestei metode actualizează valoarea în proprietate regiune și alte proprietăți potențial pentru a reflecta noua regiune hartă.

Publicat 26/02/2015 la 04:39
sursa de către utilizator

voturi
3

Aici este echivalentul SWIFT (lucru confirmat în: Xcode6.1, SDK 8.2) pentru Răspunsuri lui Mustafa:

    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)

}
Publicat 23/01/2015 la 11:19
sursa de către utilizator

voturi
2

Bazat pe răspunsul excelent de me2(acum în Swift)

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)
}
Publicat 18/05/2015 la 14:05
sursa de către utilizator

voturi
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];
}
Publicat 03/12/2014 la 09:56
sursa de către utilizator

voturi
2

O posibilă soluție ar putea fi măsurarea distanței dintre locația curentă și toate adnotările și folosind metoda MKCoordinateRegionMakeWithDistance pentru a face o regiune care are o distanță puțin mai mare decât cea mai îndepărtată adnotată.

Acest lucru ar fi, desigur obține mai lent mai multe adnotările adăugat, totuși.

Publicat 26/08/2009 la 21:13
sursa de către utilizator

voturi
1

Știu că aceasta este o întrebare veche, dar, dacă doriți să afișați toate adnotările deja pe hartă Utilizați:

 mapView.showAnnotations(mapView.annotations, animated: true)
Publicat 23/12/2016 la 20:35
sursa de către utilizator

voturi
1

A adăugat un pic în cazul în care clauza să se ocupe de 1 pentru a adăuga la localizare face fragment de cod cound mustufa lui. Funcția zoomToAnnotation pkclSoft Folosit pentru că:

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
}
Publicat 31/01/2012 la 05:31
sursa de către utilizator

voturi
0

O rapidă 5 versiune:

   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)
    }
Publicat 28/08/2019 la 12:40
sursa de către utilizator

voturi
0

Luați în considerare această extensie:

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
    }
}
Publicat 30/09/2017 la 13:21
sursa de către utilizator

voturi
0

acest cod funcționează pentru mine, arată toate pinii cu locația curentă, sper acest lucru vă ajută,

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)

}
Publicat 08/04/2016 la 08:35
sursa de către utilizator

voturi
0

Din moment ce eu nu pot comenta un răspuns, aș dori să adaug pic mea de comoditate în @ ME2 e răspunsul (deoarece am crezut că a fost abordarea cea mai elegantă S-au găsit aici).

Pentru proiectul meu personal am adăugat pur și simplu o categorie pe clasa MKMapView pentru a îngloba funcționalitatea „zona vizibilă“ pentru o operațiune comună ver: setarea pentru a putea vedea toate adnotările în prezent-încărcate pe instanța MKMapView. rezultatul a fost aceasta:

fișier .h

#import <MapKit/MapKit.h>

@interface MKMapView (Extensions)

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


@end

fișier .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

După cum puteți vedea, am adăugat 2 metode de până acum: una pentru stabilirea regiunea vizibilă a hărții cu cel care se potrivește adnotări toate în prezent-încărcate pe instanța MKMapView, și o altă metodă pentru a seta la orice matrice de obiecte. Deci, pentru a seta regiunea vizibilă mapView de codul ar fi atunci la fel de simplu ca:

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

Sper că vă ajută =)

Publicat 10/06/2014 la 14:16
sursa de către utilizator

voturi
0

Pe baza răspunsului ME2 am scris o categorie pentru MKMapView pentru a adăuga unele margini și sări peste utilizator Locul de amplasare adnotare:

@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
Publicat 15/04/2014 la 06:39
sursa de către utilizator

voturi
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];
Publicat 27/07/2012 la 13:08
sursa de către utilizator

voturi
0

Sper că acest lucru este cel puțin relevant, aceasta este ceea ce am pus împreună pentru Mono (derivate pornind de la răspunsul lui 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); 
}
Publicat 06/03/2012 la 07:13
sursa de către utilizator

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