如何在数组中添加nuber的经纬度值?

问题描述 投票:0回答:1

我使用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,用于存储源点和目的点中间点的经纬度。另一个是ListofSteps1,用于存储源点和目的地中间点的经纬度。现在我想把所有的经纬度添加到一个数组中。那么如何在一个数组中存储呢?

iphone api memory nsmutablearray nsarray
1个回答
0
投票

如果你想把两个点都存储到一个数组中,有几种可能。

  1. 创建一个新的类,其中包含两个点作为一个元素,并添加这样一个类的实例(最可能是最干净的方式)。

  2. 如果你知道你总是为每个条目存储两个点,你只需添加这些点,并通过以下方式访问第n对 arrayIndex = n*2; [myArray objectAtIndex: n] and [myArray objectAtIndex: n+1]

3 你也可以创建一个新的类,包含1个点和一个属性,即它是哪种点。但是你需要从头开始遍历列表(我不确定这是不是你想要的)。

ps你释放你的slegObject。我没有看到你为它做一个 allocinit.

你的问题是内存管理的问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.