0

我如何使用 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 的默认缩放控制器?

4

2 回答 2

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/复制了大部分内容)

于 2012-08-17T04:46:54.887 回答
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
于 2015-09-15T08:07:12.320 回答