私を助けてください、私はこれで立ち往生していますMapView
:
MapView
その下に小さなラベルがありますMapView
。にMapView
ピンがたくさんあり(MKPinAnnotationView
)、配列を使って各ピンのタイトルとサブタイトルを設定する方法は知っていますが、各ピンを区別する方法がわかりません。つまり、ユーザーがピンをタップすると、ラベルにはタップされたピンのタイトルが表示されます。
これが私のコードの一部です:
ここでピンを定義します。
@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
これは、サーバーからの結果を処理する場所です。
- (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;
}
}
これは私がピンを処理してラベルを設定する場所です-私が立ち往生している場所:(
- (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;
}