iosarraysnsarraynsmutablearraylatitude-longitude

How add value of latitude and longitude of nuber on points in array?


I have use Google direction API for calculating latitude and longitude when user given origin and destination address. In this API I also get latitude and longitude of number of mid points. I have all stored in array. Now I want to add value of latitude and longitude of each point in new array. How store value of them in an array? When get data from XML parsing then I use this code:

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

In this code I have two array on is listofPoint for storage only latitude and longitude of source bad destination points, and another is ListofSteps1 for storage of latitude and longitude of middle points of source and destination. Now I want to add all latitude and longitude in a single array. So how store in single array?


Solution

  • If you want to store both points into one array only there are several possibilities:

    1. create a new class which contains the two points as one element and add an instance of such a class (most probable the cleanest way)

    2. If you know that you always store two points for each entry you can just add these and access the the nTh pair by arrayIndex = n*2; [myArray objectAtIndex: n] and [myArray objectAtIndex: n+1]

    3 you may also create a new class containing 1 point and an attribute which kind of point it is. But then you need to traverse the list always from the beginning (I am not sure this is what you want in your case)

    ps you release your slegObject. I don't see that you do an alloc/init for it.

    is your problem rather a memory management issue?