gpt4 book ai didi

iphone - 如何找到当前用户的纬度/经度并将其传递给另一种方法

转载 作者:行者123 更新时间:2023-12-01 19:17:38 25 4
gpt4 key购买 nike

我可以从下面的locationManager方法中找到当前用户的纬度/经度。然后,我需要将这些变量传递给Google Places API方法(也如下所示)。我遇到的问题是,在 ParseXML_of_Google_PlacesAP 方法中,myLat和myLongt都具有空值。不过,这些值已在locationManager方法中正确输出。

谢谢你的帮助

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
myLat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
latLabel.text = myLat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
myLongt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
longLabel.text = myLongt;


NSLog(@"myLat is %@ myLongt is %@ from location mgr", myLat, myLongt);
}


-(void)ParseXML_of_Google_PlacesAPI
{

NSURL *googlePlacesURL=[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=bar,@%&radius=500&types=bar&sensor=false&key=myAPIKey",myLat,myLongt]];

NSLog(@"lat is %@ longt is %@", myLat, myLongt);

NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];

NSArray *arr = [xmlDocument.rootElement elementsForName:@"result"];

for(GDataXMLElement *e in arr )
{
[placesOutputArray addObject:e];
}
}

最佳答案

好吧,我只是将其存储在NSUserdefaults中。因此,在您的情况下,它看起来可能像这样:

[[NSUserDefaults standardUserDefaults] setValue:myLat forKey:@"currentLat"];
[[NSUserDefaults standardUserDefaults] setValue:myLongt forKey:@"currentLongt"];

然后,我从喜欢的每种方法中读回它:
NSString *currentLat = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentLat"];
NSString *currentLongt = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentLongt"];

关于iphone - 如何找到当前用户的纬度/经度并将其传递给另一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12157311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com