我需要在我的MKMapView上添加幾個註釋(一個國家/地區的每個城市的示例),而且我不想初始化每個CLLocation2D的所有城市的經度和緯度。我認爲這是可能的排列,所以這是我的代碼:在NSMapray座標上添加MKAnnotations
NSArray *latit = [[NSArray alloc] initWithObjects:@"20", nil]; // <--- I cannot add more than ONE object
NSArray *longit = [[NSArray alloc] initWithObjects:@"20", nil]; // <--- I cannot add more than ONE object
// I want to avoid code like location1.latitude, location2.latitude,... locationN.latitude...
CLLocationCoordinate2D location;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
for (int i=0; i<[latit count]; i++) {
double lat = [[latit objectAtIndex:i] doubleValue];
double lon = [[longit objectAtIndex:i] doubleValue];
location.latitude = lat;
location.longitude = lon;
annotation.coordinate = location;
[map addAnnotation: annotation];
}
好了,所有的權利,如果我離開一個對象在NSArrays latit和longit,我在地圖上一個註釋;但是如果我向數組應用程序構建添加多個對象,但會使用EXC_BAD_ACCESS(代碼= 1 ...)崩潰。有什麼問題,或者什麼是在沒有冗餘代碼的情況下添加多個註釋的最佳方式?謝謝!
絕對優雅,和漂亮的解釋。我只需要添加CoreLocation框架,你的代碼是完美的。謝謝! – Huxley