我可以在地圖上顯示多個位置或註釋針。當用戶點擊它時,我會彈出一個標題。當點擊彈出框上的「>」按鈕時,我如何允許用戶獲取路線如何讓用戶點擊地圖註釋針時「獲取路線」
0
A
回答
0
您需要做兩件事: 1.從地圖針腳獲取位置 2.打開Google地圖URL。
下面是它如何工作的(有的來自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]];
0
代碼的行爲在iOS 5.x和6.x中的iOS不同假設註釋是 「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]]];
}
}
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];
}
add Mapkit,AddressBook,CoreLocation Framework
相關問題
- 1. 獲取谷歌地圖中用戶點擊的兩點之間的路線
- 2. Swift Firebase在地圖註釋上下載註釋圖片點擊
- 3. Google地圖/ Places API - 在用戶點擊時獲取地址place_id?
- 4. 如何獲得指數particlular註釋針的點擊在iphone
- 5. 獲取註釋針點擊事件MapKit Swift
- 6. 如何獲取iPhone地圖上點擊的針腳?
- 7. 如何向地圖添加多個針點註釋
- 8. Google地圖獲取路線
- 9. 如何讓用戶點擊iOS地圖以快速顯示標註?
- 10. 當點擊註釋時變暗地圖視圖
- 11. 當點擊地圖註釋時顯示另一個視圖
- 12. 上的註釋時,點擊推視圖
- 13. 雙擊地圖註釋
- 14. 如何在點擊tableview中的單元格的同時在地圖視圖中獲取註釋?
- 15. 如何在用戶單擊註釋時顯示視圖?
- 16. 如何讓用戶輸入html註釋
- 17. 如何檢測地圖上的點擊註釋Pin?
- 18. 如何獲取用戶點擊地圖時的經度和緯度?
- 19. 如何在點擊時獲取圖層?
- 20. 地圖註釋顯示所有點的所有圖像/針腳
- 21. ios mapkit通過點擊地圖關閉註釋標註
- 22. 如何在Xamarin.Forms中點擊地圖時獲取座標?
- 23. iOS:如何使用Google地圖根據用戶輸入獲取路線?
- 24. 點擊地圖後獲取地址
- 25. 使用節點的GoogleMaps從谷歌獲取路線地圖
- 26. 用google地圖上的按鈕點擊獲取用戶位置
- 27. 如何在地圖上點擊鏈接地址獲取地址?
- 28. MKMapView註釋圖像更改爲針點擊
- 29. 通過谷歌地圖查找路線時添加地址註釋api
- 30. 檢查在mkmapview上點擊了哪個地圖視圖註釋