获取“方向不可用”,使用URLWithString调用Google地图

时间:2012-03-26 09:10:41

标签: ios google-maps core-location

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

问题是,在第一次点击/启动时,应用程序会打开Apple Maps,但如果它要求使用用户位置的权限,则不会,Apple Maps无法运行并返回:“方向不可用在这些位置之间找不到方向。“但在常规设置中,我的应用程序的位置服务设置为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]];
}

1 个答案:

答案 0 :(得分:2)

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

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来处理获取该位置的任何问题或至少了解它。