これがどのように機能するのか混乱しています。文字列値に基づいてピンをドロップする CLGeocoder を作成しています。私はこれを持っています:
- (void)placeMarkFromString:(NSString *)address {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
[placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@", [obj description]);
}];
// Check for returned placemarks
if (placemarks && [placemarks count] > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
// Create an MKPlacemark and add it to the mapView
MKPlacemark *place = [[MKPlacemark alloc] initWithPlacemark:topResult];
AddressAnnotation *anAddress = [[AddressAnnotation alloc] init];
anAddress.address = place.subThoroughfare;
anAddress.street = place.thoroughfare;
anAddress.city = place.locality;
anAddress.state = place.administrativeArea;
anAddress.zip = place.postalCode;
anAddress.name = place.name;
//[self.mapView addAnnotation:place];
[self.mapView addAnnotation:anAddress];
self.currentPlacemark = place;
// Center map on that region
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(topResult.location.coordinate, 2000, 2000);
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:region];
[_mapView setRegion:adjustedRegion animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results Found" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
if (error) {
NSLog(@"Error: %@", [error localizedDescription]);
}
}];
}
最初に、MKPlacemark をマップに追加すると、赤いピンが表示されます。ただし、アニメーション化されません。私は基本的に、3 つの MKPinAnnotationView の色のいずれかをドロップし、吹き出しとタイトル/サブタイトルを場所の名前と住所にする機能が必要です。これは、Google マップと同様です。しかし、私はアニメーションを取得していませんでした。
そこで、MKAnnotation クラスに準拠した独自のオブジェクトを作成する必要があるのではないかと考えました。だから私はそれをしましたが、それを場所に追加しようとすると、viewForAnnotationデリゲートメソッドにそのannotationViewが表示されません。その方法はここにあります:
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *placeMarkIdentifier = @"SimplePinIdentifier";
if ([annotation isKindOfClass:[AddressAnnotation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placeMarkIdentifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placeMarkIdentifier];
}
else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.animatesDrop = YES;
annotationView.draggable = YES;
annotationView.pinColor = MKPinAnnotationColorPurple;
annotationView.canShowCallout = YES;
// Create a button for the annotation
// UIButton *rightArrowButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// annotationView.rightCalloutAccessoryView = rightArrowButton;
// [self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5];
return annotationView;
}
return nil;
}
したがって、私の質問は、これを行うために独自のオブジェクトを作成する必要があるか、正しい軌道に乗っているか、何が間違っているのか、あるケースでは MKPlacemark オブジェクトを追加しているのはなぜですか、そして次に別の方法で行う場合は、オブジェクトを追加しますが、必ずしも MKPlacemark のサブクラスではありません。ありがとう!