如何在MapView中将缩放级别设置为用户位置

时间:2013-10-21 07:29:17

标签: ios ios7 mkmapview

我使用MKMapView获取用户位置,当视图加载时,它显示整个世界视图。无论如何我可以设置缩放级别,这样用户就不必一直放大以获得城市景观或街景......这将是一个很大的帮助。我在下面发布了我的代码供参考......

- (void)viewDidLoad
{

    [super viewDidLoad];
    [ self.mapView.delegate self];
    [self.mapView setShowsUserLocation:YES];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{

    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
    [self.mapView setRegion:region animated:YES];


    }

- (void)viewDidUnload {
    [super viewDidUnload];
    [_mapView setShowsUserLocation:NO];
}

5 个答案:

答案 0 :(得分:2)

您发布的代码应该适用于viewDidLoad中此关键行的以外的大部分内容:

[ self.mapView.delegate self];

基本上,这条线没有任何作用 设置地图视图的delegate属性(我认为应该这样做)。

实际上做的是self属性上调用 delegate

地图视图的delegate未设置(它保持nil),因此永远不会调用didUpdateUserLocation委托方法,因此地图不会缩放到用户的位置。< / p>


这条线应该是这样的:

[self.mapView setDelegate:self];

甚至更简单:

self.mapView.delegate = self;


请注意,在iOS 5或更高版本中,您只需设置userTrackingMode,地图视图就会自动缩放并跟随用户的位置,因此您无需手动执行此操作。


另请注意,自iOS 6起,viewDidUnload已被弃用,甚至不被操作系统调用。您可能希望将showsUserLocation的禁用移至viewWillDisappear(并将启用移至viewWillAppear)。

答案 1 :(得分:0)

试试这可能是你的全部帮助

MKCoordinateRegion startupRegion;
        startupRegion.center = CLLocationCoordinate2DMake(26.0000, 75.00);
        startupRegion.span = MKCoordinateSpanMake(0.5, 0.597129);
        [mapViewObj setRegion:startupRegion animated:YES];

这里26.0000和75.00是用户位置,MKCoordinateSpanMake是缩放级别。它可以根据需要增加缩放。

答案 2 :(得分:0)

在ViewDidLoad

中试试这个
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516; // your latitude value
zoomLocation.longitude= -76.580806; // your longitude value
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.18; // change as per your zoom level
span.longitudeDelta=0.18;    
region.span=span;
region.center= zoomLocation;
[self.mapView setRegion:region animated:TRUE];
[self.mapView regionThatFits:region];

答案 3 :(得分:0)

根据您的要求设置MKCoordinateRegion并更改区域范围

    MKCoordinateRegion region;
        CLLocation* currentLocation = (CLLocation*)from ; // current location
        if(currentLocation.coordinate.latitude > maxLat)
            maxLat = currentLocation.coordinate.latitude;
        if(currentLocation.coordinate.latitude < minLat)
            minLat = currentLocation.coordinate.latitude;
        if(currentLocation.coordinate.longitude > maxLon)
            maxLon = currentLocation.coordinate.longitude;
        if(currentLocation.coordinate.longitude < minLon)
            minLon = currentLocation.coordinate.longitude;

        region.center.latitude     = (maxLat + minLat) / 2;
        region.center.longitude    = (maxLon + minLon) / 2;
        region.span.latitudeDelta  = 0.4; // change as per required zoom level
        region.span.longitudeDelta = 0.4; // change as per required zoom level

//输入该区域并将地图区域设置为30000以下

   _mapView.region =MKCoordinateRegionMakeWithDistance(currentLocation.coordinate,30000, 30000);
[_mapView setRegion:region animated:YES];
    [_mapView regionThatFits:region];
    [_mapView reloadInputViews];

//将地图置于当前位置

    _mapView.centerCoordinate = _mapView.userLocation.location.coordinate;

//获取当前位置使用CLLocationManagerDelegate

    @interface GetCurrentLocation : NSObject<CLLocationManagerDelegate>
    @property(nonatomic,strong)CLLocationManager *_locationManager;
    -(void)getLocation;
    @end

-(void)getLocation{

       _locationManager = [[CLLocationManager alloc] init];
       [_locationManager setDelegate:self];
       [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
       _locationManager.distanceFilter = kCLDistanceFilterNone;
       [_locationManager startUpdatingLocation];

      }

       - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation// you can get current location here 
      {
       CLLocationCoordinate2D coord;

       coord.latitude = newLocation.coordinate.latitude;
       coord.longitude = newLocation.coordinate.longitude;

      }
      - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
      {
           // NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);

      }

答案 4 :(得分:0)

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{
    MKCoordinateRegion mapRegion;   
    mapRegion.center = mapView.userLocation.coordinate;
    mapRegion.span.latitudeDelta = 0.3;
    mapRegion.span.longitudeDelta = 0.3;
    [mapView setRegion:mapRegion animated: YES];
}
相关问题