iPhone SDK,从 LocationManager 读取 GPS 坐标
我的主 .m 文件中有以下代码(我想在 http 查询字符串中发送 GPS 坐标)
- (CLLocationManager *)locationManager {
if (locationManager != nil) {
return locationManager;
}
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
return locationManager;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(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];
}
并且在我的 .h 中:(
@interface RootViewController : UITableViewController {
CLLocationManager *locationManager;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
我已经删除了 .h 中不必要的行)
如何在不同的代码块中使用上面的当前 GPS 经度/纬度:(替换 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;
}
}
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 didUpdateToLocation:(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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许我错过了一些东西,但只是将 newLocation.cooperative.latitude 存储在您在标头中定义的某个浮点值中。
如果您在应用程序中需要它,您可以拥有一个单调类,或者将其存储到 nsuserdefaults 中。
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..