快速瞭解我正在進行的操作。 UIMapView加載並顯示單個註釋(從不超過一個)。在菜單欄上,有一個「我的我」按鈕,點擊時找到userLocation並顯示爲第二個註釋。然後,我將MapView縮小以適應範圍內的這些註釋,但我無法找到合適的方法。根據第一個註釋與用戶位置相關的位置,有時它不會縮小到足夠大。什麼是縮小和適合MapKit中所有註釋的最佳方式
例如,如果註釋是用戶的西北部,則會放大。但是,如果註釋位於用戶的東南方,則它只能縮小到足以讓它們被切斷,而且必須手動縮小一點。這是我正在使用的代碼,我在SO上找到的其他一些代碼的變體。
CLLocation *whereIAm = mapView.userLocation.location;
float lat = whereIAm.coordinate.latitude;
float lon = whereIAm.coordinate.longitude;
CLLocationCoordinate2D southWest = {[currentLatitude floatValue], [currentLongitude floatValue]};
CLLocationCoordinate2D northEast = southWest;
southWest.latitude = MIN(southWest.latitude, lat);
southWest.longitude = MIN(southWest.longitude, lon);
northEast.latitude = MAX(northEast.latitude, lat);
northEast.longitude = MAX(northEast.longitude, lon);
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];
MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude)/2.0;
region.center.longitude = (southWest.longitude + northEast.longitude)/2.0;
region.span.latitudeDelta = meters/111319.5
region.span.longitudeDelta = 7.0;
MKCoordinateRegion savedRegion = [mapView regionThatFits:region];
[mapView setRegion:savedRegion animated:YES];
[locSouthWest release];
[locNorthEast release];
有內置MapKit一個更好的辦法只有縮小,這樣既註解有,可以說它們之間的半英寸的外框?我並不在乎用戶是否需要放大,我只是想讓它縮小。
嗯,我確實嘗試過這一點,並沒有像預期的那樣工作。我有同樣的結果。儘管如此,我會盡快給它一次,然後再看一次。我會回報。 – 2010-11-12 22:08:51
「意料之中」是什麼意思?你有什麼問題? – 2010-11-12 22:20:57
剛剛實施,它比我所做的更好,所以我會堅持下去。現在唯一被切斷的是標註,我可以通過在動畫之後進行回放來解決這個問題。謝謝你的幫助。 – 2010-11-12 22:23:52