gpt4 book ai didi

ios - 使用地址字符串/地理编码在 map 上的两个注释引脚之间画线。 - objective-c

转载 作者:行者123 更新时间:2023-12-01 20:05:36 25 4
gpt4 key购买 nike

Error Screenshot

Map Screenshot

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

这是我注释每个地址的方式:

     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];

最佳答案

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

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];
}];
}];

当然,这假设您相应地为 map View 设置了委托(delegate)并实现了 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 规定您不应执行并发地理编码请求,因此我将一个放在另一个的完成处理程序中。

关于ios - 使用地址字符串/地理编码在 map 上的两个注释引脚之间画线。 - objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com