gpt4 book ai didi

objective-c - 如何将 map 注释 View 按钮与数据库连接以转到另一个 View ?

转载 作者:搜寻专家 更新时间:2023-10-30 23:18:58 24 4
gpt4 key购买 nike

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
//if it's user location, 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];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorRed;

//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;

return pinView;
}

//for map view annotation right button
-(void)showDetails:(id)sender{
NSLog(@"Annotation Click");
//fypAppDelegate *appDelegate = (fypAppDelegate *)[[UIApplication sharedApplication] delegate];
//Attraction *attraction = (Attraction *)[appDelegate.attractions objectAtIndex:sender];

infoViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"info"];
self.infoView = viewController;
[self.navigationController pushViewController:infoView animated:true];
}

//for map view annotation left button
-(void)zoomToLocation:(id)sender{
NSLog(@"Annotation Click");
}

上面是 map 注释的委托(delegate)。我能够显示图钉并显示 map 注释 View ,但我不知道如何将按钮事件链接到下一个 View (infoViewController)。正如你们所见,我希望使用右侧按钮来让用户查看有关该地点的更多信息,而使用左侧按钮,我希望允许用户放大该图钉的坐标。

数据来 self 创建的数据库。下面是我的做法,仅供引用(以防大家需要)

-(void)putPins
{
fypAppDelegate *appDelegate = (fypAppDelegate *)[[UIApplication sharedApplication] delegate]; //get data
[appDelegate readTopAttractions];
int i = 0;
int count = appDelegate.attractions.count;
self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:appDelegate.attractions.count];
while (i < count) {
Attraction *attraction = (Attraction *)[appDelegate.attractions objectAtIndex:i];
i++;

//Set coordinates for pin
CLLocationCoordinate2D location;
location.latitude = (double)[[attraction xCoor] doubleValue];
location.longitude = (double)[[attraction yCoor] doubleValue];
MapPin *mapPin = [[MapPin alloc] init];
[mapPin setCoordinate:location];
[mapPin setName: [attraction name]];
NSString *desc = [attraction description];
int i = 0, position;
while(i < 50){
if ([desc characterAtIndex:i] == ' '){
position = i;
i++;
}
else
i++;
}
desc = [@"" stringByAppendingFormat:@"%@%@", [desc substringToIndex:position], @"..."];
[mapPin setDescription: desc];
[self.mapAnnotations addObject:mapPin];
}

[self.mapView addAnnotations:self.mapAnnotations];
}

如果你们需要更多细节,请告诉我。谢谢你! =)

最佳答案

在您的 showDetails:zoomToLocation: 方法中,您可以通过执行以下操作获取对其标注按钮被点击的注释的引用:

MapPin *ann = (MapPin *)[mapView.selectedAnnotations objectAtIndex:0];


zoomToLocation: 中,您可以使用以下方法放大该注释:

[mapView setRegion:
MKCoordinateRegionMakeWithDistance(ann.coordinate, 500, 500)
//500 meters vertical span, 500 meters horizontal span
animated:YES];

showDetails:中,您可以将ann或其属性传递给详细信息 View 。


顺便说一句,您可以使用 map View 的 calloutAccessoryControlTapped 委托(delegate)方法,而不是在 viewForAnnotation 中使用 addTarget 调用自定义方法,这样可以更直接地访问被点击的注解。例如:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
MapPin *ann = (MapPin *)view.annotation;

if (control == view.rightCalloutAccessoryView)
{
NSLog(@"calloutAccessoryControlTapped: control=RIGHT");
//show detail view (or you can call your custom method here)...
}
else
if (control == view.leftCalloutAccessoryView)
{
NSLog(@"calloutAccessoryControlTapped: control=LEFT");
//zoom in (or you can call your custom method here)...
}
else
{
NSLog(@"calloutAccessoryControlTapped: unknown control");
}
}

如果您决定使用 calloutAccessoryControlTapped 委托(delegate)方法,请确保从 viewForAnnotation 中删除 addTarget 调用。

关于objective-c - 如何将 map 注释 View 按钮与数据库连接以转到另一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9097469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com