ios開發中如何調用蘋果自帶地圖導航


  前段時間一直在趕項目,在外包公司工作就是命苦,天天加班不說,工作都是和工期合同掛鈎的,稍微逾期就有可能被扣獎金,不談這些傷腦筋的事情了,讓我們說說iOS開發中如何調用蘋果手機自帶的地圖。

  學習如逆水行舟,不進則退。古人告訴我們要不斷的反思和總結,日思則日精,月思則月精,年思則年精。只有不斷的嘗試和總結,才能讓我們的工作和生活更加輕松愉快和美好。連着做了兩個大的商城外包項目,智慧城市,搜牧通,花費了近四個月的時間,終於在反復修改后完美收工。期間的困難自不必說,以后多多總結和溝通吧。百度地圖的使用之前已經發表了一篇文章,說的很詳細了,這里不再涉及,言歸正傳,我們說一下如何調用蘋果自帶的地圖

  第一步:導入地圖文件  #import <MapKit/MapKit.h>

  第二步:獲取當前位置和目的地的經緯度,然后打開地圖即可

  

  //獲取當前位置

    MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];

    

    //當前經維度

    float currentLatitude=mylocation.placemark.location.coordinate.latitude;

    float currentLongitude=mylocation.placemark.location.coordinate.longitude;

    

    CLLocationCoordinate2D coords1 = CLLocationCoordinate2DMake(currentLatitude,currentLongitude);

    

    //目的地位置

    coordinate.latitude=[[dataSource objectForKey:@"lat"] floatValue];

    coordinate.longitude=[[dataSource objectForKey:@"lng"] floatValue];

    

    

    CLLocationCoordinate2D coords2 = coordinate;

    

    // ios6以下,調用google map

    if (SYSTEM_VERSION_LESS_THAN(@"6.0"))

    {

        NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d", coords1.latitude,coords1.longitude,coords2.latitude,coords2.longitude];

        NSURL *aURL = [NSURL URLWithString:urlString];

        //打開網頁google地圖

        [[UIApplication sharedApplication] openURL:aURL];

    }

    else

    // 直接調用ios自己帶的apple map

    {

        //當前的位置

        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

        //起點

        //MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords1 addressDictionary:nil]];

        //目的地的位置

        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];

        

        toLocation.name = @"目的地";

        NSString *myname=[dataSource objectForKey:@"name"];

        if (![XtomFunction xfunc_check_strEmpty:myname])

        {

            toLocation.name =myname;

        }

        

        NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil];

        NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };

        //打開蘋果自身地圖應用,並呈現特定的item

        [MKMapItem openMapsWithItems:items launchOptions:options];

    }

  

  通過這兩步就可以輕松的開啟蘋果自帶地圖導航,感覺真是挺不錯的,唯一的缺點是開啟地圖獲取路線信息耗費的手機流量比較大,最好在wifi條件下調用。如果不是必須,盡量還是用高德或者百度自帶的地圖就好。

 


免責聲明!

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



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