gpt4 book ai didi

ios - 获取 "Directions Not Available"使用 URLWithString 调用谷歌地图

转载 作者:行者123 更新时间:2023-11-29 11:15:01 24 4
gpt4 key购买 nike

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

问题是,在第一次点击/启动应用程序时,它会打开 Apple map ,但无论是请求使用用户位置的许可还是不请求,Apple map 都不起作用并返回:“方向不可用. 在这些位置之间找不到方向。”但在常规设置中,我的应用程序的位置服务设置为开启。

我想我必须使用一种超时,因为我的应用程序需要很长时间来解析导航数据(我从 xml 检索数据)但是在第二次点击时一切正常,即使我从 Mac 运行它 Debug模式(它甚至第二次解析 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]];
}

最佳答案

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

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

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

(即使在那个委托(delegate)方法中,在使用它之前检查 newLocation 是否为 nil 可能是个好主意。它有时会在 map View 等待时发生供用户授予定位服务权限(参见 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 来处理获取位置的任何问题,或者至少要知道它。

关于ios - 获取 "Directions Not Available"使用 URLWithString 调用谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9869392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com