iOS6地圖“查看路線”、導航功能的實現


在iOS6之前,蘋果自帶的是Google地圖,所以“查看路線”的功能可以通過訪問google的url來實現:

url 格式為:“http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f”

 

不過iOS6之后,蘋果使用了自家地圖,以上的方式就只能跳到google 地圖網頁版,不過體驗不太好,

還是希望調用本地應用,於是找到了官方文檔:

http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html

 

照文檔描述,iOS6中的調用應該為:

訪問url:“http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f”

不過會提示服務器不可用,估計后面蘋果又做更改了。

 

后來,找到網友的一篇文章,http://autumnice.blog.163.com/blog/static/5552002012912113247614/

在iOS6中調用地圖API的方式可以實現,貼代碼如下:

View Code
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0) { // ios6以下,調用google map
        NSString *theString = [NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",settings.update_latitude,settings.update_longitude, __latitude, __longitude];
        theString =  [theString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
        
        NSURL *url = [[[NSURL alloc] initWithString:theString] autorelease];
        [[UIApplication sharedApplication] openURL:url];
    } else { // 直接調用ios自己帶的apple map
        CLLocationCoordinate2D to;
        
        
        to.latitude = __latitude;
        to.longitude = __longitude;
        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];
    }

 

------------------

如果不想使用蘋果自帶的地圖的話,也可以使用第三方的地圖,如百度;

百度地圖的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 )“我家”到“大雁塔”的駕車路線。

 

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

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM