//获取当前位置
MKMapItem *mylocation = [MKMapItem mapItemForCurrentLocation];
//当前经纬度
float currentLatitude = mylocation.placemark.location.coordinate.latitude;
float currentLongitude = mylocation.placemark.location.coordinate.longitude;
//设定为开始坐标
CLLocationCoordinate2D startCoor = CLLocationCoordinate2DMake(currentLatitude, currentLongitude);
//目的地位置
CLLocationCoordinate2D endCoor2D;
//中欧国际工商学院
endCoor2D.latitude = 31.232887369792;
endCoor2D.longitude = 121.6053765191;
//目的地坐标
CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(endCoor2D.latitude, endCoor2D.longitude);
//打开地图,先判断设备上有没有安装百度地图,如果有就打开百度地图,没有就打开系统自带地图
NSString *toName = @"中欧国际工商学院";
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit",
startCoor.latitude, startCoor.longitude, endCoor.latitude, endCoor.longitude, toName];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}else{
//打开系统自带地图
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
[MKMapItem openMapsWithItems:@[mylocation, toName]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}
//根据地点获取经纬度
NSString *oreillyAddress = @"中欧国际工商学院";
CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
[myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0 && error == nil) {
NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);
NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);
}
else if ([placemarks count] == 0 && error == nil) {
NSLog(@"Found no placemarks.");
} else if (error != nil) {
NSLog(@"An error occurred = %@", error);
}
}];
// 根据经纬度获取地点
CLLocationDegrees latitude = 31.237094;
CLLocationDegrees longitude = 121.596424;
CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
[clGeoCoder reverseGeocodeLocation:newLocation completionHandler: ^(NSArray *placemarks,NSError *error) {
for (CLPlacemark *placeMark in placemarks)
{
NSDictionary *addressDic = placeMark.addressDictionary;
NSString *city = [addressDic objectForKey:@"City"];
NSString *country = [addressDic objectForKey:@"Country"];
NSString *name = [addressDic objectForKey:@"Name"];
NSString *state = [addressDic objectForKey:@"State"];
NSString *street = [addressDic objectForKey:@"Street"];
NSString *subLocality = [addressDic objectForKey:@"SubLocality"];
NSString *subThoroughfare = [addressDic objectForKey:@"SubThoroughfare"];
NSString *thoroughfare = [addressDic objectForKey:@"Thoroughfare"];
NSArray *arr = [addressDic objectForKey:@"FormattedAddressLines"];
NSLog(@"city:%@",city);
NSLog(@"country:%@",country);
NSLog(@"name:%@",name);
NSLog(@"state:%@",state);
NSLog(@"street:%@",street);
NSLog(@"subLocality:%@",subLocality);
NSLog(@"subThoroughfare:%@",subThoroughfare);
NSLog(@"thoroughfare:%@",thoroughfare);
NSLog(@"FormattedAddressLines:%@",[arr firstObject]);
}
}];