4

Im using MKMapView to get the users location and when the view loads it shows the whole world view. Is there anyway that I can set a zoom level so that the users don't have to keep zooming in always to get the city view or street view...It would be a great help. I've posted my code below for reference...

- (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];
}
4

5 回答 5

2

The code you've posted should work for the most part except for this critical line in viewDidLoad:

[ self.mapView.delegate self];

Essentially, this line does nothing.
It is not setting the map view's delegate property (which is what I assume it is supposed to do).

What it is actually doing is calling self on the delegate property.

The map view's delegate is not getting set (it stays nil) and so the didUpdateUserLocation delegate method never gets called and so the map is not zooming to the user's location.


The line should be this:

[self.mapView setDelegate:self];

or even simpler:

self.mapView.delegate = self;


Note that in iOS 5 or later, you can just set userTrackingMode and the map view will automatically zoom to and follow the user's location so you don't have to do it manually.


Also note that since iOS 6, viewDidUnload is deprecated and is not even called by the OS. You probably want to move the disabling of showsUserLocation to viewWillDisappear (and move the enabling to viewWillAppear).

于 2013-10-21T12:51:11.400 回答
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];
于 2013-10-21T08:37:25.107 回答
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 是缩放级别。它会随心所欲地增加变焦。

于 2013-10-21T07:44:02.690 回答
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);

      }
于 2013-10-21T09:07:21.487 回答
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];
}
于 2016-07-15T12:15:19.627 回答