在我的应用程序中,我有位置功能:
- (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];
}
...我还有另一个功能,我想传递lati和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];
}
}
我该怎么做?我只需要lati
方法中longi
和ParseXML_of_Googe_PlacesApi:
的值。其他一切都很好
答案 0 :(得分:3)
将您的声明更改为:
-(void)ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl withLat:(NSString*)lati andLong:(NSString*)longi {
然后更改您的主叫代码:
[self ParseXML_of_Google_PlacesAPI:googleUrl withLat:lati andLong:longi];
答案 1 :(得分:2)
您需要修改ParseXML_of_Google_PlacesAPI:(NSString *)_ googleUrl方法以获取其他参数:
即ParseXML_of_Google_PlacesAPI:(NSString *)_ googleUrl long:(float)经度纬度:(浮动)纬度
如果它们是浮动的,你可以使用float,如果它们是你的方法中的nsstrings,则可以用NSString替换浮点数。
然后,您可以使用nsstringformat重新格式化您的网址。
答案 2 :(得分:2)
将您的方法重写为
- (void)parseXMLOfGooglePlacesAPI:(NSString *)url latitude:(NSString *)latitude longitude:(NSString *)longitude;
然后将其称为
[self parseXMLOfGooglePlacesAPI:googleURL latitude:late longitude:longi];
答案 3 :(得分:2)
如果您不想重写方法,或者需要在多个方法中使用变量,请在此类代码中显示类的lati
和longi
属性。
在.h
文件中,您将定义:
@property(nonAtomic, retain) NSString *lati;
@property(nonAtomic, retain) NSString *longi;
..并在.m
文件中:
@synthesize lati, longi;
然后你会像以下一样使用它们:
- (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];
}
......和:
-(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];
}
}