在ios6中調用蘋果地圖用於導航

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

http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f 會提示無法定位,不知道爲什麼?

使用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)


如果不想使用蘋果自帶的地圖的話,也可以使用第三方的地圖,如百度;
百度地圖的URI API:http://developer.baidu.com/map/uri-intro.htm#idmykey2
使用如下:

baidumap://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving&region=西安 //調起百度PC或Web地圖,展示“西安市”從(34.264642646862,108.95108518068 )“我家”到“大雁塔”的駕車路線

注意調用前,需要判斷是否已安裝百度地圖: 使用

if ([[UIApplicationsharedApplication] canOpenURL:[NSURLURLWithString:@"baidumap://map/"]]){ }

其他的如高德、搜狗、圖吧等沒有找到對應的URI API。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章