Positioning MKMapView ukázat několik poznámek najednou

hlasů
89

Mám několik poznámek, které chci přidat do mého MKMapView (to mohlo 0-n položek, kde n je obvykle okolo 5). Mohu přidat poznámky v pořádku, ale já chci, aby velikost mapy, aby se vešly všechny anotace na obrazovce najednou, a nejsem si jistý, jak to udělat.

Byl jsem při pohledu na -regionThatFits:, ale nejsem si úplně jistý, co s tím dělat. Budu psát nějaký kód, aby ukázal, co jsem zatím dostal. Myslím, že by to mělo být obecně jednoduchý úkol, ale cítím trochu zahlceni MapKit doposud.

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

Všimněte si, to vše se děje, když jsem dostávat informace o poloze ... Nevím, jestli je to vhodné místo, jak to udělat. Pokud tomu tak není, kde by byl lepším místem? -viewDidLoad?

Díky předem.

Položena 26/08/2009 v 18:35
zdroj uživatelem
V jiných jazycích...                            


23 odpovědí

hlasů
133

Odkaz odeslal Jim je nyní mrtvý, ale byl jsem schopen najít kód (který jsem záložkou někde). Snad to pomůže.

- (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]; 
}
Odpovězeno 26/08/2011 v 07:22
zdroj uživatelem

hlasů
132

Proč tak složité?

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);
}
Odpovězeno 08/08/2012 v 11:41
zdroj uživatelem

hlasů
43

Jsem udělal něco podobnými to pro oddálení (nebo v) do oblasti, která zahrnovala poznámky bod a aktuální umístění. Dalo by se rozšířit tím, že průchozí svými poznámkami.

Základní kroky jsou následující:

  • Vypočtěte min délku / šířku
  • Vypočítat maximální délku / šířku
  • Vytvořit CLLocation objekty pro tyto dva body
  • Vypočítat vzdálenost mezi body
  • Vytvoření oblasti pomocí středového bodu mezi body a vzdálenost převedeny na stupně
  • Projít regionu do MapView přizpůsobit
  • Použijte upravenou oblast pro nastavení MapView region
    -(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];
    }
Odpovězeno 27/08/2009 v 20:56
zdroj uživatelem

hlasů
36

Jak iOS7 můžete použít showAnnotations: animovaný:

[mapView showAnnotations:annotations animated:YES];
Odpovězeno 22/03/2014 v 02:27
zdroj uživatelem

hlasů
21

Mám jinou odpověď. Byl jsem dělat implementaci algoritmu zoom-to-fit sám, ale já myslel, že Apple musí mít způsob, jak dělat to, co jsme chtěli, aniž by tolik práce. Použití Doco API rychle ukázala, že bych mohl použít MKPolygon k tomu, co je potřeba:

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

Snad to pomůže.

Odpovězeno 04/10/2011 v 02:50
zdroj uživatelem

hlasů
14

můžete si také udělat to takhle ..

// 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;
}
Odpovězeno 12/09/2011 v 05:59
zdroj uživatelem

hlasů
12

Na základě informací a návrhů z každého založený jsem přišel s následujícím. Díky za všechny v této diskusi o přispění :) To by šlo v pohledu Controller, který obsahuje 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]; 
}
Odpovězeno 20/10/2011 v 21:00
zdroj uživatelem

hlasů
5

V mém případě jsem počínaje CLLocation objekty a vytvářet poznámky pro každou z nich.
Já jen potřebuji umístit dvě poznámky, takže mám jednoduchý přístup k budování řadu bodů, ale může snadno rozšířit na vybudování pole s libovolnou délkou danou sadu CLLocations.

Tady je moje implementace (nevyžaduje vytváření 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])];
Odpovězeno 25/01/2012 v 21:11
zdroj uživatelem

hlasů
4

Používání Swift, mnohoúhelník, a nějaký extra polstrování Použil jsem následující:

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

Odpovězeno 06/04/2015 v 15:46
zdroj uživatelem

hlasů
3

K dispozici je nová metoda ‚MKMapView‘ jako iOS 7, které můžete použít

Prohlášení

RYCHLÝ

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

CÍL-C

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

parametry

