2015-10-06 52 views
0

enter image description here我正在使用谷歌地圖ios sdk,我的問題是我必須得到座標在地圖上的多邊線每1000米。現在我能夠獲得給定路徑中的位置數並能夠使用以下代碼片段訪問它們。如何獲得在一個路徑中的特定距離座標

-(NSMutableArray*)getCoordinates { 

    pathCoordinatesArray = [[NSMutableArray alloc]init]; 
    GMSMutablePath *path = [VTDirectionManager getPath]; 
    NSLog(@"count %d",path.count); 

    for (int i=0 ; i<path.count; i++) { 

     if (i+1 > path.count) { 
      return pathCoordinatesArray; 
     } 
     for (int j = i+1; j<path.count; j++) { 

      CLLocationCoordinate2D sourceCoordinate = [path coordinateAtIndex:i]; 
      CLLocation *sourceLocation = [[CLLocation alloc]initWithLatitude:sourceCoordinate.latitude longitude:sourceCoordinate.longitude]; 

      CLLocationCoordinate2D destinationCoordinate = [path coordinateAtIndex:j]; 
         CLLocation *destinationLocation = [[CLLocation alloc]initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude]; 

      BOOL check ; 

      check = [self checkDistanceForSource:sourceLocation andDestination:destinationLocation]; 

      //jump to next 1000 distance position 

      if (check) { 
       i = j; 
      } 
     } 




    } 


    return pathCoordinatesArray; 



} 

-(BOOL)checkDistanceForSource:(CLLocation*)source andDestination:(CLLocation*)destination { 

    CLLocationDistance distance = [source distanceFromLocation:destination]; 

    if (distance > 1000) { 

     CLLocation *location = [[CLLocation alloc] initWithLatitude:destination.coordinate.latitude longitude:destination.coordinate.longitude]; 

     [pathCoordinatesArray addObject:location]; 
     return YES; 
    } 
    return NO; 

} 

假設如果我有5000米的距離路徑,那麼我必須得到5個座標,每個座標在1000米位置順序。

我認爲這是錯誤的代碼。用優化代碼建議我

查看圖像每個點都有1000個測量距離。

+0

你的循環代碼,你可以澄清你的問題? –

+0

@jogendra:bentween點A和點B我需要得到每1000米的座標 – MuraliMohan

+0

@jogendra:我更新了我的問題與一些代碼,請檢查它。我認爲這是錯誤的代碼。建議我使用優化代碼 – MuraliMohan

回答

0

請更改這樣

for (int i=0 ; i<path.count; i++) { 

    if (pathCoordinatesArray.count == 0) { 
     CLLocationCoordinate2D temp2d = [path coordinateAtIndex:i]; 
     CLLocation *tempLoc = [[CLLocation alloc]initWithLatitude:temp2d.latitude longitude:temp2d.longitude]; 

     [pathCoordinatesArray addObject:tempLoc]; 
    } 
    CLLocationCoordinate2D destinationCoordinate = [path coordinateAtIndex:i]; 
    CLLocation *destinationLocation = [[CLLocation alloc]initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude]; 

    [self checkDistanceForSource:[pathCoordinatesArray lastObject] andDestination:destinationLocation]; 
    } 
} 
相關問題