I have a table view with an ordered list of objects, ordered by distance between me and the object. I need to show annotations in a map to show to the user the position of the objects but i need to show in the same order than table´s order because then i need to get with one has been pressed to show info of each object.
The problem is that when I put the annotations in the map each time I put them the tag number of the annotation is different. Is like the map added the annotations in a random order.
I have an annotations array and then I add that array to the map:
MKPointAnnotation* tallerAnnotation = [[MKPointAnnotation alloc] init];
[tallerAnnotation setCoordinate:CLLocationCoordinate2DMake([taller getLatitudTaller], [taller getLongitudTaller])];
[tallerAnnotation setTitle:[taller getNombre]];
[tallerAnnotation setSubtitle:[taller getDomicilio]];
[annotations addObject:tallerAnnotation];
And then add the array to the map:
[mapa addAnnotations:annotations];
Here is the code where I custom the annotation:
- (MKAnnotationView *) mapView: (MKMapView *) mapa viewForAnnotation: (id <MKAnnotation>) annotation_ {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapa dequeueReusableAnnotationViewWithIdentifier: #"YourPinId"];
if (pin == nil) {
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation_ reuseIdentifier: #"YourPinId"];
}
else {
pin.annotation = annotation_;
}
if ([[annotation_ title]isEqualToString:#"Tu posición"])
pin.pinColor = MKPinAnnotationColorRed;
else
{
[pin setCanShowCallout:YES];
pin.pinColor = MKPinAnnotationColorGreen;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(getInfoTalleres:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTag:posicionActualTaller];
pin.rightCalloutAccessoryView = rightButton;
UILabel *l1=[[UILabel alloc] init];
l1.frame=CGRectMake(0, 5, 50, 15);
l1.backgroundColor = [UIColor clearColor];
l1.textColor = [UIColor whiteColor];
l1.font=[UIFont fontWithName:#"Arial Rounded MT Bold" size:(10.0)];
l1.text=[[[mainDelegate getTalleres]objectAtIndex: posicionActualTaller]getDistancia];
posicionActualTaller +=1;
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,15)];
[leftCAV addSubview : l1];
pin.leftCalloutAccessoryView = leftCAV;
}
pin.animatesDrop = YES;
return pin;
}
I would like to do something like this:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"Tag del taller = %i",[control tag]);
[mainDelegate setTallerActual:[[mainDelegate getTalleres]objectAtIndex:[control tag]]];
[self detalleTaller];
}
[control tag] has differents values each time I enter into map view so that way it´s impossible do something like [objects objectAtIndex:[control tag]];
Please I need help.
Thanks in advance.
Ok finally I have found a solution.
When I create the
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(getInfoTalleres:) forControlEvents:UIControlEventTouchUpInside];
for the annotation I assign the annotation title to the button this way:
[rightButton setTitle:[annotation_ subtitle] forState:UIControlStateNormal];
Then:
-(void)getInfoTalleres:(UIButton* )sender
{
for (int i = 0; i < [[mainDelegate getTalleres]count]; i++)
{
if ([[sender currentTitle] isEqualToString:[[[mainDelegate getTalleres]objectAtIndex:i]getDomicilio]])
{
[mainDelegate setTallerActual:[[mainDelegate getTalleres]objectAtIndex:i]];
[self detalleTaller];
break;
}
}
}
Hope somebody can use this solution.
Thanks a lot Cocoa.
Regards.
Check the Answer here it access title property Determine which MKPinAnnotationView was selected? and the tutorial http://www.scribd.com/doc/91629556/192/The-MKAnnotationView-class
let me know if you still not find answer
Related
EXTRA CODE ADDED
I am new to xcode. I have managed to create annotations on my map in various different colours. What I would like to do is have each annotation lead to somewhere new with a disclosure button. I have implemented a right arrow disclosure button now what I need to know is how I link that up so that each annotation will lead to a different view controller of my choice. Here is my current code on my MapViewController implementation.
MainMapViewController.m
#import "MainMapViewController.h"
#import "LocationAnnotation.h"
#interface MainMapViewController ()
#end
//Totnes Main Centre Coordinates
#define Totnes_LATITUDE 50.433741
#define Totnes_LONGITUDE -3.685797
//The Dartmouth Inn Coordinates
#define DARTMOUTH_INN_LATITUDE 50.430036;
#define DARTMOUTH_INN_LONGITUDE -3.683873;
//Pub Offers Co-Ordinates
#define TheKingBill_LATITUDE 50.431379
#define TheKingBill_LONGITUDE -3.685495
#define TheSevenStars_LATITUDE 50.431045
#define TheSevenStars_LONGITUDE -3.682945
#define TheLordNelson_LATITUDE 50.430931
#define TheLordNelson_LONGITUDE -3.683644
//Span
#define THE_SPAN 0.01f;
#implementation MainMapViewController
#synthesize mainMapView;
- (void)viewDidLoad
{
[super viewDidLoad];
//Set Delegate
mainMapView.delegate = self;
//Create the region
MKCoordinateRegion myRegion;
//Centre
CLLocationCoordinate2D centre;
centre.latitude = Totnes_LATITUDE;
centre.longitude = Totnes_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = centre;
myRegion.span = span;
//Set The Map View
[mainMapView setRegion:myRegion animated:YES];
//Annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
LocationAnnotation * myAnn;
//The King Bill Annotation
myAnn = [[LocationAnnotation alloc] initWithTitle:#"The King Bill"
andSubtitle:#"Another Pub In Town"
andCoordinate:CLLocationCoordinate2DMake(TheKingBill_LATITUDE, TheKingBill_LONGITUDE)
andID:1];
[locations addObject:myAnn];
//The Seven Stars Annotations
myAnn = [[LocationAnnotation alloc] initWithTitle:#"The Royal Seven Stars Hotel"
andSubtitle:#"Hotel In Town"
andCoordinate:CLLocationCoordinate2DMake(TheSevenStars_LATITUDE, TheSevenStars_LONGITUDE)
andID:2];
[locations addObject:myAnn];
//The Lord Nelson Annotations
myAnn = [[LocationAnnotation alloc] initWithTitle:#"The Lord Nelson"
andSubtitle:#"Great Pub In Centre of Town"
andCoordinate:CLLocationCoordinate2DMake(TheLordNelson_LATITUDE, TheLordNelson_LONGITUDE)
andID:3];
[locations addObject:myAnn];
[self.mainMapView addAnnotations:locations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView = rightButton;
int annId = ((LocationAnnotation *)annotation).idNumber;
annView.pinColor = (annId == 1) ? MKPinAnnotationColorPurple
: (annId == 2) ? MKPinAnnotationColorGreen
: MKPinAnnotationColorRed;
annView.canShowCallout = YES;
return annView;
}
#end
set rightbutton's tag to annID.
rightButton.tag = annId;
and then assign Selector to touchup event:
[rightButton addTarget:self action:#selector(YourMethod:) forControlEvents:UIControlEventTouchUpInside];
In YourMethod, you can use senders tag for navigating to different view
-(void)YourMethod:(UIButton*)sender
{
if(sender.tag==1)
{
//push viewcontroller1
}
else
{
//push viewcontroller
}
return;
}
As you can see, your right button is of type UIButton. Because of this fact you can addTarget to it:
[rightButton addTarget:self action:#selector(YourMethod) forControlEvents:UIControlEventTouchUpInside];
After user tap on rightButton YourMethod will be launched.
So for every pin you can add different button with different method, in the same way you are setting AnnotationColor
int annId = ((LocationAnnotation *)annotation).idNumber;
if (annId == 1)
{
annView.pinColor = MKPinAnnotationColorPurple;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView = rightButton;
[rightButton addTarget:self action:#selector(MethodWhereYouWillFireYouNewViewController) forControlEvents:UIControlEventTouchUpInside];
}
else if (annId == 2)
{
//similar as above
}
Check This:
-(void)clickOnMapAnnotation:(UIButton*)sender
{
int AnnotationClicked =sender.tag;
if(AnnotationClicked ==1)
{
//PushViewcontroller1
}
else
{
//PushViewcontroller1
}
}
(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
int annId = ((LocationAnnotation *)annotation).idNumber;
annView.pinColor = (annId == 1) ? MKPinAnnotationColorPurple
: (annId == 2) ? MKPinAnnotationColorGreen
: MKPinAnnotationColorRed;
//The Detail Disclosure button that I want to lead to a new view controller.
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(clickOnMapAnnotation:) forControlEvents:UIControlEventTouchUpInside];
rightButton.tag = annId;
annView.rightCalloutAccessoryView = rightButton;
annView.canShowCallout = YES;
return annView;
}
I'm a noob to iphone development and i'm trying to add a custom button to an annotation callout. I have no problems adding a regular button to rightCalloutAccessoryView, but it's just not working for custom style. My image size is 32 x 32. I also want to add a custom map pin following this stackoverflow question here Any help is greatly appreciated.
MY CODE
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Define your reuse identifier.
static NSString *identifier = #"MapPoint";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.image = [UIImage imageNamed:#"myimage.png"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setImage:[UIImage imageNamed:#"phony2.png"] forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
return nil;
}
[rightButton addTarget:self
action:#selector(showDetails:)
Remove this line
and set frame of the button
[rightButton setFrame:CGRectMake(0,0,32,32)];
and get the tap action from the degate method
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
where you get the annotation view.
Hope this will help you.
Well, I have a view with a map where I load some annotations. Everything works fine, but when I want to erase those annotations and include other annotations they do not show up until I move (scroll) the map.
Any suggestion?
EDITED:
Seems like instead of using this:
[self updateMap];
I have to use:
[self performSelectorOnMainThread:#selector(updateMap) withObject:nil waitUntilDone:true];
Being update map the method where I add my new annotations.
Try smth like this:
[mapView removeAnnotations:[mapView annotations]];
NSArray *targetArray = /*YOUR ARRAY NAME with object, containing `latitude`, `longitude` and `title` values*/
for (id *item in targetArray) {
if (item.latitude && item.longitude) {
MKPin *pin = [[MKPin alloc] init]; // MKPin is your class which specifies the deledate `MKAnnotation`
pin.coordinate = CLLocationCoordinate2DMake(item.latitude, item.longitude);
pin.title = item.title;
[mapView addAnnotation:pin];
}
}
In the viewdidload use :
- (void)viewDidLoad
{
NSMutableArray* annotations=[[NSMutableArray alloc] init];
MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];
myAnnotation.coordinate = theCoordinate;
[annotations addObject:myAnnotation];
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;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"welcome into the map view annotation");
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.draggable = NO;
pinView.pinColor=MKPinAnnotationColorGreen;
//button on the right for popup for pins
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
//zoom button on the left of popup for pins
UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[leftButton setTitle:annotation.title forState:UIControlStateNormal];
[leftButton addTarget:self
action:#selector(zoomToLocation:) forControlEvents:UIControlEventTouchUpInside];
pinView.leftCalloutAccessoryView = leftButton;
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"profile.png"]];
pinView.leftCalloutAccessoryView = profileIconView;
[profileIconView release];
return pinView;
}
Hopefully this helps !!!!!!!!!
m stucked in figuring out how can i set the viewport for my google map markers.
is there any way to do it in objC??
or i have to do it in my map itself??? if yes, then how???
My code for google map just shows the maps along with the markers,but when i click it,it just shows the name of the place in a small box.. :(
how to make it a proper viewport???
here's the viewport image:
1) You can subclass MKAnnotationView. I think it's the best way to solve your problem.
2) You can customize your annotation callout view, e.g:
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* ItemAnnotationIdentifier = #"itemAnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)
[theMapView dequeueReusableAnnotationViewWithIdentifier:ItemAnnotationIdentifier];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:ItemAnnotationIdentifier]
autorelease];
customPinView.canShowCallout = YES;
UIImage *logo = [ImageProcessor scaleImage:[UIImage imageNamed:#"logo.png"] toSize:CGSizeMake(30, 30)];
customPinView.leftCalloutAccessoryView = [[[UIImageView alloc] initWithImage:logo] autorelease];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.tag = [((ItemAnnotation *)annotation).itemId intValue];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
I have a map view with pins that when the user selects a pin it goes to a detail screen for that pin. I also have a table view that when the user selects an item it goes to the same type detail view.
Here's the problem ...
It seems to work fine in everything from 3.1.3 to 4.1. That is the detail view matches with the pin. But I have a user who just upgraded to iOS 4.2 and says that under 4.1 it worked fine, but in 4.2 the pin selection takes him to the detail for a different pin. But in the table view it still works fine.
Is it possible that there was a change in MKMapView in iOS 4.2 that changes how the pinID is selected?
Addition - I have added the relevant parts of viewForAnnotation and checkButtonTapped
- (MKPinAnnotationView *)mapView:(MKMapView *)eMapView viewForAnnotation:(id <MKAnnotation>)annotation {
int postTag = 0;
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[eMapView dequeueReusableAnnotationViewWithIdentifier:#"Pin"];
if(pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Pin"];
pinView.frame = CGRectMake(0, 0, 25, 25);
} else {
pinView.annotation = annotation;
}
// Set up the Right callout
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[myDetailButton addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
// Identify which pin is being selected
if ([[annotation title] isEqualToString:#"Current Location"]) {
postTag = 99999;
} else {
postTag = [annotation getPinID];
}
myDetailButton.tag = postTag;
pinView.rightCalloutAccessoryView = myDetailButton;
pinView.animatesDrop = YES;
// Set to show a callout on the pin
pinView.canShowCallout = YES;
return pinView;
}
// Method to show detail view when the callOut button is selected
- (IBAction) checkButtonTapped: (id) sender {
int nrButtonPressed = ((UIButton *)sender).tag;
if (nrButtonPressed < 99999) {
if (self.showDetailView == nil) {
DetailData *tmpViewController = [[DetailData alloc] initWithNibName:#"Detail" bundle:nil];
self.showDetailView = tmpViewController;
[tmpViewController release];
}
buttonDetail = [[mapView annotations] objectAtIndex:(nrButtonPressed-1)];
NSMutableArray *tmpArray = [NSMutableArray arrayWithObjects: [buttonDetail getDataI], [buttonDetail getDataII], [buttonDetail getDataIII], [buttonDetail getDataIV], [buttonDetail getDataV], nil];
self.showDetailView.eventData = [[NSMutableArray alloc] initWithArray:tmpArray copyItems:YES];
[self.navigationController pushViewController:self.showDetailView animated:YES];
[self.showDetailView.eventData release];
}
}
As you can see, in checkButtonTapped I record the pin selected then collect the data from the annotation for that pin. The problem seems to be that nrButtonPressed is now incorrect. But it is fine when compiled in 4.1
In checkButtonTapped, the annotation is being identified using the button's tag. The button's tag is set in viewForAnnotation by calling the custom method getPinID.
The button tag is used as the index into the mapView annotations array. It's not clear how the getPinID method figures out what index it is at in the mapView's annotations array. I'm not sure it's wise to assume an annotation is going to be at a specific index in the array. Even if mapView doesn't shuffle the annotations around, your app might be constantly adding and removing annotations.
Because you are assigning the button as a callout accessory in the annotation view, rather than using a button tag to identify the annotation, you can use the mapView's own mapView:annotationView:calloutAccessoryControlTapped: delegate method.
Instead of doing addTarget on the button and having it call your custom method, remove the call to addTarget and implement calloutAccessoryControlTapped.
In calloutAccessoryControlTapped, you can directly access the annotation using view.annotation. You can also easily check if the annotation is the "user location":
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
if (view.annotation == mapView.userLocation)
return;
buttonDetail = (MyCustomAnnotationClass *)view.annotation;
//show detail view using buttonDetail...
}
Additionally, in viewForAnnotation, you are creating a button every time even if the view is being re-used. So a re-used annotation view probably ends up getting multiple buttons on top of each other. The button should only be created and set in the if(pinView == nil) section.
If anyone would care to see what I am using now (and it is working) ...
- (MKPinAnnotationView *)mapView:(MKMapView *)eMapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[eMapView dequeueReusableAnnotationViewWithIdentifier:#"Pin"];
if(pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Pin"];
pinView.frame = CGRectMake(0, 0, 25, 25);
} else {
pinView.annotation = annotation;
}
// Set up the Right callout
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
pinView.rightCalloutAccessoryView = myDetailButton;
pinView.animatesDrop = YES;
// Set to show a callout on the pin
pinView.canShowCallout = YES;
return pinView;
}
// Added this at the suggestion of aBitObvious on StackOverflow - 120810
- (void)mapView:(MKMapView *)eMapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
if (view.annotation == eMapView.userLocation)
return;
if (self.showDetailView == nil) {
DetailData *tmpViewController = [[DetailData alloc] initWithNibName:#"DetailData" bundle:nil];
self.showDetailView = tmpViewController;
[tmpViewController release];
}
buttonDetail = (MyCustomAnnotationClass *)view.annotation;
NSMutableArray *tmpArray = [NSMutableArray arrayWithObjects: [buttonDetail getDataI], [buttonDetail getDataII], [buttonDetail getDataIII], [buttonDetail getDataIV], [buttonDetail getDataV], nil];
self.showDetailView.eventData = [[NSMutableArray alloc] initWithArray:tmpArray copyItems:YES];
[self.navigationController pushViewController:self.showDetailView animated:YES];
[self.showDetailView.eventData release];
}
Thanks again!