最近做了個地圖軟體,寫一些經驗和心得,以及一些問題
管理提醒: 本帖被 gagaga 設定為精華(2010-04-08)
這個軟體的源碼在此:
http://www.cocoachina.com/bbs/read.php?tid=19117
1,看到很多人問如何計算兩點之間的距離,其實很簡單哎。
準備兩個CLLocation的對象,比如要計算某個位置與使用者當前位置的距離,則其中一個CLLocation是userLocation = [locationManager location],locationManager是CLLocationManager的執行個體,並已執行[locationManager startUpdatingLocation];
然後計算這兩個CLLocation的距離(已格式化成12.34 km):
複製代碼
- [NSString stringWithFormat:@"%0.2f km",[userLocation getDistanceFrom:location]/1000]
|
2,在處理MKAnnotationView時,都要判斷對應annotation是不是MKUserLocation這個顯示使用者當前位置的藍點,以避免誤操作。
複製代碼
- [annotation isKindOfClass:[MKUserLocation class]]
|
3,- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
這個delegate函數一般會在給MKMapView對象添加annotations後馬上執行,但執行不會馬上結束。如果在它完成之前就調用
[mapView viewForAnnotation:someAnnotation];的話,會返回nil的結果,這時對這個返回的nil做任何操作都不會在螢幕上顯示……
4,MKMapView放大縮小時,需要注意的是放大,至少放大2倍以上才會被執行。
複製代碼
- - (IBAction)doZoomIn:(id)sender{//放大
- MKCoordinateRegion region = mMapView.region;
- region.span.latitudeDelta=region.span.latitudeDelta * 0.4;
- region.span.longitudeDelta=region.span.longitudeDelta * 0.4;
- [mapView setRegion:region animated:YES];
- }
- - (IBAction)doZoomOut:(id)sender{//縮小
- MKCoordinateRegion region = mMapView.region;
- region.span.latitudeDelta=region.span.latitudeDelta * 1.3;
- region.span.longitudeDelta=region.span.longitudeDelta * 1.3;
- [mapView setRegion:region animated:YES];
- }
|
5,下面隨便列一些其他沒提到的代碼,供搜尋引擎搜尋
複製代碼
- CLLocationCoordinate2D coordinate;
- //Location Paris
- coordinate.latitude = 48.856660;
- coordinate.longitude = 2.350996;
- MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, distance, distance);
- MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
- [mapView setRegion:adjustedRegion animated:animated];
|
6,問題:
發現MKAnnotationView的Callout View有bug。
我在後台更新subtitle後,若不手動點一下其他MKAnnotationView再點回之前已經顯示Calloutview的MKAnnotationView的話,那這個MKAnnotationView的Calloutview裡的subtitle就不會被更新……
說清楚點就是:點了某個圖釘顯示資訊後,若這時後台更新了它的subtitle,然後你再點這個圖釘會發現它的subtitle顯示成了一個空白。這時得點一下其他圖釘,再點回來,原來那個圖釘的資訊才是完整的。
雖然有個notification是MKAnnotationCalloutInfoDidChangeNotification,但SDK手冊裡說這個已經不能用了。我嘗試用了一下也沒有任何效果……
不知道還有其他方法沒
後面同學回答在此
引用 引用第22樓cjlin於2010-06-02 23:45發表的 :
回一下第6個問題...
如果用戶選上的是mkav,那...
if(mkav.selected)
{
[mkav setSelected:NO];
[mkav setSelected:YES];
}
這樣就會更新subtitle了[url=job.php?action=topost&tid=17969&pid=138830][/url]