【问题标题】:Draw line between two annotation pins on map using address strings/geocoding. -Objective C使用地址字符串/地理编码在地图上的两个注释引脚之间画线。 -目标C
【发布时间】:2016-12-21 05:00:13
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c google-maps dictionary


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 感谢您的回复。不幸的是,注释/地址是基于用户输入的动态的。它们可以是该国的任何地址。我只是以洛杉矶和芝加哥为例。有什么方法可以通过获取每个地址字符串的纬度和经度来填充坐标数组?
      • 以任何你想要的方式填充数组。这里没有任何东西以固定字符串数组为前提。
      • 所以在你的代码中,我可以用“lblLeftAddress.text 和 lblRightAddress.text”替换 @"Los Angeles, CA", @"Chicago, IL" 吗?
      • 可能会检查以确保那里确实有文本等,但是是的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      相关资源
      最近更新 更多