1. 程式人生 > >ios 呼叫系統的地圖

ios 呼叫系統的地圖

NSString *string = @"http://maps.apple.com/maps?saddr=39.98,116.31&daddr=41.59,117.40";
[[UIApplication sharedApplication]  openURL:[NSURL URLWithString:string]];

ios6以下我們一般用google地圖來導航,但ios6中呼叫會有點問題,會開啟web瀏覽器再詢問之類的,不直觀友好。所以在ios6中建議直接用apple map。本來呼叫apple map應該和呼叫google map類似,但使用:

使用maps://saddr=%f,%f&daddr=%f,%f 會找不到當前位置,也不清楚原因?

不過還是找到了解決方法,如下(包括兩種地圖呼叫方式)

但是有時候會有問題
if (SYSTEM_VERSION_LESS_THAN(@”6.0”)) { // ios6以下,呼叫google map
NSString *urlString = [[NSString alloc]
initWithFormat:@”http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d“,
cutLat,cutLon,stationLat,stationLon];
NSURL *aURL = [NSURL URLWithString:urlString];
[urlString release];
[[UIApplication sharedApplication] openURL:aURL];
} else { // 直接呼叫ios自己帶的apple map
CLLocationCoordinate2D to;
to.latitude = stationLat;
to.longitude = stationLon;
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[[MKPlacemark alloc] initWithCoordinate:to addressDictionary:nil] autorelease]];
toLocation.name = @”Destination”;
[MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil] launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil] forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]];
[toLocation release];
}

define

SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice
currentDevice]
systemVersion]
compare:v
options:NSNumericSearch] ==
NSOrderedAscending)