以下代码返回“未选择注释”CalloutAccessories委托有助于确定选择了哪个注释。我的代码如下。有任何想法吗)?提前致谢
“RemoveViewController.m”
- (IBAction)removePin:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate viewController] removeAnno];
[self.navigationController popViewControllerAnimated:YES];
}
“ViewController.m”
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
// Do your thing when the detailDisclosureButton is touched
RemoveViewController *settingAnnotation = [[RemoveViewController alloc]initWithNibName:@"RemoveViewController" bundle:nil];
Annotation *annotation1;
annotation1 = view.annotation;
settingAnnotation.title = annotation1.title;
settingAnnotation.subtitle = annotation1.subtitle;
[self.navigationController pushViewController:settingAnnotation animated:YES];
}
}
- (void)removeAnno{
[mapView removeAnnotations: mapView.selectedAnnotations];
if (mapView.selectedAnnotations.count == 0)
{
NSLog(@"no annotation selected");
}
else
{
id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
CLLocationCoordinate2D coord = ann.coordinate;
NSLog(@"lat = %f, lon = %f", coord.latitude, coord.longitude);
}
}
答案 0 :(得分:1)
问题已解决。 [mapView removeAnnotations:mapView.selectedAnnotations]应该是在条件之后来的! 下面的代码工作正常!
“RemoveViewController.m”
- (IBAction)removePin:(id)sender {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate viewController] removeAnno];
[self.navigationController popViewControllerAnimated:YES];
}
“ViewController.m”
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
// Do your thing when the detailDisclosureButton is touched
RemoveViewController *settingAnnotation = [[RemoveViewController alloc]initWithNibName:@"RemoveViewController" bundle:nil];
Annotation *annotation1;
annotation1 = view.annotation;
settingAnnotation.title = annotation1.title;
settingAnnotation.subtitle = annotation1.subtitle;
[self.navigationController pushViewController:settingAnnotation animated:YES];
}
}
- (void)removeAnno{
if (mapView.selectedAnnotations.count == 0)
{
NSLog(@"no annotation selected");
}
else
{
id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
CLLocationCoordinate2D coord = ann.coordinate;
NSLog(@"lat = %f, lon = %f", coord.latitude, coord.longitude);
}
[mapView removeAnnotations: mapView.selectedAnnotations];
}
答案 1 :(得分:0)
是的,在selectedAnnotations
数组的索引0处获取注释(如果有)并读取其coordinate
属性:
if (mapView.selectedAnnotations.count == 0)
{
NSLog(@"no annotation selected");
}
else
{
id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0];
CLLocationCoordinate2D coord = ann.coordinate;
NSLog(@"lat = %f, lon = %f", coord.latitude, coord.longitude);
}
在调用removeAnnotations
之前,您应该这样做。
答案 2 :(得分:0)
尝试
CLLocationCoordinate2D coord = [[mapView.selectedAnnotations objectAtIndex:0] coordinate]
假设您正在尝试访问mapView.selectedAnnotations
的第一个对象。
答案 3 :(得分:0)
这将为您提供以下形式的地址字符串的坐标:State,City,Street,Number。
-(CLLocationCoordinate2D) addressLocation:(NSString*) locationSTR {
NSString *addressM = [NSString stringWithString:locationSTR];
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[addressM stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *urlCont = [NSURL URLWithString:urlString];
NSError* error = nil;
NSString *locationString = [NSString stringWithContentsOfURL:urlCont encoding:NSASCIIStringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}