前段时间一直在赶项目,在外包公司工作就是命苦,天天加班不说,工作都是和工期合同挂钩的,稍微逾期就有可能被扣奖金,不谈这些伤脑筋的事情了,让我们说说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条件下调用。如果不是必须,尽量还是用高德或者百度自带的地图就好。