【问题标题】:Getting "Directions Not Available" calling Google Maps with URLWithString使用 URLWithString 调用 Google 地图时获取“路线不可用”
【发布时间】:2012-03-26 09:10:41
【问题描述】:

我有一个应用程序非常成功地使用了 Apple 地图:我有一个包含一些兴趣点的列表,如果我先点击其中一个,应用程序会在地图上显示它,然后,如果我点击它,它会打开 Apple 地图应用导航。

问题是,在第一次点击/启动时,应用程序会打开 Apple Maps,但无论是否请求使用用户位置的权限,Apple Maps 都不起作用并返回:“Directions Not Available . 在这些位置之间找不到方向。”但在 General Setting 中,我的应用的位置服务设置为 ON。

我认为我必须使用某种超时,因为我的应用程序需要很长时间来解析导航数据(我从 xml 检索数据)但在第二次点击时一切正常,即使我从 Mac 中运行它调试模式(甚至第二次解析xml)...

我希望我解释得很好。有什么提示吗?

   -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                      initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

    pinView.pinColor = MKPinAnnotationColorRed; 


    UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
    [myAccessoryButton setBackgroundColor:[UIColor clearColor]];
    [myAccessoryButton setImage:[UIImage imageNamed:@"flag.png"] forState:UIControlStateNormal];
    pinView.rightCalloutAccessoryView = myAccessoryButton;
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    [myAccessoryButton release];
} 
else {
    [mapView.userLocation setTitle:@"You're here"];
}
return pinView;
}

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
[locationManager startUpdatingLocation];

CLLocation *location = [locationManager location];

// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];

NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];


CLLocationCoordinate2D start = { [latitude floatValue], [longitude floatValue] };
CLLocationCoordinate2D end = {[posto.latitude floatValue], [posto.longitude floatValue]};

NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", start.latitude, start.longitude, end.latitude, end.longitude];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
}

【问题讨论】:

    标签: ios google-maps core-location


    【解决方案1】:

    calloutAccessoryControlTapped 中,您在调用startUpdatingLocation 后立即使用[locationManager location]

    location 通常在开始查找当前位置后立即不可用(如果是,它可能不可靠或已过时)。

    location 可用时,位置管理器将调用其didUpdateToLocation 委托方法,这就是您应该移动startUpdatingLocation 之后的所有代码的地方。

    (即使在那个委托方法中,在使用它之前检查newLocation 是否是nil 可能是个好主意。有时在地图视图等待用户授予位置服务权限时会发生这种情况(见this question))。

    -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        [locationManager startUpdatingLocation];
    }
    
    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        if (newLocation == nil)
        {
            NSLog(@"didUpdateToLocation: newLocation is nil");
            return;
        }
    
        //May want to check newLocation.horizontalAccuracy and 
        //newLocation.timestamp to see if the location is accurate 
        //enough for you.  If not, return and wait for a more 
        //accurate location (which may never come).
    
        //all the code currently after "startUpdatingLocation" goes here...
        CLLocation *location = [locationManager location];
        ...
    }
    
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"locationManager:didFailWithError: %@", error);
    }
    

    您可能还想实现didFailWithError 来处理获取位置的任何问题,或者至少知道它。

    【讨论】:

    • 我已经完成了你告诉我的更正,但现在应用程序正确地请求使用用户位置的权限,进入了一种循环:它从地图移动到应用程序,总是在 didUpdateToLocation 方法中输入:我忘了管理什么吗?
    • 是的,我忘了说:在调用 openURL 之前,请执行 [locationManager stopUpdatingLocation]; 否则,当它返回您的应用时,它会再次更新位置并继续返回地图应用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多