首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个引脚计算路由

使用多个引脚计算路由
EN

Stack Overflow用户
提问于 2012-03-14 03:11:06
回答 1查看 293关注 0票数 1

在我的应用程序中,我有一个带有几个自定义引脚的mapView。

我正在尝试这样做:在showCallout上出现一个AlertView,告诉您是否要计算路由。这是有效的,但只适用于一个位置。或者更好的是,只调用一个方法,所以我只有一个目的地。

以下是我的代码(我有9个目的地,但为了使代码更简短,我只发布了2个):

代码语言:javascript
复制
- (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];
}
}

问题是,只有第一个

代码语言:javascript
复制
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation    
*)newLocation fromLocation:(CLLocation *)oldLocation

是调用的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-14 04:31:14

对于正在使用的每个位置管理器实例,不能对委托方法使用不同的名称。每个位置管理器仍将调用CLLocationManagerDelegate协议定义的方法。

您可以通过检查manager参数是否等于您正在创建的实例之一来检查哪个管理器正在调用(例如,if (manager == locationManagerMori))。

但是您不需要从一开始就为每个注释创建单独的位置管理器实例。

相反,只保留一个位置管理器实例,在委托方法中,您可以找出当前选择的注释是什么,并在url字符串中使用它的坐标。例如:

代码语言:javascript
复制
- (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:

代码语言:javascript
复制
if (buttonIndex == alertView.firstOtherButtonIndex)
{
    self->locationManager = [[CLLocationManager alloc] init];
    self->locationManager.delegate = (id)self;
    self->locationManager startUpdatingLocation];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9690476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档