2011-08-24 30 views
0

當用戶給出起始地址和目的地址時,我已經使用Google方向AP計算緯度和經度。在這個api中,我也獲取了中點數的緯度和經度。我全部存儲在數組中。現在我想添加新數組中每個點的經度和緯度值。如何將它們的值存儲在數組中?當從XML獲取數據解析然後我使用此代碼如何在數組中添加數字的經緯度值?

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 
if (qName) 
{ 
    elementName = qName; 
} 

else if ([elementName isEqualToString:@"start_location"]) 
{ 
    isFoundStartLoc = NO; 
}   
else if ([elementName isEqualToString:@"end_location"]) 
{ 
    isFoundEndLoc = NO; 
}    
else if ([elementName isEqualToString:@"lat"]) 
{ 
    if (isFoundStep) 
    { 
     if (isFoundStartLoc) 
     { 
      stepObject.startLocLat = [self.currentElement doubleValue]; 
     } 
     else if(isFoundEndLoc) 
     { 
      stepObject.endLocLat = [self.currentElement doubleValue]; 
     } 
    } 
    else{ 
     if (isFoundStartLoc) 
     { 
      slegObject.startLocLat = [self.currentElement doubleValue]; 
     } 
     else if(isFoundEndLoc) 
     { 
      slegObject.endLocLat = [self.currentElement doubleValue]; 
     } 
    } 
}   
else if ([elementName isEqualToString:@"lng"]) 
{ 
    if (isFoundStep) 
    { 
     if (isFoundStartLoc) 
     { 
      stepObject.startLocLong = [self.currentElement doubleValue]; 
     } 
     else if(isFoundEndLoc) 
     { 
      stepObject.endLocLong = [self.currentElement doubleValue]; 
     } 
    } 
    else{ 
     if (isFoundStartLoc) 
    { 
     slegObject.startLocLong = [self.currentElement doubleValue]; 
    } 
    else if(isFoundEndLoc) 
    { 
     slegObject.endLocLong = [self.currentElement doubleValue]; 
    } 
     } 
}    
else if ([elementName isEqualToString:@"distance"]) 
{ 
    isFoundDistance = NO; 
} 
else if ([elementName isEqualToString:@"text"]) 
{ 
    if (isFoundStep) 
    { 
     if (isFoundDistance){ 
       stepObject.distance=[self.currentElement doubleValue]; 
      } 
    } 
    else 
    { 
     if(isFoundDistance) 
     { 
      slegObject.total_distance=[self.currentElement doubleValue]; 
     } 
    } 
} 
else if ([elementName isEqualToString:@"step"]) 
{ 
    if (ListofSteps1==nil) 
    { 
     ListofSteps1=[[NSMutableArray alloc]init]; 
    } 
    [ListofSteps1 addObject:stepObject]; 
    [stepObject release]; 
    stepObject=nil; 
    isFoundStep=NO; 
} 

else if ([elementName isEqualToString:@"leg"]) 
{ 
    if (listofPoint==nil) 
    { 
     listofPoint=[[NSMutableArray alloc]init]; 
    } 
    [listofPoint addObject:slegObject]; 
    [slegObject release]; 
    slegObject=nil; 
} 

} 在此代碼我有兩個陣列上是listofPoint用於存儲僅緯度和源壞目的地點的經度。另一個是ListofSteps1,用於存儲源和目標中間點的緯度和經度。現在我想在一個數組中添加所有的經度和緯度。那麼如何在單個數組中存儲?

+0

請發佈一些您的相關代碼。 – jtbandes

+0

緯度和經度在浮動(或雙打)對不對? – Krishnabhadra

+0

@ Krishnabhadra雙打 – iRam11

回答

0

如果你想這兩點存儲到一個數組只能有以下幾種可能:

  1. 創建一個包含兩個點作爲一個元素的新類,並添加這樣一個類的實例(最有可能的最徹底的方法)

  2. 如果你知道你總是存儲兩個點的每個條目你可以添加這些和arrayIndex = n*2; [myArray objectAtIndex: n] and [myArray objectAtIndex: n+1]

訪問的第n個對

3你也可以創建一個包含1個點和屬性的新類,它是一種點。但是,然後你需要總是從頭開始遍歷列表(我不確定這是你想要的情況)

ps你釋放你的slegObject。我沒有看到你爲它做了一個alloc/init。

是你的問題,而不是內存管理問題?

相關問題