2010-01-18 27 views
4

我正在研究一些iPhone MapKit代碼,當地圖邊界發生變化(基本上是對外部服務器的某些查詢)時會觸發一些事件。有一個邊緣案例我似乎無法解決。當用戶輸入新地址時,我使用setRegion放大該位置,然後計算地圖的邊界並用這些座標查詢我的外部服務器。我總是打電話給setRegion,但是有一個邊緣案例,它不會觸發。也就是說,當用戶奇蹟般地選擇位於當前地圖中心的完全相同的地址時,該地圖被放大到完全相同的區域。當然,這可能是罕見的,但它很容易實現用下面的代碼:我如何知道什麼時候MKMapView上的setregion不會觸發regionWillChange/regionDidChange

CLLocation *centerOfMap = [[CLLocation alloc] initWithLatitude:mapView.centerCoordinate.latitude longitude:mapView.centerCoordinate.longitude]; 
mySearchLocation.searchLocation = centerOfMap; 

//Recenter and zoom map in on search location 
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}}; 
region.center = mySearchLocation.searchLocation.coordinate;region.span.longitudeDelta = 0.01f; 
region.span.latitudeDelta = 0.01f; 
[self.mapView setRegion:region animated:YES]; 

這將允許用戶在當前地圖的中心放置圖釘和斷火查詢到相當於我基於此的服務器。不幸的是,因爲代碼邏輯取決於區域的變化(我已經這樣做了,用戶可以輕鬆地在地圖上移動,而不必明確地按下刷新按鈕),我需要一個解決方法。如何知道setRegion何時被調用,但不會觸發regionWillChange/regionDidChange。我可以重寫這個函數嗎?

編輯: 正如我在第一個答案評論,我想檢測時,該區域將不被使用下面的代碼更改:

//Recenter and zoom map in on search location 
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}}; 
region.center = mySearchLocation.searchLocation.coordinate; 
region.span.longitudeDelta = 0.01f; 
region.span.latitudeDelta = 0.01f; 
region = [mapView regionThatFits:region]; //Not sure if this is necessary 

NSLog(@"diff in latitude of center %f", (mapView.region.center.latitude - region.center.latitude)); 
NSLog(@"diff in longitude of center %f", (mapView.region.center.longitude - region.center.longitude)); 
NSLog(@"diff in latitude span %f", (mapView.region.span.latitudeDelta - region.span.latitudeDelta)); 
NSLog(@"diff in longitude span %f", (mapView.region.span.longitudeDelta - region.span.longitudeDelta)); 

[self.mapView setRegion:region animated:YES]; 

不幸的是,當我設置searchLocation爲中心當前地圖我得到如下結果:

diff in latitude of center 0.000000 
diff in longitude of center 0.000016 
diff in latitude span -0.000000 
diff in longitude span 0.000000 

在不同情況下的錯誤略有不同,但總體上說,即使該地區是這樣setRegion或morever,regionDidChange,不火略有不同。誰能解釋爲什麼?或者我可以怎樣去正確地檢測這個。注意:我也嘗試過使用mapView.centerCoordinate,但是我得到了類似的錯誤,我只想知道區域何時不會改變(如果centerCoordinate相同,縮放級別/區域仍然可以不同)。

回答

0

爲什麼不直接調用regionWill/Did設置新地圖位置的代碼是否手動更改,如果它檢測到它將設置地圖的位置將成爲當前中心?

+0

這正是我想這樣做,但有錯誤的一個奇怪的不明原因的餘量,使他們永遠不會完全相等。然後,我繼續嘗試檢測(我自己)該區域何時會發生變化(在調用setRegion之前),並且儘管有相同的奇怪誤差範圍(意味着區域不相同),setRegion也不會觸發。我將使用代碼編輯原始帖子。更多的想法讚賞! – deadroxy 2010-01-18 22:45:57

+0

任何人有任何其他建議,或者我應該向蘋果公司提交錯誤? – deadroxy 2010-01-21 11:34:41

+0

對不起,沒有看到你的其他評論 - 我認爲你應該提交一個錯誤(提示,蘋果將要求一個示例項目,所以你現在可以建立一個)。然而,一種解決方法是檢測到您正在將中心設置爲一個新的中心,並且在一定的誤差範圍內,並自己調用您的回調...您可以繼承MKMapView以覆蓋setCenter並在您調用super.setCenter之前進行檢測。 – 2010-01-21 18:00:42

0

這是一個黑客入侵的解決方法,似乎對我很好。

MKCoordinateRegion currentRegion = mapView.region; 

// Capture the instances where setRegion will not be fired 
BOOL regionLatitudeSame = (int)(currentRegion.center.latitude*1000000) == (int)(region.center.latitude*1000000); 
BOOL regionLongitudeSame = (int)(currentRegion.center.longitude*1000000) == (int)(region.center.longitude*1000000); 
BOOL regionSame = regionLatitudeSame && regionLongitudeSame; 
if (regionSame) 
{ 
    // Safe to assume that regionWillChange/regionDidChange WON'T be called 
} 

正如你可能已經注意到,我所做的假設,即第六小數點後的精度是太微不足道(up to 11 mm)在地圖上時所需的視覺變化(即使在其最高縮放級別)。

0

下面的代碼爲我工作:

let predictRect = mapView.convertRegion(mapView.regionThatFits(THE_REGION_YOU_WANT_TO_SET), toRectTo: mapView) 

    if predictRect.origin.x.rounded() == 0 && predictRect.origin.y.rounded() == 0 
     && predictRect.size.width.rounded() == mapView.bounds.width 
     && predictRect.size.height.rounded() == mapView.bounds.height 
    { 
     debugPrint("setRegion same, will not trigger region change event") 
    } 
相關問題