您的位置:首页 > 移动开发 > IOS开发

iOS 根据地址名称 调取系统导航

2017-01-04 13:54 393 查看
- (IBAction)goNav:(id)sender {

  __block 
CLLocationCoordinate2D coordinate;

    CLGeocoder *myGeocoder = [[CLGeocoder
alloc] init];

    [myGeocoder geocodeAddressString:self.setTF.text
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);

            coordinate = CLLocationCoordinate2DMake(firstPlacemark.location.coordinate.latitude,
firstPlacemark.location.coordinate.longitude) ;

        }

        else
if ([placemarks count] ==
0 && error == nil) {

            NSLog(@"Found no placemarks.");

        }

        else
if (error != nil) {

            NSLog(@"An error occurred = %@", error); }

    }];

    [CommonTool
gotoMapNavigationWithEndLocation:coordinate
andName:self.setTF.text];

}

+ (void)gotoMapNavigationWithEndLocation:(CLLocationCoordinate2D)endLocation andName:(NSString
*)name{

    //起点

    MKMapItem *currentLocation = [MKMapItem
mapItemForCurrentLocation];

    //目的地的位置

    MKMapItem *toLocation = [[MKMapItem
alloc] initWithPlacemark:[[MKPlacemark
alloc] initWithCoordinate:[CommonTool
GCJ02FromBD09:endLocation]
addressDictionary:nil]];

    

    currentLocation.name =
@"我的位置";

    toLocation.name = name;

    

    NSArray *items = [NSArray
arrayWithObjects:currentLocation, toLocation,
nil];

    NSDictionary *options =
@{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey: [NSNumber
numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:@YES
};

   
//打开苹果自身地图应用,并呈现特定的item

    [MKMapItem
openMapsWithItems:items
launchOptions:options];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: