i have added a detail button on my pin annotation. i want to go to another view. when i click on it, following code doesn't work. i don't know where is the problem.
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != mapview.userLocation)
{
static NSString *defaultPinID = #"myPin";
pinAnnotation = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.canShowCallout = YES;
//instatiate a detail-disclosure button and set it to appear on right side of annotation
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
return pinAnnotation;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
annotationViewController *detailView=[[annotationViewController alloc] initWithNibName:#"annotationViewController" bundle:nil];
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
}
The method - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control is a delegate method. That's why you need to set your mapview's delegate to the class that implements this method (here self), in order for it to be called.
You also need to tell the class that you're implementing the MKMapViewDelegate methods. To do so, in you .h, change this line :
#interface MyViewController : UIViewController
To :
#interface MyViewController : UIViewController <MKMapViewDelegate>
Your method should get called now.
Related
I have a map view and there are 10 store locations which data comes via webservice. I just want to push to my detail view to show address, telephone and other informations of the clicked store.
I need to pass data to my detailview when user tapped or touch up inside to a annotation on mapkit. There are 10 annotations in my mapview and first I want to know, how can I understand or how can I get the annotationID of which annotation is clicked?
this is the method I return pins
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]) return nil;
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorPurple;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
/* and my action method for clicked or tapped annotation: */
- (IBAction)showDetails:(id)sender{
NSLog(#"Annotation Click");
[[mtMap selectedAnnotations]objectAtIndex:0];
magazaDetayViewController *detail = [[magazaDetayViewController
alloc]initWithNibName:#"magazaDetayViewController" bundle:nil];
detail.sehir=#"";
detail.magazaAdi=#"";
detail.adres=#"";
detail.telefon=#"";
detail.fax=#"";
[self.navigationController pushViewController:detail animated:YES];
}
if i can just get the clicked annotation index no i can fill detail properties with my array.
if this is impossible is there any other way to do it?
First in your annotaion view delegat make a button to go in detail view like bellow:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"current"];
mypin.pinColor = MKPinAnnotationColorPurple;
mypin.backgroundColor = [UIColor clearColor];
UIButton *goToDetail = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
mypin.rightCalloutAccessoryView = myBtn;
mypin.draggable = NO;
mypin.highlighted = YES;
mypin.animatesDrop = TRUE;
mypin.canShowCallout = YES;
return mypin;
}
Now use the following delegate whenever the button in annotationView will get tapped the following delegate will be called from where you can easly get which particular annotaion's button is tapped
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
annotation *annView = view.annotation;
detailedViewOfList *detailView = [[detailedViewOfList alloc]init];
detailView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
detailView.address = annView.address;
detailView.phoneNumber = annView.phonenumber;
[self presentModalViewController:detailView animated:YES];
}
here annotaion is a class importing MKAnnotaion.h and address and phonenumber are properties of annotaion class you can make many more while the address and phoneNumber properties of detailView class are strong. So that you can pass values. Hope this will help you!
Bellow is Delegate method of MapView......
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
}
when user tap annotation pin above method call automaticaly if method define in .m file nut first declare MKMapViewDelegate delegate in .h file
and also you can get title or subtitle and id from google place API....
link is...
https://developers.google.com/maps/documentation/places/
for(int i=0; i<[arrLat count];i++) {
CLLocationCoordinate2D theCoordinate1;
konumpin* myAnnotation1=[[konumpin alloc] init];
theCoordinate1.latitude = [[arrLong objectAtIndex:i] doubleValue];
theCoordinate1.longitude = [[arrLat objectAtIndex:i] doubleValue];
myAnnotation1.coordinate=theCoordinate1;
myAnnotation1.title=[arrMagaza objectAtIndex:i];
[annotations addObject:myAnnotation1];
}
[mtMap addAnnotations:annotations];
I am new to mapkit in objective-c. I am able to add custom annotation in mapview.
i need to place custom callout view like below image
.
But i didn't get how can i design callout view like this.
i know i need to add callout in view for annotation method.
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:#"blue_without_pin.png"];
annotationView.annotation = annotation;
return annotationView;
}
Basically what you want to do is add a new custom view by implementing the following method in your MKMapViewDelegate instance :
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
CGPoint annotationCenter = [mapView convertCoordinate:view.annotation.coordinate toPointToView:mapView];
[self displayCustomCalloutForAnnotation:view.annotation center:annotationCenter];
}
#interface CustomAnnotaionView : MKAnnotationView
#property (strong, nonatomic) UIView *calloutView;
-(void)setSelected:(BOOL)selected animated:(BOOL)animated;
#end
#implementation CustomAnnotaionView
#synthesize calloutView = _calloutView;
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
[self.calloutView setFrame:CGRectMake(30, 130, 250, 135)];
[self.calloutView sizeToFit];
self.calloutView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"popup.png"]];
[self addSubview:self.calloutView];
}
else
{
[self.calloutView removeFromSuperview];
}
}
-(void)didAddSubview:(UIView *)subview
{
if([[[subview class]description]isEqualToString:#"UICalloutView"])
{
[subview removeFromSuperview];
}
}
Use this customAnnotationView class in MapView delegate method,
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id
<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MapAnnotation class]])
{
CustomAnnotationView *annotationView = (CustomAnnotaionView*)[theMapView dequeueReusableAnnotationViewWithIdentifier:#"CustomAnnotationView"];
if(!annotationView)
{
MapAnnotation *annotation = (MapAnnotation *)annotation;
annotationView = [[CustomAnnotaionView alloc]initWithAnnotation:annotation reuseIdentifier:#"CustomAnnotationView"];
[annotationView setCanShowCallout:YES];
}
else
annotationView.annotation = annotation;
return annotationView;
}
return nil;
}
One solution for this would be to detect the exact coordinate of the annotation selected relative to the parent UIView and then drawing a UIView directly on top of the MKMapView.
Here's a snippet that might help:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
CGPoint pointInMap = [self.mapView convertCoordinate:buildingAnnotation.coordinate
toPointToView:self.view];
// Hide the default callout generated by MKMapView
[mapView deselectAnnotation:view.annotation animated:NO];
// Create a custom callout view and center the arrow on the pointInMap CGPoint
}
You must implement a callout view yourself, and use
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
this delegate to make the callout view showing at right place,
IOS have not any method to custom callout view yet
I have created a MKAnnotationView and do add a UIButton on it. Now I would like that button to remove the pin it is in.
This is how I add the button:
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != myMapView.userLocation)
{
NSString *defaultPinID = [[NSString alloc] initWithFormat:#"pin%d",pinCount];
pinAnnotation = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.canShowCallout = YES;
//// ADDING MY BUTTON
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotation.rightCalloutAccessoryView = deleteButton;
//// ADDING IT'S ACTION
[deleteButton addTarget:self action:#selector(deletePin) forControlEvents:UIControlEventTouchUpInside];
}
pinCount++;
return pinAnnotation;
}
As you can see there, I set the action to call this method:
-(void)deletePin
{
//What to put here...?
}
How can I make that action delete it's button's pin?
I figured maybe sending the id thru or something, but am not sure how.
With annotation callout accessory views, do not use your own target/action method.
Instead, use the map view's calloutAccessoryControlTapped delegate method which will make this job much easier.
Remove the addTarget line and your custom method. Then implement the delegate method instead:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
[mapView removeAnnotation:view.annotation];
}
Also, unrelated, but the way you are setting the re-use identifier is wrong and defeats re-usability by assigning a different id to every annotation. The following is recommended:
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotationView = nil;
if (annotation != myMapView.userLocation)
{
NSString *reuseId = #"StandardPin";
pinAnnotationView = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if ( pinAnnotationView == nil )
{
pinAnnotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
pinAnnotationView.canShowCallout = YES;
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotationView.rightCalloutAccessoryView = deleteButton;
}
else
//update annotation property if view is being re-used...
pinAnnotationView.annotation = annotation;
}
return pinAnnotationView;
}
Get the sender in your callback
[deleteButton addTarget:self action:#selector(deletePin:) forControlEvents:UIControlEventTouchUpInside];
-(void)deletePin:(id)sender
{
//What to put here...?
}
Then get the MKAnnotation associated with the MKPinAnnotationView, remove it from the MKMapView's annotations and redraw ([mapView setNeedsDisplay])
I have a map with only one annotation.I created a simple class which I want it to show when the user clicks on the annotation.The problem is that when I click on the annotation nothing happens.
Here is my code:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
NSLog(#"Reverse Geocoder completed");
mPlacemark=placemark;
[mapView addAnnotation:placemark];
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
annView.animatesDrop=TRUE;
//create UIButton for annotation
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//NSInteger annotationValue = [self.annotations indexOfObject:annotation];
[detailButton addTarget:self action:#selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView=detailButton;
return annView;
}
-(void)showDetailView:(id)sender{
NSLog("inside the stupid method");
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
My showDetailView function never gets called.Please help me.I'm new to iphone and I might forget a simple thing.Thanks
Still not working!!!!
First, check that the map view's delegate is set otherwise your viewForAnnotation method will not get called.
Next, the accessory button appears on the annotation's callout which will only appear if you set canShowCallout:
annView.canShowCallout = YES;
Next, instead of using your own method to handle the button action, it's much better to use the map view's own calloutAccessoryControlTapped delegate method which gets called when tapping an accessory:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"inside the stupid method");
//Here, the annotation tapped can be accessed using view.annotation
}
Remove the [detailButton addTarget... line from viewForAnnotation.
Also note your NSLog in the showDetailView method is missing the leading # which will result in a crash when the method does get called.
Another thing is you should use dequeueReusableAnnotationViewWithIdentifier in viewForAnnotation to enable annotation view re-use.
Example as requested:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString *annReuseId = #"currentloc";
MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annReuseId];
if (annView == nil)
{
annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annReuseId];
annView.animatesDrop = YES;
annView.canShowCallout = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView=detailButton;
}
else {
annView.annotation = annotation;
}
return annView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"calloutAccessoryControlTapped: annotation = %#", view.annotation);
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
//here, can set annotation info in some property of detailView
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
Ok. If you are using storyboards, you need to create a segue between the map view controller and then where the below code exists:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"calloutAccessoryControlTapped: annotation = %#", view.annotation);
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
//here, can set annotation info in some property of detailView
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
Use the following code instead:
NSLog(#"calloutAccessoryControlTapped: annotation = %#", view.annotation);
[self performSegueWithIdentifier:#"DetailViewController" sender:self];
This will give you the movement to the detailed view.
write your function like this:
-(IBAction)showDetailView:(id)sender{
NSLog("inside the stupid method");
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
you have to set the map delegate to self like this,
mapview.delegate=self; (if your calling the method in same page)
if you don't assign the delegate in your code the respective methods won't be called, so check if you have the above mentioned line.
Replace -(IBAction)showDetailView:(UIView*)sender with following...
-(void)showDetailView:(id)sender
In my MapView i have pin dropped on certain locations and now i want to display some title on these pin annotation and make it clickable so that on click i can push another view.
Please help !!!!
This is the code snippet:
Custom Annotation View using this method
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation) return nil;
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: #"asdf"];
if (pin == nil)
{
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: #"asdf"] autorelease];
}
else
{
pin.annotation = annotation;
}
pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;
}
Delegate method which get's called when you tap on the button
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
//DetailPg=[[BookMarksDetail alloc]initWithNibName:#"BookMarksDetail" bundle:nil];
//DetailPg.selectRest =[view.annotation title]; //In addition, to get the Title of the AnnotationView.
//DetailPg.m=1;
//[self.navigationController pushViewController:DetailPg animated:YES];
//[DetailPg release];
}
The answers to this are all provided in Apples Docs I suggest you read them more closely. However to have a call out all you need to do is set the title property.
For buttons and such you need to use the MapView Delegate method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
_pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
Apple suggests reusing annotations this is something you will need to take into account.