7 Best Astrology Apps for Accurate Horoscopes
Millions of people across the globe turn to astrology for guidance in love, career, health, and personal growth. A survey by Pew Research Center found that 29% of Americans believe […]
We use Maps to find places and directions. The MapKit framework makes it easy for developers to create custom annotation view to implement applications which can make use of the maps in the applications. To create a custom annotation view over map view using Mapkit framework just follow these simple steps.
Step 1- While adding annotation don’t give its title and subtitle. Just add it with Latitude / Longitude and take an integer variable that starts with 100(so that its tag do not clash with others) and increases as per the loop for adding notation.
annotationTag = 100;
for(NSDictionary* postLocateDict in mapData ) {
doublelat = [[postLocateDictobjectForKey:@”location_dx”] doubleValue];
doublelongt = [[postLocateDictobjectForKey:@”location_dy”] doubleValue];
CLLocationCoordinate2D loc;
loc.longitude = longt;
loc.latitude = lat;
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = loc;
[mapViewHuanBao addAnnotation:annotationPoint];
annotationTag++;
}
Step 2- When you add a notation this will call an MKMapViewDelegate function, just set a tag of that notation.
– (MKAnnotationView *)mapView:(MKMapView *)mapViewviewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *identifier = @”myAnnotation”;
MKAnnotationView * annotationView = (MKAnnotationView *)[self.mapViewHuanBao dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.image = [UIImage imageNamed:@”pin.png”];
}else {
annotationView.annotation = annotation;
}
annotationView.tag = annotationTag;
returnannotationView;
}
Step 3 – When you tap on notation, a delegate function will call where you can identify the notation by its tag.
(void)mapView:(MKMapView *)mapViewdidSelectAnnotationView:(MKAnnotationView *)view
Here you will identify the notation by its tag using following code-
NSInteger indexOfTheObject = [mapView.annotations indexOfObject:view.annotation];
In indexOfTheObject you will get the notation tag.
Step 4- Now create a custom view as per your requirement, and give tag to every property you used in that view. We used callout view name as calloutView. Set its file owner to your view controller.
Step 5- After finding the tag of selected notation, just use following method to create a custom call out view.
[[NSBundle mainBundle] loadNibNamed:@”calloutView” owner:self options:nil];
CGPointhitPoint = [view convertPoint:CGPointZero toView:mapView];
[annotationView2 setFrame:CGRectMake(hitPoint.x-94,hitPoint.y-73, 220, 73)];
annotationView2.tag = selectedAnnotation;
UILabel *nameLbl = (UILabel *)[annotationView2 viewWithTag:[here give ur tag which u set in custom view]];
And so on….
Step 6-Finally just add it to the view.
[view addSubview:annotationView2];
You can also navigate it to its detail but for that you have to add it on map view, because when you are adding it on view it will remove it from view, so the object is DE allocated.
Comment here if you found any problem.
You may Also like:
Feb 20th, 2025
Millions of people across the globe turn to astrology for guidance in love, career, health, and personal growth. A survey by Pew Research Center found that 29% of Americans believe […]
Feb 11th, 2025
Remember when your parents warned you never to get into a car with a stranger? Fast forward to today, and getting into a stranger’s car has become a global business […]
Feb 6th, 2025
In today’s digital age, convenience is king, and online gift delivery services are thriving. With the global online gift market expected to grow significantly, now is the perfect time to […]