前段時(shí)間一直在趕項(xiàng)目挎扰,在外包公司工作就是命苦,天天加班不說(shuō)巢音,工作都是和工期合同掛鉤的遵倦,稍微逾期就有可能被扣獎(jiǎng)金,不談這些傷腦筋的事情了官撼,讓我們說(shuō)說(shuō)iOS開發(fā)中如何調(diào)用蘋果手機(jī)自帶的地圖梧躺。
第一步:導(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)頁(yè)google地圖
[[UIApplication sharedApplication] openURL:aURL];
}
else
// 直接調(diào)用ios自己帶的apple map
{
//當(dāng)前的位置
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
//起點(diǎn)
//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];
}
通過(guò)這兩步就可以輕松的開啟蘋果自帶地圖導(dǎo)航囊卜,感覺(jué)真是挺不錯(cuò)的,唯一的缺點(diǎn)是開啟地圖獲取路線信息耗費(fèi)的手機(jī)流量比較大错沃,最好在wifi條件下調(diào)用栅组。如果不是必須,盡量還是用高德或者百度自帶的地圖就好枢析。