Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用地址字符串/地理编码在地图上的两个注释引脚之间绘制线-目标C_Ios_Objective C_Google Maps_Dictionary - Fatal编程技术网

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

Ios 使用地址字符串/地理编码在地图上的两个注释引脚之间绘制线-目标C,ios,objective-c,google-maps,dictionary,Ios,Objective C,Google Maps,Dictionary,我想根据地址字符串在两个注释引脚之间画一条线。地址标签预先填充了来自用户的数据,可以是美国的任何位置。我已经在每个地址上有了注释,我只是不确定坐标数组(lat,long)应该放什么,以便在这两点之间画一条线。我附上了2个截图,以帮助澄清和显示我收到的错误 以下是我对每个地址的注释: NSString *location = [(UILabel *)[[self view] viewWithTag:1]text];; CLGeocoder *geocoder = [[CLGeocoder

我想根据地址字符串在两个注释引脚之间画一条线。地址标签预先填充了来自用户的数据,可以是美国的任何位置。我已经在每个地址上有了注释,我只是不确定坐标数组(lat,long)应该放什么,以便在这两点之间画一条线。我附上了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];
CLLocationCoordinate2D coordinateArray[2];

CoordinarRay[0]=CLLocationCoordinate2DMake(lat1,lon1) 您应该只构建一个坐标数组,也许可以从要添加到地图的注释数组中填充它。例如:

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


还要注意,API规定不应执行并发地理代码请求,因此我将一个请求放入另一个的完成处理程序。

只需更改源和目标的坐标即可

#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;
}
#pragma标记-显示路线方向
-(无效)装货路线
{
MKPlacemark*source=[[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(34.0207504,-118.69193)addressDictionary:[NSDictionary Dictionary WithObjectsandKeys:@“@”,“nil]”;
MKMapItem*srcMapItem=[[MKMapItem alloc]initWithPlacemark:source];
[srcMapItem setName:@”“;
MKPlacemark*destination=[[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.757815,-122.5076406)地址字典:[NSDictionary Dictionary WithObjectsandKeys:@“@”,“nil]”;
MKMapItem*distMapItem=[[MKMapItem alloc]initWithPlacemark:destination];
[distMapItem setName:@”“;
MKDirectionsRequest*请求=[[MKDirectionsRequest alloc]init];
[请求设置源:srcMapItem];
[请求设置目标:distMapItem];
[请求setTransportType:MKDirectionsTransportTypeAutomobile];
MKDirections*direction=[[MKDirections alloc]initWithRequest:request];
[方向计算方向WithCompletionHandler:^(MKDirectionsResponse*响应,NSError*错误){
NSLog(@“response=%@”,response);
NSArray*arrRoutes=[响应路由];
[ArrObjectSusingBlock:^(id obj,整数idx,布尔*停止){
MKRoute*rout=obj;
MKPolyline*line=[rout polyline];
[self.mapView addOverlay:line];
NSLog(@“路由名称:%@”,路由名称);
NSLog(@“总距离(米):%f”,路线距离);
NSArray*步数=[rout steps];
NSLog(@“总步数:%lu”,(无符号长)[步数];
[步骤enumerateObjectsUsingBlock:^(id obj、整数idx、布尔*停止){
NSLog(@“路由指令:%@,[obj指令]);
}];
}];
}];
}
-(MKOverlayRenderer*)地图视图:(MKMapView*)地图视图渲染器ForOverlay:(id)overlay
{
if([overlay iskindof类:[MKPolyline类]]){
MKPolylineRenderer*渲染器=[[MKPolylineRenderer alloc]initWithOverlay:overlay];
[渲染器setStrokeColor:[UIColor blueColor]];
[渲染器设置线宽:3.0];
返回渲染器;
}
返回零;
}

谢谢您的回复。不幸的是,注释/地址是基于用户输入的动态的。他们可以是这个国家的任何地址。我只是以洛杉矶和芝加哥为例。是否有任何方法可以通过获取每个地址字符串的纬度和经度来填充坐标数组?可以按任何方式填充数组。这里没有固定字符串数组的谓词。因此,在您的代码中,我是否可以将@“Los Angeles,CA”@“Chicago,IL”替换为“lblLeftAddress.text和lblRightAddress.text?可能需要检查以确保确实存在文本,等等,但是可以。
#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;
}