使用地址字符串/地理编码在地图上的两个注释引脚之间绘制线。 - 目标C.

时间:2016-08-14 23:15:35

标签: ios objective-c google-maps dictionary

Error Screenshot

Map Screenshot

我想基于地址字符串在两个注释引脚之间画一条线。地址标签预填充来自用户的数据,并且可以是美国的任何位置。我已经在每个地址上都有一个注释,我只是不确定要为坐标数组(lat,long)放置什么,以便在这两个点之间画一条线。我附上了两个截图,以帮助澄清并显示我收到的错误。

以下是我如何注释每个地址:

     NSString *location = [(UILabel *)[[self view] viewWithTag:1]text];;
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;


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

NSString *location2 = [(UILabel *)[[self view] viewWithTag:6]text];;
CLGeocoder *geocoder2 = [[CLGeocoder alloc] init];
[geocoder2 geocodeAddressString:location2
             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;

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


                 }
             }
 ];

这是错误,我不知道在坐标数组中为纬度和经度添加什么:

      CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] =  CLLocationCoordinate2DMake(lat1, lon1);<- NEED HELP HERE
coordinateArray[2] = CLLocationCoordinate2DMake(lat2, lon2);<- NEED HELP HERE


self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
[self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible

[self.mapView addOverlay:self.routeLine];

1 个答案:

答案 0 :(得分:1)

您应该只构建一个坐标数组,或者从您要添加到地图的注释数组中填充它。例如:

NSArray <NSString*> *addressStrings = @[@"Los Angeles, CA", @"Chicago, IL"];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];

NSMutableArray <MKPlacemark *> *annotations = [NSMutableArray array];

// first request

[geocoder geocodeAddressString:addressStrings[0] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    if (placemarks.count == 0) {
        NSLog(@"didn't annotation for %@", addressStrings[1]);
        return;
    }

    [annotations addObject:[[MKPlacemark alloc] initWithPlacemark:placemarks[0]]];

    // second request

    [geocoder geocodeAddressString:addressStrings[1] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count == 0) {
            NSLog(@"didn't annotation for %@", addressStrings[1]);
            return;
        }

        [annotations addObject:[[MKPlacemark alloc] initWithPlacemark:placemarks[0]]];

        // when both are done

        CLLocationCoordinate2D coordinates[2];
        coordinates[0] = annotations[0].coordinate;
        coordinates[1] = annotations[1].coordinate;
        MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:2];
        [self.mapView addAnnotations:annotations];
        [self.mapView addOverlay:polyline];
        [self.mapView showAnnotations:annotations animated:true];
    }];
}];

当然,这假定您相应地设置地图视图的委托并实施rendererForOverlay

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        renderer.lineWidth = 4;
        renderer.strokeColor = [[UIColor cyanColor] colorWithAlphaComponent:0.75];
        return renderer;
    }
    return nil;
}

chicago to la

另请注意,API规定您不应执行并发地理编码请求,因此我将其中一个放在另一个请求的完成处理程序中。