2009-12-09 72 views
2

我使用本教程整合MapKit給我的應用程序: http://iphonebcit.wordpress.com/iphone-map-kit-tutorial/iPhone MapKit - 更新註釋座標和地圖

CLLocationCoordinate2D coordinate; 
coordinate.latitude = 49.2802; 
coordinate.longitude = -123.1182; 

NSUInteger count = 1; 
for(int i = 0; i < 10; i++) { 
    CGFloat latDelta = rand()*.035/RAND_MAX - .02; 
    CGFloat longDelta = rand()*.03/RAND_MAX - .015; 

    CLLocationCoordinate2D newCoord = {coordinate.latitude+latDelta, coordinate.longitude+longDelta}; 
    MapDemoAnnotation* annotation = [[MapDemoAnnotation alloc] initWithCoordinate:newCoord andID:count++]; 
    [mapView addAnnotation:annotation]; 
    [annotation release]; 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapViewLocal viewForAnnotation:(id <MKAnnotation>)annotation { 
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapViewLocal dequeueReusableAnnotationViewWithIdentifier:@"Pin"]; 
if(pinView == nil) { 
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"]; 
    pinView.pinColor = MKPinAnnotationColorPurple; 
    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    pinView.animatesDrop = YES; 
    pinView.canShowCallout = YES; 
} else { 
    pinView.annotation = annotation; 
} 
return pinView; 

}

使引腳將設置隨機地圖。在我的應用程序中,座標將會改變。如何更改註釋的座標,以便它們在地圖上更新?

有誰知道嗎?

回答

2

在iPhone SDK 3.x中,您必須刪除引腳註釋並重新設置。如果你的地圖有很多註釋,那就不太好了。

我試圖讓它更好,所以我ony顯示/更新我的pin註釋在屏幕上。因此,如果用戶放大到紐約,舊金山將不會有註釋或其他用戶看不到的註釋。所以表現會好很多。

也許在將來這將是可能的。我希望如此:-)

1

該教程僅用於獲取地圖視圖以顯示,沒有別的。你將需要一個更大的教程。我發現這是有用的:通過地圖上的所有註釋

http://blog.objectgraph.com/index.php/2009/04/02/iphone-sdk-30-playing-with-map-kit/

可能就是你需要做的是循環,去除它們,然後通過你的數據數組循環,再次添加註釋。你可以更聰明一點,並循環訪問數據數組,檢查地圖上是否已經有一個與緯度/經度相同的pin,但這種方式會變得更加複雜。

+0

非常感謝。我實現的註釋協議有一個名爲locationId的額外實例變量。這個ID是唯一的,我知道哪個locationId應該用新的座標更新。 我怎樣才能輕鬆訪問它?我可以刪除註釋: 「註釋locationId = 3999刪除並添加新的座標?」 - 更好:「帶有locationId = 3999的註釋設置了新的座標,並且如果該針」更新了位置「。 – Tim 2009-12-10 09:16:45

+0

啊,我忘了。我已經看過這個教程,但這不能幫助我,因爲解決我的問題沒有什麼用處。 – Tim 2009-12-10 09:19:38

1

因爲我現在正在做註釋,所以我只做了一個快速測試。你會得到一個編譯器警告,所以它可能不被支持。但它的工作。

創建自定義MKAnnotation類,您可以設置座標屬性是可寫的:在任何事件然後

@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate; 

或間隔需要,改變座標使用這樣的事情:

CLLocation *loc=[[CLLocation alloc] initWithLatitude:55.0 longitude:17.0]; 
annotation.coordinate=loc.coordinate; 
[loc release];