anotací anotace, které chcete, aby byly viditelné v mapě. animovaný ANO, pokud chcete, aby se změna mapa region má být animované, nebo Ne, pokud chcete, aby se na stránkách okamžitě zobrazit novou oblast bez animací.

Diskuse

Volání této metody aktualizuje hodnotu v majetku kraje a potenciálně další vlastnosti tak, aby odrážely nové mapy regionu.

Odpovězeno 26/02/2015 v 04:39
zdroj uživatelem

hlasů
3

Zde je SWIFT Equivalent (Potvrzeno Práce v: Xcode6.1, SDK 8.2) pro Mustafa své odpovědi:

    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)

}
Odpovězeno 23/01/2015 v 11:19
zdroj uživatelem

hlasů
2

Na výbornou odpověď na bázi aplikace me2(nyní v 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)
}
Odpovězeno 18/05/2015 v 14:05
zdroj uživatelem

hlasů
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];
}
Odpovězeno 03/12/2014 v 09:56
zdroj uživatelem

hlasů
2

Jedním z možných řešení by mohlo být, že měří vzdálenost mezi stávajícím místě a všechny anotace a použití metody MKCoordinateRegionMakeWithDistance aby se oblast, která má o něco větší vzdálenost, než je nejvzdálenější anotace.

To by samozřejmě dostat pomalejší, tím více anotací jste však přidali.

Odpovězeno 26/08/2009 v 21:13
zdroj uživatelem

hlasů
1

Vím, že to je stará otázka, ale chcete-li zobrazit všechny poznámky jsou již na mapě použít toto:

 mapView.showAnnotations(mapView.annotations, animated: true)
Odpovězeno 23/12/2016 v 20:35
zdroj uživatelem

hlasů
1

Přidá trochu pokud klauzule jej zpracovat 1 lokalizovaných přidat do mustufa je Cound fragmentu kódu. Použita funkce zoomToAnnotation pkclSoft pro které:

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
}
Odpovězeno 31/01/2012 v 05:31
zdroj uživatelem

hlasů
0

Při rychlém 5 verze:

   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)
    }
Odpovězeno 28/08/2019 v 12:40
zdroj uživatelem

hlasů
0

Vezměme si toto rozšíření:

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
    }
}
Odpovězeno 30/09/2017 v 13:21
zdroj uživatelem

hlasů
0

Tento kód funguje pro mě, to ukazuje všechny kolíky s aktuální polohou, doufám, že to pomůže,

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)

}
Odpovězeno 08/04/2016 v 08:35
zdroj uživatelem

hlasů
0

Vzhledem k tomu nemohu vyjádřit k odpovědi, bych chtěl přidat svůj trochu pohodlí do @ ME2 je odpověď (protože jsem myslel, že to bylo nejvíce elegantní přístup zde).

Pro můj osobní projekt jsem prostě přidal kategorii o třídu MKMapView zapouzdřit funkce „viditelné oblasti“ pro běžný provoz ver: nastavení, aby bylo možné vidět všechny aktuálně nahrané poznámky na instanci MKMapView. Výsledek byl tento:

.h file

#import <MapKit/MapKit.h>

@interface MKMapView (Extensions)

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


@end

.m souborů

#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

Jak vidíte, jsem přidal 2 způsoby doposud: jeden pro nastavení viditelné oblasti mapy na ten, který je vhodný pro všechny aktuálně nahrané poznámky na instanci MKMapView a jinou metodu nastavit jej do libovolné pole objektů. Takže nastavit viditelnou oblast na MapView je kód by pak být stejně jednoduché jako:

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

Doufám, že to pomůže =)

Odpovězeno 10/06/2014 v 14:16
zdroj uživatelem

hlasů
0

Na reakci ME2 bázi jsem napsal kategorii pro MKMapView přidat nějaké rezervy a přeskočit anotace polohy uživatelů:

@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
Odpovězeno 15/04/2014 v 06:39
zdroj uživatelem

hlasů
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];
Odpovězeno 27/07/2012 v 13:08
zdroj uživatelem

hlasů
0

Doufám, že to je nejméně důležité, je to, co jsem dal dohromady pro Mono (založené mimo pkclSoft odpověď):

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); 
}
Odpovězeno 06/03/2012 v 07:13
zdroj uživatelem

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