Uitableview mapView和UITable详细信息
尝试将注释的数据(名称、地址、电话、url、说明等)传递给带有表的DetailViewController。卡住了----请读代码。数据未通过Callout AccessoryTapped传递。帮忙Uitableview mapView和UITable详细信息,uitableview,mkmapview,mkannotation,cllocation,mkmapitem,Uitableview,Mkmapview,Mkannotation,Cllocation,Mkmapitem,尝试将注释的数据(名称、地址、电话、url、说明等)传递给带有表的DetailViewController。卡住了----请读代码。数据未通过Callout AccessoryTapped传递。帮忙 - (IBAction)gasButton:(id)sender { [self.mapView removeAnnotations:self.mapView.annotations]; self.localSearchRequest = [[MKLocalSearchR
- (IBAction)gasButton:(id)sender {
[self.mapView removeAnnotations:self.mapView.annotations];
self.localSearchRequest = [[MKLocalSearchRequest alloc] init];
self.localSearchRequest.region = self.mapView.region;
self.localSearchRequest.naturalLanguageQuery = @"gas station";
self.localSearch = [[MKLocalSearch alloc] initWithRequest:self.localSearchRequest];
[self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
if(error){
NSLog(@"localSearch startWithCompletionHandlerFailed! Error: %@", error);
return;
} else {
for(mapItem in response.mapItems){
MKPointAnnotation *zip = [[MKPointAnnotation alloc] init ];
zip.coordinate = mapItem.placemark.location.coordinate;
zip.title = mapItem.name;
self.mapView.delegate = self;
[self.mapView addAnnotation: zip];
[self.mapView selectAnnotation:zip animated:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];
NSLog(@"%@ - 1", mapItem.name);
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:zip.coordinate.latitude longitude:zip.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
NSString *dist = [[NSString alloc] initWithFormat:@"%.2f miles", distance * 0.000621371192];
zip.subtitle = dist;
}
}
}];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MYVC"];
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
else if ([annotation isKindOfClass: [MKPointAnnotation class] ])
{
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.enabled = YES;
annotationView.animatesDrop = YES;
annotationView.pinColor = MKPinAnnotationColorGreen;
annotationView.canShowCallout = YES;
NSLog(@"%@ - 2", mapItem.name);
return annotationView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"%@ - tapped", mapItem.name);
//STUCK HERE - mapItem.name is (null)
}
-(iAction)按钮:(id)发送方{
[self.mapView removeAnnotations:self.mapView.annotations];
self.localSearchRequest=[[MKLocalSearchRequest alloc]init];
self.localSearchRequest.region=self.mapView.region;
self.localSearchRequest.naturalLanguageQuery=@“加油站”;
self.localSearch=[[MKLocalSearch alloc]initWithRequest:self.localSearchRequest];
[self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse*response,NSError*error){
如果(错误){
NSLog(@“localSearch startWithCompletionHandlerFailed!错误:%@”,错误);
返回;
}否则{
用于(响应中的mapItem.mapItems){
MKPointAnnotation*zip=[[MKPointAnnotation alloc]init];
zip.coordinate=mapItem.placemark.location.coordinate;
zip.title=mapItem.name;
self.mapView.delegate=self;
[self.mapView addAnnotation:zip];
[self.mapView selectAnnotation:zip动画:是];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];
NSLog(@“%@-1”,映射项名称);
CLLocation*loc1=[[CLLocation alloc]initWithLatitude:zip.coordinate.latitude-longitude:zip.coordinate.longitude];
CLLocation*loc2=[[CLLocation alloc]initWithLatitude:self.mapView.userLocation.coordinate.latitude经度:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance距离=[LocalDistanceFromLocation:Local2];
NSString*dist=[[NSString alloc]initWithFormat:@“%.2f英里”,距离*0.000621371192];
zip.subtitle=dist;
}
}
}];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
MKPinAnnotationView*annotationView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“MYVC”];
if([annotation isKindOfClass:[MKUserLocation类]])
{
返回零;
}
else if([annotation isKindOfClass:[MKPointAnnotation class]])
{
annotationView.rightCalloutAccessoryView=[UIButton Button,类型:UIButtonTypedTailDisclosure];
annotationView.enabled=是;
annotationView.animatesDrop=YES;
annotationView.pinColor=MKPinAnnotationColorGreen;
annotationView.canShowCallout=是;
NSLog(@“%@-2”,mapItem.name);
返回注释视图;
}
返回零;
}
-(无效)地图视图:(MKMapView*)地图视图注释视图:(MKAnnotationView*)视图调用访问控制点击:(UIControl*)控制
{
NSLog(@“%@-点击”,映射项名称);
//卡在此处-mapItem.name为(null)
}
mapItem
存在于您的gasButton
函数的for循环中,就我所见,没有其他地方。您已经创建了注释(zip
)并将其添加到地图中。在viewForAnnotation
中,将该注释作为参数提供给您,并要求您从中创建一个annotationView。在calloutAccessoryControlTapped
中,您将看到一个annotationView,并被告知它已被点击。您需要沿着链返回到您的数据。从annotationView获取注释,从注释获取您的姓名
MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"%@ - tapped", view.annotation.title);
}
在annotationView.title=annotation.title上的MKPinAnnotationView类型的对象上找不到“Property”title;行。对不起,我想你应该只设置注释的标题,这样你就可以去掉那行,我会调整我的答案。您可能还需要获得自己的MKPointAnnotation类,因为MKPointAnnotation没有
title
属性。一个快速的方法是使用以下答案中的代码: