ios開發中如何調用蘋果內建地圖導航,ios蘋果

來源:互聯網
上載者:User

ios開發中如何調用蘋果內建地圖導航,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條件下調用。如果不是必須,盡量還是用高德或者百度內建的地圖就好。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.