使用标签内的地址在地图上注释 2 个点。 - Objective C

Annotate 2 points on map using addresses inside labels. - Objective C

我想在地图上放置一个图钉,显示每个地址的位置。标签预先填充了数据,所以我假设注释代码需要放在 viewDidLoad 中。

为避免混淆并帮助澄清正确答案,标签被命名为 lblLeftAddresslblRightAddress

也对 bRight 地址重复相同的过程。

 // NSString *location = lblLeftAddress.text; I hust checked with your address it worked.
 NSString *location = @"296 Broadway Blvd Santa Monica CA 90016";
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:location
                     completionHandler:^(NSArray* placemarks, NSError* error){
                         if (placemarks && placemarks.count > 0) {
                             CLPlacemark *topResult = [placemarks objectAtIndex:0];
                             MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                             MKCoordinateRegion region = self.mapView.region;
                             region.span.longitudeDelta /= 8.0;
                             region.span.latitudeDelta /= 8.0;

                             [self.mapView setRegion:region animated:YES];
                             [self.mapView addAnnotation:placemark];
                         }
                     }
         ];

只需在此处更改源和目​​标中的坐标即可。

#pragma mark - Show Route Direction

-(void)loadRoute
{
    MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(34.0207504,-118.69193) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];

    MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source];
    [srcMapItem setName:@""];

    MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.757815,-122.5076406) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];

    MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination];
    [distMapItem setName:@""];

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
    [request setSource:srcMapItem];
    [request setDestination:distMapItem];
    [request setTransportType:MKDirectionsTransportTypeAutomobile];

    MKDirections *direction = [[MKDirections alloc]initWithRequest:request];

    [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

        NSLog(@"response = %@",response);
        NSArray *arrRoutes = [response routes];
        [arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            MKRoute *rout = obj;

            MKPolyline *line = [rout polyline];
            [self.mapView addOverlay:line];
            NSLog(@"Rout Name : %@",rout.name);
            NSLog(@"Total Distance (in Meters) :%f",rout.distance);

            NSArray *steps = [rout steps];

            NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);

            [steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                NSLog(@"Rout Instruction : %@",[obj instructions]);

            }];
        }];
    }];

}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{

    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        [renderer setStrokeColor:[UIColor blueColor]];
        [renderer setLineWidth:3.0];
        return renderer;
    }
    return nil;
}

更多参考可以访问这个here