Ios 获得;指示不可用“;使用URLWithString调用Google地图

Ios 获得;指示不可用“;使用URLWithString调用Google地图,ios,google-maps,core-location,Ios,Google Maps,Core Location,我有一个非常成功地使用Apple Maps的应用程序:我有一个带有一些兴趣点的列表,如果我先点击其中一个,应用程序会在地图上显示它,然后,如果我点击它,它会打开Apple Maps应用程序进行导航 问题是,在第一次点击/启动应用程序时,它会打开Apple Maps,但如果它请求使用用户位置的权限,或者如果没有,Apple Maps都不会工作,并返回:“方向不可用。在这些位置之间找不到方向。”但通常情况下,我的应用程序的位置服务设置为打开 我认为我必须使用一种超时,因为我的应用程序解析导航数据(我

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

问题是,在第一次点击/启动应用程序时,它会打开Apple Maps,但如果它请求使用用户位置的权限,或者如果没有,Apple Maps都不会工作,并返回:“方向不可用。在这些位置之间找不到方向。”但通常情况下,我的应用程序的位置服务设置为打开

我认为我必须使用一种超时,因为我的应用程序解析导航数据(我从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]];
}
-(MKAnnotationView*)地图视图:(MKMapView*)mV视图用于注释:(id)注释
{
MKPinAnnotationView*pinView=nil;
if(注释!=mapView.userLocation)
{
静态NSString*defaultPinID=@“com.invasivecode.pin”;
pinView=(MKPinAnnotationView*)[mapView出列重用AnnotationViewWithIdentifier:defaultPinID];
如果(pinView==nil)pinView=[[[MKPinAnnotationView alloc]
initWithAnnotation:annotation重用标识符:defaultPinID]autorelease];
pinView.pinColor=MKPinAnnotationColorRed;
UIButton*myAccessoryButton=[[UIButton alloc]initWithFrame:CGRectMake(0,0,24,24)];
[myAccessoryButton setBackgroundColor:[UIColor clearColor]];
[myAccessoryButton setImage:[UIImage ImageName:@“flag.png”]用于状态:UIControlStateNormal];
pinView.rightCalloutAccessoryView=myAccessoryButton;
pinView.canShowCallout=是;
pinView.animatesDrop=是;
[myAccessoryButton释放];
} 
否则{
[mapView.userLocation setTitle:@“您在这里”];
}
返回pinView;
}
-(无效)地图视图:(MKMapView*)地图视图注释视图:(MKAnnotationView*)视图调用访问控制点击:(UIControl*)控制{
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
locationManager.desiredAccuracy=KCallocationAccuracyBest;
locationManager.distanceFilter=KCLDistanceFilterOne;
[locationManager startUpdatingLocation];
CLLocation*location=[locationManager位置];
//使用来自位置的信息配置新事件
CLLocationCoordinate2D坐标=[位置坐标];
NSString*纬度=[NSString stringWithFormat:@“%f”,坐标.纬度];
NSString*经度=[NSString stringWithFormat:@“%f”,坐标.经度];
CLLocationCoordinate2D start={[纬度浮点值],[经度浮点值]};
CLLocationCoordinate2D end={[posto.latitude浮点值],[posto.latitude浮点值]};
NSString*googleMapsURLString=[NSString stringWithFormat:@”http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f“,开始.纬度,开始.经度,结束.纬度,结束.经度];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:googleMapsURLString]];
}

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

开始查找当前位置后,
位置通常不可用(如果是,则可能不可靠或过时)

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

(即使在该委托方法中,最好在使用前检查
newLocation
是否为
nil
。地图视图在等待用户授予位置服务权限时有时会发生这种情况(请参阅))


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

我已经完成了您告诉我的更正,但现在该应用程序正确地请求使用用户位置的权限,进入一种循环:它从地图移动到应用程序,始终以DidUpdateLocation方法输入:我是否忘记管理某些内容?是的,我忘记提到:在调用openURL之前,执行
[locationManager StopUpdateLocation]否则,当它返回到您的应用程序时,它会再次更新位置并继续返回到“地图”应用程序。
-(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);
}