【发布时间】: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];
}
这有点“有效”。它将用户带到带有显示地址的图钉的地图屏幕。用户可以点击它并获取路线。但是,我对这种方法有一些保留意见:
- 我在设置 kABPersonAddressStreetKey 时出现编译器警告:“不兼容的指针类型将 'const CFStringRef'(又名 'const struct __CFString *const')发送到 'NSString *' 类型的参数”
- 我使用的字符串值是完整地址。我使用该值作为街道地址,尽管地址值更具有原子性——街道、城市、州。它似乎有效,但我担心这不是正确的做法。
- 如果 Apple 地图显示商家/目的地的名称,而不仅仅是地址,那就太好了。
- 如果让 Apple 地图自动显示路线,而不是带有目的地点的地图,这样用户只需轻点几下就可以了。
对于如何改进我的方法有什么建议吗?虽然看起来可行,但我怀疑这不是正确的方法。
【问题讨论】:
标签: objective-c cocoa-touch ios6 ios6-maps