user4277177
user4277177

Reputation:

mapkit zoom to maximum scale objective c

I am using mapkit .I have developed simple storyboard application .

1-The mapkit should zoom to maximum scale to show user location on loading mapkit.

2-On clicking loc.png the map should load the description of location with title and subtitle and detail about location

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"testAnnotationView"];
    if(annotationView == nil){
        annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"testAnnotationView"];
        annotationView.image = [UIImage imageNamed:@"loc.png"];
        annotationView.canShowCallout = true;
    }

    return annotationView;
}

How i can accomplish these task?From this link you can download sample project.https://drive.google.com/file/d/0B5pNDpbvZ8SnRExkamtmdkwzeWc/view?usp=sharing

Upvotes: 2

Views: 944

Answers (2)

James Bush
James Bush

Reputation: 1525

Here's code I wrote for zooming an WKInterfaceMap (WatchKit) in and out no less than its minimum (0.0) and no greater than its maximum (the width/height of the world map)—and, that, on an curve that accelerates the zoom increase the farther out you go and decelerates the zoom decrease the closer in you go:

- (void)crownDidRotate:(WKCrownSequencer *)crownSequencer rotationalDelta:(double)rotationalDelta
{    
    span.latitudeDelta  += ((rotationalDelta * rotationalDelta) * (rotationalDelta)) + (span.latitudeDelta * rotationalDelta);
    span.longitudeDelta += ((rotationalDelta * rotationalDelta) * (rotationalDelta)) + (span.longitudeDelta * rotationalDelta);
    span.latitudeDelta   = (span.latitudeDelta < 0) ? 0  : (span.latitudeDelta  > MKCoordinateRegionForMapRect(MKMapRectWorld).span.latitudeDelta)  ? MKCoordinateRegionForMapRect(MKMapRectWorld).span.latitudeDelta  : span.latitudeDelta;
    span.longitudeDelta  = (span.longitudeDelta < 0) ? 0 : (span.longitudeDelta > MKCoordinateRegionForMapRect(MKMapRectWorld).span.longitudeDelta) ? MKCoordinateRegionForMapRect(MKMapRectWorld).span.longitudeDelta : span.longitudeDelta;
    MKCoordinateRegion visibleRegion = MKCoordinateRegionMake(PlanetaryHourDataSource.sharedDataSource.locationManager.location.coordinate, span);
    [self.map setRegion:visibleRegion];
}

Not only does this prevent invalid regions (and indicates to the user when the maximum and minimum zoom levels are reached), but it makes sure that the zoom slows down when the map shows more detail, and vice versa:

<iframe src="https://player.vimeo.com/video/313729712" width="640" height="1384" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>

The acceleration curve looks like this:

enter image description here

The above answers provide arbitrary values, which will not necessarily correspond to either end of the spectrum.

Upvotes: 0

Reinier Melian
Reinier Melian

Reputation: 20804

Use this extension of mapKit, and adjust the values as you need, if the values is lower the the zoom is greater

EDITED

OBJECTIVE-C

.h

#import <MapKit/MapKit.h>

@interface MKMapView (Zoom)

-(void)zoomToUserLocation;

-(void)zoomToUserLocationWith:(CLLocationCoordinate2D)coordinate and:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters;

-(void)zoomToUserLocationWith:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters;

@end

.m

#import "MKMapView+Zoom.h"

@implementation MKMapView (Zoom)

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)zoomToUserLocation
{
    [self zoomToUserLocationWith:1000 and:1000];
}

-(void)zoomToUserLocationWith:(CLLocationCoordinate2D)coordinate and:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters
{
    [self setRegion:MKCoordinateRegionMakeWithDistance(coordinate, latitudinalMeters, longitudinalMeters)];
}

-(void)zoomToUserLocationWith:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters
{
    if(self.userLocation.location != nil){
        [self zoomToUserLocationWith:self.userLocation.location.coordinate and:latitudinalMeters and:longitudinalMeters];
    }
}
@end

use it

[self.mapView zoomToUserLocation];

or

[self.mapView zoomToUserLocationWith:50 and:50];

or you can use it in

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    [mapView zoomToUserLocationWith:view.annotation.coordinate and:500 and:500];
}

SWIFT

  extension MKMapView {
  func zoomToUserLocation() {
     self.zoomToUserLocation(latitudinalMeters: 1000, longitudinalMeters: 1000)
  }

  func zoomToUserLocation(latitudinalMeters:CLLocationDistance,longitudinalMeters:CLLocationDistance)
  {
    guard let coordinate = userLocation.location?.coordinate else { return }
    let region = MKCoordinateRegionMakeWithDistance(coordinate, latitudinalMeters, longitudinalMeters)
    setRegion(region, animated: true)
  }

}

Use it

mapView.zoomToUserLocation()

or

mapView.zoomToUserLocation(latitudinalMeters:50,longitudinalMeters:50)

Hope this helps

Upvotes: 1

Related Questions