溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ios開發(fā)中如何調(diào)用蘋果自帶地圖導(dǎo)航

發(fā)布時間:2020-06-07 11:50:37 來源:網(wǎng)絡(luò) 閱讀:1882 作者:kuuailetianzi 欄目:移動開發(fā)

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

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

  第一步:導(dǎo)入地圖文件  #import <MapKit/MapKit.h>

  第二步:獲取當(dāng)前位置和目的地的經(jīng)緯度,然后打開地圖即可

  

  //獲取當(dāng)前位置

    MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];

    

    //當(dāng)前經(jīng)維度

    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以下,調(diào)用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];

        //打開網(wǎng)頁google地圖

        [[UIApplication sharedApplication] openURL:aURL];

    }

    else

    // 直接調(diào)用ios自己帶的apple map

    {

        //當(dāng)前的位置

        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 };

        //打開蘋果自身地圖應(yīng)用,并呈現(xiàn)特定的item

        [MKMapItem openMapsWithItems:items launchOptions:options];

    }

  

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


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI