0

In my application I have location function:

- (void)locationUpdate:(CLLocation *)location {    
  lati = [[NSString alloc]initWithFormat:@"%f", location.coordinate.latitude] ;
  longi = [[NSString alloc ]initWithFormat:@"%f", location.coordinate.longitude ];    
  [self ParseXML_of_Google_PlacesAPI:googleUrl];
}

...and I have another function to which I want to pass lati and longi:

-(void)ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl { 
  //use lati and longi in this line 
  googleUrl=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&name=asda&sensor=false&key=mykey",lati,longi];
  NSURL *googlePlacesURL = [NSURL  URLWithString:googleUrl ];
  NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
  xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
  NSArray *arr = [xmlDocument.rootElement elementsForName:@"result"];
  placesOutputArray=[[NSMutableArray alloc]init];
  for(GDataXMLElement *e in arr ){        
          [placesOutputArray addObject:e];
  }
}

How can I do that? I just need the values of lati and longi in the ParseXML_of_Googe_PlacesApi: method. Everything else is working fine

1
  • You pass the argument _googleUrl for ParseXML_of_Google_PlacesAPI: but you don't use it in the method. Commented Aug 16, 2011 at 22:36

4 Answers 4

3

Change your declaration to:

-(void)ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl withLat:(NSString*)lati andLong:(NSString*)longi {

Then change your calling code:

[self ParseXML_of_Google_PlacesAPI:googleUrl withLat:lati andLong:longi];
Sign up to request clarification or add additional context in comments.

Comments

2

You need to modify the ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl method to take other arguments:

i.e )ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl long:(float) longitude lat:(float) latitude

You can use float if they floats or replace the float with NSString if they are nsstrings like in your method.

You can then use the nsstringformat to reformat your URL.

Comments

2

Rewrite your method as

- (void)parseXMLOfGooglePlacesAPI:(NSString *)url latitude:(NSString *)latitude longitude:(NSString *)longitude;

and then call it as

[self parseXMLOfGooglePlacesAPI:googleURL latitude:late longitude:longi];

Comments

2

If you don't want to rewrite the method or you need to use the variables in more than one method, make lati and longi properties of the class this code appears in.

In the .h file you would define:

@property(nonAtomic, retain) NSString *lati;
@property(nonAtomic, retain) NSString *longi;

.. and in the .m file you would:

@synthesize lati, longi;

Then you would use them like:

- (void)locationUpdate:(CLLocation *)location {
  self.lati = [[NSString alloc]initWithFormat:@"%f", location.coordinate.latitude] ;
  self.longi = [[NSString alloc ]initWithFormat:@"%f", location.coordinate.longitude ];    
  [self ParseXML_of_Google_PlacesAPI:googleUrl];
}

... and:

-(void)ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl {  
  googleUrl=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&name=asda&sensor=false&key=mykey",self.lati,self.longi];
  NSURL *googlePlacesURL = [NSURL  URLWithString:googleUrl ];
  NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
  xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
  NSArray *arr = [xmlDocument.rootElement elementsForName:@"result"];
  placesOutputArray=[[NSMutableArray alloc]init];
  for(GDataXMLElement *e in arr ){        
          [placesOutputArray addObject:e];
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.