2012-03-30 75 views
1

我在我的應用程序的首頁上顯示地圖視圖,我使用CLLocation從當前位置設置區域和座標。 當應用程序運行時,藍圖正顯示在地圖視圖上,當我打開其子視圖並返回到主屏幕時,地圖將顯示完美的位置和縮放級別。 下面的代碼導致藍屏是否有任何錯誤?使用當前位置縮放MapView的問題

- (void)viewWillAppear:(BOOL)animated { 
    CLLocationManager *lm = [[CLLocationManager alloc] init]; 
    lm.delegate = self; 
    lm.desiredAccuracy = kCLLocationAccuracyBest; 
    lm.distanceFilter = kCLDistanceFilterNone; 
    [lm startUpdatingLocation]; 

    CLLocation *location = [lm location]; 

    CLLocationCoordinate2D coord; 


    coord = [location coordinate]; 

    CLLocationCoordinate2D zoomLocation; 
    zoomLocation.latitude = coord.latitude; //39.281516; 
    zoomLocation.longitude= coord.longitude; //-76.580806; 

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE); 

    MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];     

    [self.mainMapView setRegion:adjustedRegion animated:YES];  
} 

回答

4

我猜你會看到藍色,因爲位置是(0,0),它位於大海中間。可能因爲第一次,CLLocationManager的位置尚未設置。

您應該檢查是否還有位置,如果沒有,請等待CLLocationManagerDelegate方法locationManager:didUpdateToLocation:fromLocation:中的回調,然後將地圖居中放置在所需位置。

事情是這樣的:

... 
@property (nonatomic, assign) BOOL needsCentering 
... 

@synthesize needsCentering = _needsCentering; 

- (void)viewWillAppear:(BOOL)animated { 
    CLLocationManager *lm = [[CLLocationManager alloc] init]; 
    lm.delegate = self; 
    lm.desiredAccuracy = kCLLocationAccuracyBest; 
    lm.distanceFilter = kCLDistanceFilterNone; 
    [lm startUpdatingLocation]; 

    CLLocation *location = [lm location]; 

    if (!location) { 
     _needsCentering = YES; 
    } else { 
     _needsCentering = NO; 

     CLLocationCoordinate2D coord; 

     coord = [location coordinate]; 

     CLLocationCoordinate2D zoomLocation; 
     zoomLocation.latitude = coord.latitude; //39.281516; 
     zoomLocation.longitude= coord.longitude; //-76.580806; 

     MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE); 

     MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];     

     [self.mainMapView setRegion:adjustedRegion animated:YES]; 
    } 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    if (_needsCentering) { 
     _needsCentering = NO; 

     CLLocationCoordinate2D coord; 

     coord = [newLocation coordinate]; 

     CLLocationCoordinate2D zoomLocation; 
     zoomLocation.latitude = coord.latitude; //39.281516; 
     zoomLocation.longitude= coord.longitude; //-76.580806; 

     MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE); 

     MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];     

     [self.mainMapView setRegion:adjustedRegion animated:YES]; 
    } 
} 

但同時,你可能想看看的MKMapViewshowsUserLocation

+0

不錯,你能幫我實施這個委託方法嗎 – Firdous 2012-03-30 09:15:39

+0

@Firdous - 增加了一個例子。 – mattjgalloway 2012-03-30 09:53:50

+0

我的locationManager:didUpdateToLocation:fromLocation:不會被調用,即使當我在viewDidLoad中設置mapView.delegate = self。雖然藍屏並未到來,問題似乎已解決,但我沒有看到來自此委託方法的日誌中的nslog消息 – Firdous 2012-03-30 12:16:04