views:

435

answers:

2

Hi I am wondering if there is away to send the lat and long of a persons location to a URL? It would also need to have their UDID number to match with the database.

Here is what I have so far if anyone can help that would be great...

-(void)viewDidLoad {    
     NSString *query = [[NSString alloc] initWithFormat:
                          @"http://mysite.com/ihome.php?uid=%@", 
                          [[UIDevice currentDevice] uniqueIdentifier], 
                          @"&year=2010%@"];
     NSURL *url = [[NSURL alloc] initWithString:query];
     NSURLRequest *requestObj = [ NSURLRequest requestWithURL: url ];
     webView.opaque = NO;
     webView.backgroundColor = [UIColor clearColor];
     [webView loadRequest: requestObj ];
}
A: 

You have to calculate the longitude and the latitude before to request the URL.

- (void) viewDidLoad {
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    self.locationManager.delegate = self; // send location updates to this object
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
     NSLog(@"Your location: %@", [newLocation description]);

     NSString *query = [[NSString alloc] initWithFormat:
                          @"http://mysite.com/ihome.php?uid=%@&longitude=%d&latitude=%d", 
                          [[UIDevice currentDevice] uniqueIdentifier], 
                          @"&year=2010%@",
                          newLocation.longitude,
                          newLocation.latitude];
     NSURL *url = [[NSURL alloc] initWithString:query];
     NSURLRequest *requestObj = [ NSURLRequest requestWithURL: url ];
     webView.opaque = NO;
     webView.backgroundColor = [UIColor clearColor];
     [webView loadRequest: requestObj ];

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Error: %@", [error description]);
}
Yannick L.
I get errors saying /Users/russellharrower/Documents/iPhone/QH/WebViewController.m:18:0 /Users/russellharrower/Documents/iPhone/QH/WebViewController.m:18: error: request for member 'locationManager' in something not a structure or unionand fours other errorsIn my .h file I have this#import <UIKit/UIKit.h>#import <CoreLocation/CoreLocation.h>@interface WebViewController : UIViewController { IBOutlet UIWebView *webView;}@property (nonatomic, retain) UIWebView *webView;@endHave I missed something? My m file has the above code you added.
Now I only have two errors - error: request for member 'longitude' in something not a structure or unionerror: request for member 'latitude' in something not a structure or union
Ah sorry, I have made a mistake. Before the NSString *query, use it:CLLocation *location = [locationManager location]; if (!location) { return; } CLLocationCoordinate2D coordinate = [location coordinate];And then use coordonate.longitude and coordonate.latitude instead of the newLocation.longitude or latitude.
Yannick L.
A: 

This is what I have so far

my .h file

    #import < UIKit/UIKit.h>
#import < CoreLocation/CoreLocation.h>

@interface WebViewController : UIViewController {
    IBOutlet UIWebView *webView;
    IBOutlet CLLocationManager *locationManager;
    IBOutlet NSString *latitude;
    IBOutlet NSString *longitude;
}

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) NSString *longitude;
@property (nonatomic, retain) NSString *latitude;


@end

my .m file

#import "WebViewController.h"
@implementation WebViewController

@synthesize webView;
@synthesize locationManager;
@synthesize longitude;
@synthesize latitude;

- (void) viewDidLoad {
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    self.locationManager.delegate = self; // send location updates to this object
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"Your location: %@", [newLocation description]);

    NSString *query = [[NSString alloc] initWithFormat:
                       @"http://mysite.com/ihome.php?uid=%@&amp;longitude=%d&amp;latitude=%d", 
                       [[UIDevice currentDevice] uniqueIdentifier], 
                       @"&year=2010%@",
                       newLocation.longitude,
                       newLocation.latitude];
    NSURL *url = [[NSURL alloc] initWithString:query];
    NSURLRequest *requestObj = [ NSURLRequest requestWithURL: url ];
    webView.opaque = NO;
    webView.backgroundColor = [UIColor clearColor];
    [webView loadRequest: requestObj ];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; 
}

- (void)dealloc {
    [ webView release ];
    [ latitude release ];
    [ longitude release ];
    [ locationManager release];
    [ super dealloc ];
}


@end

Do I need to add anything to IB?