2013-01-21 69 views
1

我的應用程序有一個MKMapView。這是iOS6。MKMapview ios在第一次加載應用程序時不顯示當前位置

-(void)viewDidLoad 
{ 
    ..... 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation]; 
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 
    [locationManager startUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    NSLog(@"Update locations is hit"); 
    NSLog(@"379 the locations count is %d",locations.count); 

    CLLocation *obj = [locations lastObject]; 
    NSLog(@"the lat is %f", obj.coordinate.latitude); 
    NSLog(@"the long is %f", obj.coordinate.longitude); 
    NSLog(@"the horizontal accuracy is %f",obj.horizontalAccuracy); 
    NSLog(@"the vertical accuracty is %f",obj.verticalAccuracy); 
    if (obj.coordinate.latitude != 0 && obj.coordinate.longitude != 0) 
    { 
     CLLocationCoordinate2D currrentCoordinates ; 
     currrentCoordinates.latitude = obj.coordinate.latitude; 
     currrentCoordinates.longitude = obj.coordinate.longitude; 
    } 
    ....more computation 
    [locationManager stopUpdatingLocation]; 
} 

當我第一次加載應用程序時,我的位置顯示很少。幾英里遠。我也有重置位置按鈕,如果我點擊該地圖顯示正確的位置。這是我在重置位置按鈕點擊:

- (IBAction)btnResetLocationClick:(UIButton *)sender 
{ 
    locationManager = [[CLLocationManager alloc]init]; 
    locationManager.delegate = self; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation]; 
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 
    [locationManager startUpdatingLocation]; 
} 

那麼我該如何使應用程序獲得正確的當前位置加載本身。有沒有辦法讓應用程序告訴地圖等待幾毫秒然後更新。還是有其他想法?請告訴我。如果您需要更多信息,請詢問。謝謝。

回答

2

你可以做的是:

  • didUpdateLocations關閉定位服務自動,而是;
  • 關閉定位服務在didUpdateLocations只有當你足夠滿意的horizontalAccuracy;和
  • 即使您沒有達到所需的準確度,在經過一段時間後關閉位置服務。

因此,didUpdateLocations可能看起來像:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *location = [locations lastObject]; 

    // do whatever you want with the location 

    // finally turn off location services if we're close enough 
    // 
    // I'm using 100m; maybe that's too far for you, but 5m is probably too small 
    // as you frequently never get that accurate of a location 

    if (location.horizontalAccuracy > 0 && location.horizontalAccuracy < 100) 
     [locationManager stopUpdatingLocation]; 
} 

然後在viewDidLoad,請關閉電源的一段時間過去之後(您可能要檢查您設置,如果你的一些狀態變量「VE已經關閉定位服務):

-(void)viewDidLoad 
{ 
    ..... 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation]; 
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 
    [locationManager startUpdatingLocation]; 

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 60.0 * NSEC_PER_SEC); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     [locationManager stopUpdatingLocation]; 
    }); 
} 

原創回答:

我看不到您要更新地圖的位置。我希望看到這樣的:

self.mapView.centerCoordinate = location.coordinate; 

或類似:

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location.coordinate, 300, 300); 
[self.mapView setRegion:region]; 

我也建議,而不是立即關閉位置服務(因爲頻繁的前幾個位置不準確),把它留在了一會兒,讓它磨練你的位置,直到horizontalAccuracyverticalAccuracy秋天某個預定的限制之內。看看那些準確數字爲以didUpdateLocations打了幾個電話,你就會明白我的意思。

我本來以爲你都拿到在這一點我建議實施didFailToLocateUserWithError因爲根據horizontalAccuracy,負horizontalAccuracy「負值表示該位置的經度和緯度是無效的。」希望你得到一個描述問題的錯誤。即使您目前沒有收到負號horizontalAccuracy,您也可能需要實施此方法,以確保您正確處理任何錯誤。

+0

謝謝。那麼我採取的座標,並將它們傳遞給一個方法,我形成一個MKMapView並設置其座標。我還記錄了垂直和水平精度。他們一致地出來-1和5。我正在調試,並且我在didUpdateLocations中放置了一個斷點,並且-1和5保持了日誌記錄。如果兩個精度都大於0? – RookieAppler

+0

@ Rob.What我可以做些什麼使他們大於0. – RookieAppler

+0

@Phanindar水平(經度和長期的準確性)是否定的?還是垂直(高度)爲負?你可能不關心後者(雖然我不確定它是如何得到高度,如果它不知道你在哪裏),但前者是非常重要的。我猜你只是沒有獲得一個好的GPS信號(因此我建議嘗試開啓wifi,或者如果你開啓了它,關掉它)。 – Rob

0

您無法使iPhone中的GPS在您的應用程序中更加準確,但您可以在繼續之前檢查結果是否準確。現在你只是檢查經緯度,長不是0,但如果你檢查obj的水平準確度,那麼你就會知道位置信息何時足夠好。在這種情況發生之前不要stopUpdatingLoation

相關問題