Reputation: 13
Please help me, I am stuck with this MapView
:
I have a MapView
and a small label under that MapView
. On the MapView
, I have many pins (MKPinAnnotationView
), I know how to set the title and subtitle of each pin by using array, but I do not know how to distinguish each pin. I mean when user taps a pin, the label will show the title of the tapped pin.
Here is some of my code:
This is where I define the pin:
@implementation PlacePin
@synthesize coordinate,title,subtitle;
@synthesize nTag;
- (id)initWithLocation:(CLLocationCoordinate2D)coord{
self = [super init];
if (self) {
coordinate = coord;
}
return self;
}
-(void)dealloc{
[title release];
[subtitle release];
[super dealloc];
}
@end
This is where I proccess the result from server:
- (void) resultCheck {
NSString *strUrl = [NSString stringWithFormat:@"ServerAddress.com"];
NSLog(@"MapView - resultCheck: url: %@ ", strUrl);
NSURL *url = [ NSURL URLWithString:strUrl];
//get the result from server
NSString *result = [NSString stringWithContentsOfURL:url];
NSLog(@"MapView - resultCheck: result: %@", result);
NSDictionary *dictionary = [result JSONValue];
NSLog(@"MapView - resultCheck: dictionary: %@", dictionary);
//process the JSON, get two parameters: xPos, yPos
NSDictionary *value1 = [dictionary valueForKey:@"result"];
NSDictionary *value2 = [value1 valueForKey:@"post"];
NSArray *arrXPos = [value2 valueForKey:@"xPos"]; //array of xPos
NSArray *arrYPos = [value2 valueForKey:@"yPos"]; //array of yPos
self.arrName = [value2 valueForKey:@"name"]; //array of name
self.arrPlaceInfor = [value2 valueForKey:@"place_info"];
NSLog(@"MapView - resultCheck: value1: %@",value1);
NSLog(@"MapView - resultCheck: value2: %@",value2);
NSLog(@"MapView - resultCheck: value3: %@",arrXPos);
NSLog(@"MapView - resultCheck: value4: %@",arrYPos);
//get the xPos and yPos
for (int i = 0 ; i < [value2 count]; i++) {
//display the place depended on the xPos and yPos
CLLocationCoordinate2D location;
location.latitude = [[ NSString stringWithFormat:@"%@",[arrXPos objectAtIndex:i]] doubleValue];
location.longitude = [[ NSString stringWithFormat:@"%@",[arrYPos objectAtIndex:i]] doubleValue];
PlacePin *mapPoint = [[PlacePin alloc] initWithLocation:location];
//set the title and subtitle of the pin depending on the result from server
mapPoint.title = [ NSString stringWithFormat:@"%@",[self.arrName objectAtIndex:i]];
mapPoint.subtitle = [ NSString stringWithFormat:@"%@",[self.arrPlaceInfor objectAtIndex:i]];
[mapView addAnnotation:mapPoint];
[mapPoint release];
mapPoint = nil;
}
}
This is where I proccess the pin and set the label - where I am getting stuck :(
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(NA, 4_0){
self.labelShortIntro.text = @"1111111111111";
}
//customize an annotation
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorPurple;
annView.animatesDrop = TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
Upvotes: 1
Views: 249
Reputation: 1424
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
PlacePin *selectedPin = view.annotation;
NSLog(@"Title = %@ Subtitle = %@",selectedPin.title,selectedPin.subtitle);
self.labelShortIntro.text = [NSString stringWithFormat:@"Title = %@ Subtitle = %@",selectedPin.title,selectedPin.subtitle];
}
Upvotes: 1
Reputation: 2281
in didSelectAnnotationView
delegate:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSArray *selectedAnnotations = mapView.selectedAnnotations;
for(PlacePin *ann in selectedAnnotations)
{
NSLog(@"%@",ann.title);
self.labelShortIntro.text=ann.title;
}
}
I hope this code will help you.
Upvotes: 0