iPhone SDK,从 LocationManager 读取 GPS 坐标

发布于 2024-12-02 20:27:43 字数 2381 浏览 0 评论 0原文

我的主 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦 2024-12-09 20:27:43

也许我错过了一些东西,但只是将 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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文