我可以在地图上显示多个位置或注释引脚。当用户点击它时,我会弹出一个带标题的弹出窗口。如何让用户在点击“>”时获取路线弹出窗口上的按钮
答案 0 :(得分:0)
你需要做两件事: 1.从地图引脚获取位置 2.打开Google地图网址。
以下是它的工作原理(其中一些来自here)
CLLocationCoordinate2D start = myMapView.userLocation.location.coordinate;
CLLocationCoordinate2D destination = [pinSelected.annotation coordinate];
NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
start.latitude, start.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
答案 1 :(得分:0)
代码在iOS 5.x和iOS 6.x上表现不同。假设注释是“toAnnotation”,下面的代码将起作用:
- (void)openMap
{
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { // iOS 6.x and later
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc]
initWithPlacemark:[[MKPlacemark alloc]
initWithCoordinate:CLLocationCoordinate2DMake(toAnnotation.coordinate.latitude, toAnnotation.coordinate.longitude)
addressDictionary:nil]];
toLocation.name = toAnnotation.title;
[MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil]
launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil]
forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]];
} else { // iOS 5.1 and earlier
NSMutableString *mapURL = [NSMutableString stringWithString:@"http://maps.google.com/maps?"];
[mapURL appendFormat:@"saddr=Current Location"];
[mapURL appendFormat:@"&daddr=%f,%f", toAnnotation.coordinate.latitude, toAnnotation.coordinate.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[mapURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
}
}
答案 2 :(得分:0)
首先你需要在视图中查看controller.h文件
- (IBAction)startWithOnePlacemark:(id)sender;
- (IBAction)startWithMultiplePlacemarks:(id)sender;
- (IBAction)startInDirectionsMode:(id)sender;
然后在Viewcontroller.m
- (IBAction)startWithOnePlacemark:(id)sender
{
CLLocationCoordinate2D bigBenLocation = CLLocationCoordinate2DMake(51.50065200, -0.12483300);
MKPlacemark *bigBenPlacemark = [[MKPlacemark alloc] initWithCoordinate:bigBenLocation addressDictionary:nil];
MKMapItem *bigBenItem = [[MKMapItem alloc] initWithPlacemark:bigBenPlacemark];
bigBenItem.name = @"Big Ben";
[bigBenItem openInMapsWithLaunchOptions:nil];
// Note: use initWithPlacemark: to initialize with CLPlacemark
}
- (IBAction)startWithMultiplePlacemarks:(id)sender
{
CLLocationCoordinate2D bigBenLocation = CLLocationCoordinate2DMake(51.50065200, -0.12483300);
MKPlacemark *bigBenPlacemark = [[MKPlacemark alloc] initWithCoordinate:bigBenLocation addressDictionary:nil];
MKMapItem *bigBenItem = [[MKMapItem alloc] initWithPlacemark:bigBenPlacemark];
bigBenItem.name = @"Big Ben";
CLLocationCoordinate2D westminsterLocation = CLLocationCoordinate2DMake(51.50054300, -0.13570200);
MKPlacemark *westminsterPlacemark = [[MKPlacemark alloc] initWithCoordinate:westminsterLocation addressDictionary:nil];
MKMapItem *westminsterItem = [[MKMapItem alloc] initWithPlacemark:westminsterPlacemark];
westminsterItem.name = @"Westminster Abbey";
NSArray *items = [[NSArray alloc] initWithObjects:bigBenItem, westminsterItem, nil];
[MKMapItem openMapsWithItems:items launchOptions:nil];
}
- (IBAction)startInDirectionsMode:(id)sender
{
CLLocationCoordinate2D bigBenLocation = CLLocationCoordinate2DMake(51.50065200, -0.12483300);
MKPlacemark *bigBenPlacemark = [[MKPlacemark alloc] initWithCoordinate:bigBenLocation addressDictionary:nil];
MKMapItem *bigBenItem = [[MKMapItem alloc] initWithPlacemark:bigBenPlacemark];
bigBenItem.name = @"Big Ben";
CLLocationCoordinate2D westminsterLocation = CLLocationCoordinate2DMake(51.50054300, -0.13570200);
MKPlacemark *westminsterPlacemark = [[MKPlacemark alloc] initWithCoordinate:westminsterLocation addressDictionary:nil];
MKMapItem *westminsterItem = [[MKMapItem alloc] initWithPlacemark:westminsterPlacemark];
westminsterItem.name = @"Westminster Abbey";
NSArray *items = [[NSArray alloc] initWithObjects:bigBenItem, westminsterItem, nil];
NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking};
[MKMapItem openMapsWithItems:items launchOptions:options];
}
添加Mapkit,AddressBook,CoreLocation Framework