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

来源:互联网 发布:crm软件有哪些 编辑:程序博客网 时间:2024/05/01 05:18

- (IBAction)goNav:(id)sender {

  __block CLLocationCoordinate2D coordinate;

    CLGeocoder *myGeocoder = [[CLGeocoderalloc] init];

    [myGeocoder geocodeAddressString:self.setTF.textcompletionHandler:^(NSArray *placemarks,NSError *error) {

        if ([placemarkscount] > 0 && error ==nil) {

            NSLog(@"Found %lu placemark(s).", (unsignedlong)[placemarks count]);

            CLPlacemark *firstPlacemark = [placemarksobjectAtIndex: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) ;

        }

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

            NSLog(@"Found no placemarks.");

        }

        elseif (error != nil) {

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

    }];

    [CommonToolgotoMapNavigationWithEndLocation:coordinateandName:self.setTF.text];

}


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

    //起点

    MKMapItem *currentLocation = [MKMapItemmapItemForCurrentLocation];

    //目的地的位置

    MKMapItem *toLocation = [[MKMapItemalloc] initWithPlacemark:[[MKPlacemarkalloc] initWithCoordinate:[CommonToolGCJ02FromBD09:endLocation]addressDictionary:nil]];

    

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

    toLocation.name = name;

    

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

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

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

    [MKMapItemopenMapsWithItems:items launchOptions:options];

}




0 0
原创粉丝点击