Cha*_*ltz 2 cocoa-touch objective-c ios6 ios6-maps
我对文档感到有点困惑.经过一些研究和实验,这就是我所拥有的.
if ([self canUseMKMapItem]) {
[self iosTheMap];
} else {
[self googleTheMap];
}
Run Code Online (Sandbox Code Playgroud)
使用它来检测我们是否可以使用IOS6映射功能:
- (BOOL) canUseMKMapItem {
Class itemClass = [MKMapItem class];
return (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]);
}
Run Code Online (Sandbox Code Playgroud)
这适用于IOS5,使用谷歌地图.它会自动将我们带到一个屏幕,其中包含从当前地址(如果用户允许)到目的地的路线列表.
- (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]];
}
Run Code Online (Sandbox Code Playgroud)
这是棘手的部分 - 这就是我想要使用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];
}
Run Code Online (Sandbox Code Playgroud)
这种"有效",有点像.它将用户带到一个带有显示地址的引脚的地图屏幕.用户可以点击它并获取路线.但是,我对这种方法有一些保留意见:
关于如何改进我的方法的任何建议?虽然它似乎有用,但我怀疑这不是正确的方法.
你很近.您需要使用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
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4317 次 |
| 最近记录: |