1

我可以从下面的 locationManager 方法中找到当前用户 Lat/Longt。然后我需要将这些变量传递给 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];
    } 
}
4

2 回答 2

2

由于 myLat 和 myLongt 是 NSString ,它显示值为 null 意味着它正在被释放。

所以在喂值后保留两个对象。

 myLat = nil;
 myLat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                     degrees, minutes, seconds];
 [myLat retain];

还,

 myLongt = nil;
 myLongt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                       degrees, minutes, seconds];
 [myLongt retain];
于 2012-08-28T10:41:59.993 回答
1

好吧,我只是将它存储在 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"];
于 2012-08-28T10:46:58.830 回答