我創建具有多個註釋的地圖,但我不知道如何動態(我有他們在CoreData中)。如何動態放置地圖註釋?
我puting他們固定的驗證碼
- (void)viewDidLoad {
[super viewDidLoad];
CLLocation *userLoc = mapView.userLocation.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
NSLog(@"user latitude = %f",userCoordinate.latitude);
NSLog(@"user longitude = %f",userCoordinate.longitude);
mapView.delegate=self;
NSMutableArray* annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = 43.82078;
theCoordinate1.longitude = 15.307265;
CLLocationCoordinate2D theCoordinate2;
theCoordinate2.latitude = 44.91035917;
theCoordinate2.longitude = 14.65576172;
MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
[email protected]"Rohan";
[email protected]"in the city";
MyAnnotation* myAnnotation2=[[MyAnnotation alloc] init];
myAnnotation2.coordinate=theCoordinate2;
[email protected]"Vaibhav";
[email protected]"on a Bridge";
[mapView addAnnotation:myAnnotation1];
[mapView addAnnotation:myAnnotation2];
[annotations addObject:myAnnotation1];
[annotations addObject:myAnnotation2];
NSLog(@"%d",[annotations count]);
//[self gotoLocation];//to catch perticular area on screen
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Walk the list of overlays and annotations and create a MKMapRect that
// bounds all of them and store it into flyTo.
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
NSLog(@"fly to on");
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo)) {
flyTo = pointRect;
} else {
flyTo = MKMapRectUnion(flyTo, pointRect);
//NSLog(@"else-%@",annotationPoint.x);
}
}
// Position the map so that all overlays and annotations are visible on screen.
mapView.visibleMapRect = flyTo;
UIBarButtonItem* temp=[[UIBarButtonItem alloc] init];
[email protected]"Back";
self.navigationItem.backBarButtonItem=temp;
//[temp release];
}
附:如果您需要額外的代碼,請告訴。
感謝對幫助
UPDATE:
Locations.h
...
@interface Locations : NSManagedObject
@property (nonatomic, retain) NSDecimalNumber * lat;
@property (nonatomic, retain) NSDecimalNumber * lon;
...
更新2:
這裏就是我puting註釋示例代碼「固定」。
http://dl.dropbox.com/u/77033905/stackoverflow/Map.zip
我怎樣才能在陣列添加此註解?定義這個位置(座標,標題,subitle陣列,並將它們添加到地圖。)
你的動態是什麼意思? –
在PHP中,我把它們放在這裏:$ array [] = $ annotation;等等。 – CroiOS
還是不知道。讓我知道你到底想做什麼。 –