【问题标题】:Programmatically getting directions using Apple Maps使用 Apple 地图以编程方式获取路线
【发布时间】:2012-09-24 15:34:11
【问题描述】:

我对文档有点困惑。经过一些研究和实验,这就是我所拥有的。

if ([self canUseMKMapItem]) {
            [self iosTheMap];
        } else {
            [self googleTheMap];
        }

以此来检测是否可以使用IOS6映射功能:

- (BOOL) canUseMKMapItem {
    Class itemClass = [MKMapItem class];
    return (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]);
}

这适用于 IOS5,使用 Google 地图。它会自动将我们带到一个屏幕,其中包含从当前地址(如果用户允许)到目的地的路线列表。

- (void)googleTheMap
{
    NSNumber *userLat = [[NSNumber alloc]initWithDouble:mapView.userLocation.coordinate.latitude];
    NSNumber *userLong = [[NSNumber alloc]initWithDouble:mapView.userLocation.coordinate.longitude];

    NSMutableString *queryString = [NSMutableString stringWithFormat:@"http://maps.google.com/?saddr=%@,%@&daddr=",userLat,userLong];
    NSString *address = [partnerObject valueForKey:ATTRIBUTE_ADDRESS];
    [queryString appendString:address];
    NSString *escaped = [queryString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];
}

这是棘手的部分——这就是我尝试使用 Apple Maps 所做的事情

- (void)iosTheMap {
    NSNumber * latitude = [partnerObject valueForKey:ATTRIBUTE_LATITUDE];
    NSNumber * longitude = [partnerObject valueForKey:ATTRIBUTE_LONGITUDE];
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = latitude.doubleValue;
    coordinate.longitude = longitude.doubleValue;
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
    [addressDictionary setValue:[partnerObject valueForKey:ATTRIBUTE_ADDRESS] forKey:kABPersonAddressStreetKey];
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:addressDictionary];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem openInMapsWithLaunchOptions:nil];
}

这有点“有效”。它将用户带到带有显示地址的图钉的地图屏幕。用户可以点击它并获取路线。但是,我对这种方法有一些保留意见:

  1. 我在设置 kABPersonAddressStreetKey 时出现编译器警告:“不兼容的指针类型将 'const CFStringRef'(又名 'const struct __CFString *const')发送到 'NSString *' 类型的参数”
  2. 我使用的字符串值是完整地址。我使用该值作为街道地址,尽管地址值更具有原子性——街道、城市、州。它似乎有效,但我担心这不是正确的做法。
  3. 如果 Apple 地图显示商家/目的地的名称,而不仅仅是地址,那就太好了。
  4. 如果让 Apple 地图自动显示路线,而不是带有目的地点的地图,这样用户只需轻点几下就可以了。

对于如何改进我的方法有什么建议吗?虽然看起来可行,但我怀疑这不是正确的方法。

【问题讨论】:

    标签: objective-c cocoa-touch ios6 ios6-maps


    【解决方案1】:

    你很接近。您需要使用 MKLaunchOptionsDirectionsModeDriving、MKLaunchOptionsDirectionsModeKey 值和键为 openInMapsWithLaunchOptions 函数指定 Launch Options 字典。

        Class itemClass = [MKMapItem class];
        if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
            // Use iOS 6 maps
            CLLocationCoordinate2D coordinate = [((LocationAnnotation*)[map.annotations objectAtIndex:0]) coordinate];
            MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
            MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
            [mapItem openInMapsWithLaunchOptions:[NSDictionary dictionaryWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsDirectionsModeKey, nil]];
        } else {
            //Fall back on google
            ...
        }
    

    【讨论】:

    • 请注意,我的特定地图始终只有 1 个注释,并且我的 LocationAnnotation 子类具有可用的坐标。您可以对其进行修改以满足您的需要,但这里的关键是选项字典值。
    猜你喜欢
    • 1970-01-01
    • 2016-02-20
    • 2021-12-12
    • 1970-01-01
    • 2016-04-19
    • 2011-06-24
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多