Zoom in un MKMapView programmazione

voti
23

Io sto usando un MKMapViewall'interno di un'applicazione per iPhone. Quando si fa clic su un tasto il livello di zoom deve aumentare. Questo è il mio primo approccio:

MKCoordinateRegion zoomIn = mapView.region;
zoomIn.span.latitudeDelta *= 0.5;
[mapView setRegion:zoomIn animated:YES];

Tuttavia, questo codice non ha avuto effetto, dal momento che non ho aggiornato il valore longitudeDelta. Così ho aggiunto questa linea:

zoomIn.span.longitudeDelta *= 0.5;

Ora funziona, ma solo a volte. La latitudeDeltae longitudeDeltanon modificare nello stesso modo, cioè, i loro valori non sono proporzionali. Qualsiasi idea di come risolvere questo?

È pubblicato 23/06/2009 alle 11:37
fonte dall'utente
In altre lingue...                            


9 risposte

voti
27

Non ho idea se questo è il modo giusto per farlo, ma sto usando questo per ingrandire e rimpicciolire.

        case 0: { // Zoom In
        //NSLog(@"Zoom - IN");
        MKCoordinateRegion region;
        //Set Zoom level using Span
        MKCoordinateSpan span;  
        region.center=mapView.region.center;

        span.latitudeDelta=mapView.region.span.latitudeDelta /2.0002;
        span.longitudeDelta=mapView.region.span.longitudeDelta /2.0002;
        region.span=span;
        [mapView setRegion:region animated:TRUE];
    }
        break;

    // Zoom Out 
    case 2: {
        //NSLog(@"Zoom - OUT");
        MKCoordinateRegion region;
        //Set Zoom level using Span
        MKCoordinateSpan span;  
        region.center=mapView.region.center;
        span.latitudeDelta=mapView.region.span.latitudeDelta *2;
        span.longitudeDelta=mapView.region.span.longitudeDelta *2;
        region.span=span;
        [mapView setRegion:region animated:TRUE];
    }
Risposto il 29/07/2009 a 14:58
fonte dall'utente

voti
21

Basta ripulire la risposta di dkdarel

// delta is the zoom factor
// 2 will zoom out x2
// .5 will zoom in by x2
- (void)zoomMap:(MKMapView*)mapView byDelta:(float) delta {

    MKCoordinateRegion region = mapView.region;
    MKCoordinateSpan span = mapView.region.span;
    span.latitudeDelta*=delta;
    span.longitudeDelta*=delta;
    region.span=span;
    [mapView setRegion:region animated:YES];

}

Codice SWIFT:

func zoomMap(byFactor delta: Double) {
    var region: MKCoordinateRegion = self.mapView.region
    var span: MKCoordinateSpan = mapView.region.span
    span.latitudeDelta *= delta
    span.longitudeDelta *= delta
    region.span = span
    mapView.setRegion(region, animated: true)
}
Risposto il 21/11/2013 a 19:43
fonte dall'utente

voti
21

Ecco una soluzione più semplice:

MKUserLocation *userLocation = mapView.userLocation;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (
      userLocation.location.coordinate, 50, 50);
[mapView setRegion:region animated:NO];
Risposto il 28/11/2012 a 18:40
fonte dall'utente

voti
2

Basta tradotto soluzione dkardel a rapida:

@IBAction func zoomOutButtonClicked(sender: UIButton) {
    let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta*2, longitudeDelta: mapView.region.span.longitudeDelta*2)
    let region = MKCoordinateRegion(center: mapView.region.center, span: span)

    mapView.setRegion(region, animated: true)
}

@IBAction func zoomInButtonClicked(sender: UIButton) {
    let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta/2, longitudeDelta: mapView.region.span.longitudeDelta/2)
    let region = MKCoordinateRegion(center: mapView.region.center, span: span)

    mapView.setRegion(region, animated: true)
}
Risposto il 31/03/2015 a 08:43
fonte dall'utente

voti
2

Qui è il mio modo per spostare la mappa fino al punto di annotazione e zoom abbastanza vicino ad esso. Si può facilmente cambiare lo zoom in lineaCGFloat newLatDelta = 0.06f;

- (void)moveMapToAnnotation:(MKPointAnnotation*)annotation
{
    CGFloat fractionLatLon = map.region.span.latitudeDelta / map.region.span.longitudeDelta;
    CGFloat newLatDelta = 0.06f;
    CGFloat newLonDelta = newLatDelta * fractionLatLon;
    MKCoordinateRegion region = MKCoordinateRegionMake(annotation.coordinate, MKCoordinateSpanMake(newLatDelta, newLonDelta));
    [map setRegion:region animated:YES];
}
Risposto il 01/04/2014 a 11:57
fonte dall'utente

voti
1

mapView.setRegion metodo ha problema quando il mappa viene ruotata

È possibile cliccare sulla carta tramite mapView.camera.altitudeproprietà, ma non è animata:

mapView.camera.altitude *= 1.05

È possibile creare nuovo oggetto fotocamera e impostarla con l'animazione:

let currentCamera = mapView.camera
let newCamera: MKMapCamera
if #available(iOS 9.0, *) {
    newCamera = MKMapCamera(lookingAtCenter: currentCamera.centerCoordinate, fromDistance: currentCamera.altitude * 2, pitch: currentCamera.pitch, heading: currentCamera.heading)
} else {
    newCamera = MKMapCamera()
    newCamera.centerCoordinate = currentCamera.centerCoordinate
    newCamera.heading = currentCamera.heading
    newCamera.altitude = currentCamera.altitude * 2
    newCamera.pitch = currentCamera.pitch
}

mapView.setCamera(newCamera, animated: true)
Risposto il 17/05/2017 a 12:33
fonte dall'utente

voti
1
- (IBAction)btnZoomInPressed
{
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    region.center.latitude = lati;
    region.center.longitude = longi;
    span.latitudeDelta=viewMapSingleVenue.region.span.latitudeDelta /2.0002;
    span.longitudeDelta=viewMapSingleVenue.region.span.longitudeDelta /2.0002;
    region.span=span;
    [viewMapSingleVenue setRegion:region animated:TRUE];
}

- (IBAction)btnZoomOutPressed
{
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    region.center.latitude = lati;
    region.center.longitude = longi;
    span.latitudeDelta=viewMapSingleVenue.region.span.latitudeDelta *2;
    span.longitudeDelta=viewMapSingleVenue.region.span.longitudeDelta *2;
    if(span.latitudeDelta < 200)
    {
    region.span=span;
    [viewMapSingleVenue setRegion:region animated:TRUE];
    }
}
Risposto il 20/02/2015 a 13:56
fonte dall'utente

voti
1

Io uso codice simile al tuo e sembra funzionare. Che cosa può accadere è che il delta non cambia abbastanza per causare il livello di zoom per aumentare da un livello di zoom di Google a quello successivo. Ciò dipenderà anche dallo stato iniziale della mappa, il che potrebbe spiegare il motivo per cui è intermittente - così come si fa a impostare la mappa e livello di zoom fino a cominciare, prima che l'utente preme il pulsante dello zoom?

Si potrebbe anche prendere in considerazione il metodo regionThatFits, che regolerà la regione che fornisci (nome è dalla memoria, come non ho la documentazione di mele a portata di mano).

Risposto il 23/06/2009 a 13:16
fonte dall'utente

voti
0

In Swift 4.2

let location = mapView.userLocation
let region = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpan(latitudeDelta: 50, longitudeDelta: 50))
mapView.setRegion(region, animated: true)
Risposto il 22/03/2019 a 12:02
fonte dall'utente

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