Saving Latitude and Longitude values to a MySQL Database iOS [closed]

巧了我就是萌 提交于 2019-12-25 18:24:53

问题


I've set my code up so that the user inputs all of the location details (address, city, county, country and postcode) and then on the click of a button the lat and lon values are fed into two text boxes... My problem now is that the code that was previously working to save the data to the database is not working.

Here's my code...

php

<?php

$username = "username";
$database = "database";
$password = "password";

mysql_connect(localhost, $username, $password);

@mysql_select_db($database) or die("unable to find database");

$name = $_GET["name"];
$address = $_GET["address"];
$city = $_GET["city"];
$county = $_GET["county"];
$country = $_GET["country"];
$postcode = $_GET["postcode"];
$date = $_GET["date"];
$time = $_GET["time"];
$latitude = $_GET["latitude"];
$longitude = $_GET["longitude"];

$query = "INSERT INTO map VALUES ('', '$name', '$address', '$city', '$county', '$country', '$postcode', '$date', '$time, '$latitude', '$longitude'')";

mysql_query($query) or die(mysql_error("error"));
mysql_close();

?>

AddSessionViewController.h

#import <UIKit/UIKit.h>

#define kpostURL @"http://myurl.com/map.php"
#define kname @"name"
#define kaddress @"address"
#define kcity @"city"
#define kcounty @"county"
#define kcountry @"country"
#define kpostcode @"postcode"
#define kdate @"date"
#define ktime @"time"
#define klatitude @"latitude"
#define klongitude @"longitude"

@interface addSessionViewController : UIViewController {

UIScrollView *scrollView_;
BOOL keyboardVisible_;

IBOutlet UITextField *nameText;
IBOutlet UITextField *addressText;
IBOutlet UITextField *cityText;
IBOutlet UITextField *countyText;
IBOutlet UITextField *countryText;
IBOutlet UITextField *postcodeText;
IBOutlet UITextField *dateText;
IBOutlet UITextField *timeText;
IBOutlet UITextField *latitudeText;
IBOutlet UITextField *longitudeText;
NSURLConnection *postConnection;

}

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;

-(IBAction)textFieldDoneEditing:(id) sender;
-(IBAction)post:(id)sender;
-(IBAction)getLatLon:(id)sender;


-(void) postMessage:(NSString*) postcode withName:(NSString *) name withAddress:(NSString *) address withCity:(NSString *) city withCounty:(NSString *) county withCountry:(NSString *) country  withDate:(NSString *) date withTime:(NSString *) time withLatitude:(NSString *) latitude withLongitude:(NSString *) longitude;

- (void) keyboardDidShow:(NSNotification *)notif;
- (void) keyboardDidHide:(NSNotification *)notif;

@end

AddSessionViewController.m

#import "addSessionViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "SVGeocoder.h"

@implementation addSessionViewController

@synthesize scrollView;

- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

self.scrollView.contentSize = self.view.frame.size;

UIColor *backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg-splash.png"]];
[self.view setBackgroundColor:backgroundColor];

UIView* gradientView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 4)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = gradientView.bounds;

UIColor* lightColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
UIColor* darkColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

gradient.colors = [NSArray arrayWithObjects:(id)darkColor.CGColor, (id)lightColor.CGColor, nil];
[gradientView.layer insertSublayer:gradient atIndex:0];

[self.view addSubview:gradientView];

}

-(void) postMessage:(NSString*) postcode withName:(NSString *) name withAddress:(NSString *) address withCity:(NSString *) city withCounty:(NSString *) county withCountry:(NSString *) country withDate:(NSString *) date withTime:(NSString *) time withLatitude:(NSString *)latitude withLongitude:(NSString *)longitude; {

if (name != nil && address != nil && city != nil && county != nil && country != nil && postcode !=nil && date != nil && time != nil && latitude != nil && longitude != nil){

    NSMutableString *postString = [NSMutableString stringWithString:kpostURL];

    [postString appendString:[NSString stringWithFormat:@"?%@=%@", kname, name]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kaddress, address]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcity, city]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcounty, county]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcountry, country]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kpostcode, postcode]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kdate, date]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", ktime, time]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", klatitude, latitude]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", klongitude, longitude]];

    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
    [request setHTTPMethod:@"POST"];

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

}

}


