I've got the following code in my main .m file (I want to send the gps co-ords in a http query string)
- (CLLocationManager *)locationManager {
if (locationManager != nil) {
return locationManager;
}
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
return locationManager;
}
- (void)locationManager:(CLLocationManager *)manager didUpda开发者_StackOverflowteToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *) oldLocation {
NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.latitude];
NSLog(@"lat is %@", latitudeString);
//self.myLatitude = latitudeString;
[latitudeString release];
NSString *longitudeString = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.longitude];
NSLog(@"long is %@", longitudeString);
//self.myLongitude = longitudeString;
[longitudeString release];
}
And in my .h I have:
@interface RootViewController : UITableViewController {
CLLocationManager *locationManager;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
(i've stripped out the unnecessary lines in the .h)
How can I use the current GPS long/lat from above in a different block of code: (substituting the LONGVAR/LATVAR)
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *tempvariable;
NSString *gpsLat;
switch (indexPath.row) {
case 0:
tempvariable = @"http://randomurl.randomurl.com/mobilesearch/what/Hotels%20and%20Inns/0/0/0/UK/**LONGVAR/LATVAR**/0/0/342/0/0/0/0/0/search.aspx";
break;
case 1:
tempvariable = (@"http://randomurl.randomurl.com/mobilesearch/what/Public%20Houses/0/0/0/UK/**LONGVAR/LATVAR**/0/0/505/0/0/0/0/0/search.aspx");
break;
case 2:
tempvariable = (@"http://randomurl.randomurl.com/mobilesearch/what/All%20Restaurants/0/0/0/UK/**LONGVAR/LATVAR**/0/0/527/0/0/0/0/0/search.aspx");
break;
case 3:
tempvariable = (@"http://randomurl.randomurl.com/mobilesearch/what/Health Care/0/0/0/UK/**LONGVAR/LATVAR**/0/0/843/0/0/0/0/0/search.aspx");
break;
default:
tempvariable = (@"http://randomurl.randomurl.com/mobilesearch/what/Hotels and Inns/0/0/0/UK/**LONGVAR/LATVAR/**0/0/342/0/0/0/0/0/search.aspx");
break;
}
}
maybe i'm missing something but just store newLocation.coordinate.latitude in some float value that you define in header..
you can have a singletone class if you need it across the application, or store it to nsuserdefaults..
精彩评论