在我的应用程序中,我有一个带有几个自定义引脚的mapView。
我正在尝试这样做:在showCallout上会出现一个AlertView,并告诉您是否要计算路线。这适用于一个位置。或者更好,只调用一个方法,所以我只有一个目的地。
这是我的代码(我有9个目的地,但我只发布了2个以缩短代码):
- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:(id)self];
MKCoordinateRegion TavernaCongiura = { {0.0, 0.0} , {0.0, 0.0} };
TavernaCongiura.center.latitude = 40.380784;
TavernaCongiura.center.longitude = 15.541373;
TavernaCongiura.span.longitudeDelta = 0.004f;
TavernaCongiura.span.latitudeDelta = 0.004f;
[mapView setRegion:TavernaCongiura animated:YES];
mapView.showsUserLocation = YES;
TeggianoAnnotation *ann0 = [[TeggianoAnnotation alloc] init];
ann0.title = @"Taverna de la Congiura";
ann0.subtitle = @"Antipasto";
ann0.coordinate = TavernaCongiura.center;
[mapView addAnnotation: ann0];
MKCoordinateRegion TavernaDeiMori = { {0.0, 0.0} , {0.0, 0.0} };
TavernaDeiMori.center.latitude = 40.380535;
TavernaDeiMori.center.longitude = 15.542028;
TavernaDeiMori.span.longitudeDelta = 0.004f;
TavernaDeiMori.span.latitudeDelta = 0.004f;
[mapView setRegion:TavernaDeiMori animated:YES];
TeggianoAnnotation *ann1 = [[TeggianoAnnotation alloc] init];
ann1.title = @"Taverna dei Mori";
ann1.subtitle = @"Parmatieddi";
ann1.coordinate = TavernaDeiMori.center;
[mapView addAnnotation: ann1];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation {
if (newLocation.horizontalAccuracy == oldLocation.horizontalAccuracy) {
[self->locationManager stopUpdatingLocation];
CLLocationCoordinate2D coords = newLocation.coordinate;
NSString *stringURL = [NSString stringWithFormat:@"http://maps.google.com
/maps?saddr=%g,%g&daddr=40.380784,15.541373", coords.latitude, coords.longitude];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
}
- (void)locationManagerMori:(CLLocationManager *)manager didUpdateToLocation:
(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (newLocation.horizontalAccuracy == oldLocation.horizontalAccuracy) {
[self->locationManager stopUpdatingLocation];
CLLocationCoordinate2D coords = newLocation.coordinate;
NSString *stringURL = [NSString stringWithFormat:@"http://maps.google.com
/maps?saddr=%g,%g&daddr=40.380535,15.542028", coords.latitude, coords.longitude];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:
(id<MKAnnotation>)annotation {
MKAnnotationView *MyPin=[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"current"];
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
if ([[annotation title] isEqualToString:@"Taverna de la Congiura"]) {
[advertButton addTarget:self action:@selector(buttonCongiura:)
forControlEvents:UIControlEventTouchUpInside];
MyPin.image = [UIImage imageNamed:@"CongiuraMappa.png"];
}
if ([[annotation title] isEqualToString:@"Taverna dei Mori"]) {
[advertButton addTarget:self action:@selector(buttonMori:)
forControlEvents:UIControlEventTouchUpInside];
MyPin.image = [UIImage imageNamed:@"MoriMappa.png"];
}
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.canShowCallout = YES;
return MyPin;
}
-(void)buttonCongiura:(id)sender {
UIAlertView *alertViewCongiura;
alertViewCongiura = [[UIAlertView alloc] initWithTitle:@"Taverna de la Congiura"
message:@"Calcola Percorso" delegate:self cancelButtonTitle:@"Annulla"
otherButtonTitles:@"Calcola", nil];
[alertViewCongiura show];
}
-(void)buttonMori:(id)sender {
UIAlertView *alertViewMori;
alertViewMori = [[UIAlertView alloc] initWithTitle:@"Taverna dei Mori"
message:@"Calcola Percorso" delegate:self cancelButtonTitle:@"Annulla"
otherButtonTitles:@"Calcola", nil];
[alertViewMori show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
self->locationManager = [[CLLocationManager alloc] init];
self->locationManager.delegate = (id)self;
[self->locationManager startUpdatingLocation];
}
if (buttonIndex == 2) {
self->locationManagerMori = [[CLLocationManager alloc] init];
self->locationManagerMori.delegate = (id)self;
[self->locationManagerMori startUpdatingLocation];
}
}
问题是只有第一个
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation
被调用。
答案 0 :(得分:1)
您不能为正在使用的每个位置管理器实例的委托方法使用其他名称。每个位置管理器仍将调用CLLocationManagerDelegate
协议定义的方法。
您可以通过检查manager
参数是否等于您正在创建的实例之一(例如if (manager == locationManagerMori)
)来检查哪个管理器正在调用。
但您首先不需要为每个注释创建单独的位置管理器实例。
相反,只保留一个位置管理器实例,在委托方法中,您可以找出当前选择的注释是什么,并在url字符串中使用其坐标。例如:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (newLocation.horizontalAccuracy == oldLocation.horizontalAccuracy)
//the above if-condition looks a little suspicious by the way
{
if (mapView.selectedAnnotations.count == 0)
{
//no annotation selected, do nothing
return;
}
//there can only be one selected annotation so get the one at index 0
id<MKAnnotation> selectedAnnotation =
[mapView.selectedAnnotations objectAtIndex:0];
[self->locationManager stopUpdatingLocation];
CLLocationCoordinate2D coords = newLocation.coordinate;
NSString *stringURL = [NSString stringWithFormat:
@"http://maps.google.com/maps?saddr=%g,%g&daddr=%g,%g",
coords.latitude, coords.longitude,
selectedAnnotation.coordinate.latitude,
selectedAnnotation.coordinate.longitude];
NSURL *url = [NSURL URLWithString:stringURL];
//stop updating location to avoid possible endless loop
//when user comes back to this app...
[manager stopUpdatingLocation];
[[UIApplication sharedApplication] openURL:url];
}
}
每个注释也不需要单独的按钮操作方法。您只需创建一个按钮操作方法,并使用相同的技术来获取选定的注释。
另一个问题是在警报视图clickedButtonAtIndex
方法中。您似乎通过查看警报视图的按钮索引来检查它是哪个注释。该索引将是Annulla或Calcola按钮(不是注释)。
由于您不需要为每个注释创建单独的位置管理器,因此您无需知道警报视图的注释。您只需要检查用户是否点击了Annulla或Calcola:
if (buttonIndex == alertView.firstOtherButtonIndex)
{
self->locationManager = [[CLLocationManager alloc] init];
self->locationManager.delegate = (id)self;
self->locationManager startUpdatingLocation];
}