我如何使用UISlider在iphone中更改MKMapView zoomIn和ZoomOut?

时间:2012-08-17 04:32:32

标签: iphone mkmapview uislider

如何使用UISlider更改MKMapView的缩放值。

我试过这段代码,但作品并不完美,

- (IBAction)slideAction:(id)sender 
   {


    span.latitudeDelta = 125*(1-slideValue.value)+0.01;
    span.longitudeDelta = 0.001;
    region.span = span;
    region.center=map.centerCoordinate;
    [map setRegion:region animated:TRUE];

   }

有什么建议吗?我认为它们不是MKMap的默认缩放控制器吗?

2 个答案:

答案 0 :(得分:0)

请注意,您的longitudeDelta需要更改,否则可能无法缩放。

您可能会觉得这很有用:https://github.com/calabash/calabash-ios-server/blob/master/calabash/Classes/MapKit/MKMapView%2BZoomLevel.m

(我从http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/复制了大部分内容)

答案 1 :(得分:0)

//
//  ViewController.m
//  MapKitRegion

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>

@interface ViewController () <MKMapViewDelegate>

@property (strong, nonatomic) IBOutlet UITextView *txtAddress;
@property (strong, nonatomic) IBOutlet UIButton *btnGetMap;
@property (strong, nonatomic) IBOutlet UISegmentedControl *segType;
@property (strong, nonatomic) IBOutlet UISlider *slideZoom;
@property (strong, nonatomic) IBOutlet MKMapView *mapView;

@property CLLocationCoordinate2D coord;

- (IBAction)btnGetMapTouched:(id)sender;
- (IBAction)segTypeChanged:(id)sender;
- (IBAction)slideZoomChanged:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _mapView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)btnGetMapTouched:(id)sender {
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:self.txtAddress.text
                 completionHandler:^(NSArray *placemarks, NSError *error) {
                     if (error) {
                         NSLog(@"Geocode failed with error: %@", error);
                         return;
                     }
                     if(placemarks && placemarks.count > 0) {
                         CLPlacemark *placemark = placemarks[0];
                         _coord = placemark.location.coordinate;
                         _mapView.centerCoordinate = _coord;

                        [self SetZoom];
                     }
                 }];
}

- (IBAction)segTypeChanged:(id)sender {
    if (self.segType.selectedSegmentIndex == 0)
        _mapView.mapType = MKMapTypeStandard;
    else
        _mapView.mapType = MKMapTypeSatellite;
}

- (IBAction)slideZoomChanged:(id)sender {
    [self SetZoom];
}

- (void) SetZoom {
    int meters = self.slideZoom.value * 30000;
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (_coord, meters, meters);

    [_mapView setRegion:region animated:NO];
}

@end