-(IBAction)post:(id)sender{

[self postMessage:(NSString*) postcodeText.text withName:nameText.text withAddress:addressText.text withCity:cityText.text withCounty:countyText.text withCountry:countryText.text withDate:dateText.text withTime:timeText.text withLatitude:latitudeText.text withLongitude:longitudeText.text];

[postcodeText resignFirstResponder];
nameText.text = nil;
addressText.text = nil;
cityText.text = nil;
countyText.text = nil;
countryText.text = nil;
postcodeText.text = nil;
dateText.text = nil;
timeText.text = nil;
latitudeText.text = nil;
longitudeText.text = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test2" object:self];
}


-(IBAction)getLatLon:(id)sender{


NSString *eventName = [nameText text];
NSString *eventAddress = [addressText text];
NSString *eventCity = [cityText text];
NSString *eventCountry = [countryText text];
NSString *eventCounty = [countyText text];
NSString *eventDate = [dateText text];
NSString *eventPostCode = [postcodeText text];
NSString *eventTime = [timeText text];

// Use this to build the full event string
NSString *addressString = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@,%@", eventName, eventAddress, eventCity, eventCounty, eventCountry, eventPostCode, eventDate, eventTime];

// A search can't inlude the event and time and hour as it means nothing to MapKit
// The geocodeAddressString simply contains the address info
NSString *geocodeAddressString = [NSString stringWithFormat:@"%@,%@,%@,%@,%@", eventAddress, eventCity, eventCounty, eventCountry, eventPostCode];

//NSLog(@"Hello %@",addressString);
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

[geocoder geocodeAddressString:geocodeAddressString completionHandler:^(NSArray* placemarks, NSError* error){
    // Check for returned placemarks
    if (placemarks && placemarks.count > 0) {
        CLPlacemark *topResult = [placemarks objectAtIndex:0];

        //NSLog([topResult locality]);
        //NSLog([topResult location]);
        //MKCoordinateRegion region;
        //double *value = topResult.region.center.latitude;

        CLLocationDegrees latitude = topResult.region.center.latitude;
        CLLocationDegrees longitude = topResult.location.coordinate.longitude;

        NSString *latitudeString = [NSString stringWithFormat:@"%f",latitude];
        NSString *longitudeString = [NSString stringWithFormat:@"%f",longitude];

        latitudeText.text = latitudeString;
        longitudeText.text = longitudeString;

        NSLog(@"Lat: %f", latitude);
        NSLog(@"Long: %f", longitude);
    }
}];
}

- (void)viewDidUnload
{

// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{

NSLog(@"%@", @"Registering for keyboard events...");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
keyboardVisible_ = NO;


[super viewWillAppear:animated];

}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{

NSLog(@"%@", @"Unregistering for keyboard events...");
[[NSNotificationCenter defaultCenter] removeObserver:self];

[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark -
#pragma mark Keyboard handlers

- (void) keyboardDidShow:(NSNotification *)notif {
NSLog(@"%@", @"Received UIKeyboardDidShowNotification");

if (keyboardVisible_) {
    NSLog(@"%@", @"Keyboard is already visible.  Ignoring notifications.");
    return;
}

// The keyboard wasn't visible before
NSLog(@"Resizing smaller for keyboard");

// Get the origin of the keyboard when it finishes animating
NSDictionary *info = [notif userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

// Get the top of the keyboard in view's coordinate system. 
// We need to set the bottom of the scrollview to line up with it
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;

// Resize the scroll view to make room for the keyboard
CGRect viewFrame = self.view.bounds;
viewFrame.size.height = keyboardTop - self.view.bounds.origin.y;

self.scrollView.frame = viewFrame;
keyboardVisible_ = YES;
}

- (void) keyboardDidHide:(NSNotification *)notif {
NSLog(@"%@", @"Received UIKeyboardDidHideNotification");

if (!keyboardVisible_) {
    NSLog(@"%@", @"Keyboard already hidden.  Ignoring notification.");
    return;
}

// The keyboard was visible
NSLog(@"%@", @"Resizing bigger with no keyboard");

// Resize the scroll view back to the full size of our view
self.scrollView.frame = self.view.bounds;
keyboardVisible_ = NO;
}

@end

I know the code isn't the best as I'm new to Objective C and iOS development, but I'm really panicking as this is my final university assignment and it needs to be handed in at 10am tomorrow so ANY help would be appreciated!


回答1:


You have a superfluous ' in your INSERT statement after $longitude.



来源:https://stackoverflow.com/questions/10693276/saving-latitude-and-longitude-values-to-a-mysql-database-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!