2011-09-23 72 views
11

我似乎有許多相關的問題,這裏發佈的有關MKMapView的許多人。而不是不能得到它放大和顯示當前的位置,我似乎無法得到它停止這樣做。這裏是發生了什麼:防止MKMapView不斷重新縮放和重新定位到用戶位置

  • 我啓動的應用程序
  • 的的MKMapView顯示了我的位置,並以藍點
  • 我用我的手指
  • 幾秒鐘後,的MKMapView縮小和遠在地圖上突然放大早在到中心對我的當前位置再次

我試圖告訴我的CLLocationManagerstopUpdatingLocation(沒有任何影響,因爲一個MKMapView知道如何使用CoreLocation),我試過告訴MKMapViewsetShowsUserLocation:NO(根本不顯示藍點,這不是我想要的)。我甚至試圖消除我的CLLocationManager(沒有效果)。是什麼原因導致這種情況,我該如何阻止它?


是的,我做的設置位置管理的精度和距離-loadView。我不執行-mapViewDidFinishLoadingMap:。這是我實現

-locationManager:didUpdateToLocation:fromLocation: 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    // How many seconds ago was this new location created? 
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow]; 

    // CLLocationManagers will return the last found location of the device first, 
    // you don't want that data in this case. If this location was made more than 3 
    // minutes ago, ignore it. 
    if (t < -180) 
    { 
     // This is cached data, you don't want it, keep looking 
     return; 
    } 

    [locationManager stopUpdatingLocation]; 
} 

我覺得這是定心你問一下,@Anna·卡列尼娜:

- (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)u 
{ 
    CLLocationCoordinate2D loc = [u coordinate]; 
    // Display the region 500 meters square around the current location 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500); 
    [mv setRegion:region animated:YES]; 
} 
+0

你在做什麼?在'mapView:didUpdateUserLocation:'?你可以發佈該代碼嗎? – Anna

+0

每次用戶位置更改或獲取新讀數時,都會調用didUpdateUserLocation方法。您可以添加一個bool ivar,您可以在該方法中進行檢查/設置,以便只進行一次對中_OR_,您可以根據u.location.horizo​​ntalAccuracy或u.location.timestamp有條件地進行居中。 – Anna

+0

好的,這是有道理的,但是...我現在有-mapView:didUpdateUserLocation:有條件地集中,如果u.location.horizo​​ntalAccuracy> locationManager.desiredAccuracy,它仍然在發生。如果我扭轉了這種情況,我沒有獲得初始縮放。 – DJHoward

回答

0

首先,我不知道這會影響很多事情,但做您設置位置管理者精度和距離濾波器viewDidLoad在所有(或任何你要實現你的LocationManager):

[locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
[locationManager setDistanceFilter:kCLDistanceFilterNone]; 

其次,檢查y中這兩個函數我們的源(無視我在這裏列出的實際職能的內容,它只是爲了說明和展示使用),如果可能的話,這些功能爲您的代碼,以便我們更好地幫助:

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView 
{ 
    [activityIndicator stopAnimating]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSDate* eventDate = newLocation.timestamp; 
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
    if (abs(howRecent) < 15.0) 
    { 
     NSLog(@"New Latitude %+.6f, Longitude %+.6f", newLocation.coordinate.latitude, newLocation.coordinate.longitude); 
    } 
} 
4

不執行​​方法。

改爲使用locationmanager didUpdateToLocation

didUpdateUserLocation被無數次調用,其中didUpdateToLocation在發生任何位置更改時被調用。

然後你可以手動設置didUpdateToLocation方法中的mkmapview區域。

0

我在MKMapViewDelegate方法中使用dispatch_once在開始只放大一次。這是代碼。

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {  
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     CLLocationCoordinate2D newCoordinate = CLLocationCoordinate2DMake(mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude); 

     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newCoordinate, 500, 500); 
     [mapView setRegion:region animated:YES]; 
    }); 
}