将坐标转换为json以上传到数据库

时间:2014-11-13 07:39:54

标签: php ios mysql json core-location

我试图将用户的位置转换为json,然后才能将其插入到带有php的mysql数据库中,任何人都可以帮忙吗?我是新手,我的代码如下:

项目细节:我正在尝试创建以下代码:

    首次启动时
  • 创建用户ID
  • 收集设备的位置
  • 将用户ID和经度/纬度转换为Json
  • 在标签“Label1”
  • 中打印Json
  • 然后将json发送到mysql数据库

现在,我的标签上没有出现坐标或用户ID,有人可以帮忙吗?

这是我的ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
    CLLocationManager *locationManager;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;

locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

[locationManager startUpdatingLocation];
NSLog(@" lat: %f",locationManager.location.coordinate.latitude);
NSLog(@" lon: %f",locationManager.location.coordinate.longitude);

//   Request use on iOS 8
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
    [self.mapView setShowsUserLocation:YES];
} else {
    [locationManager requestWhenInUseAuthorization];
}


if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
    //App has previously launched
}
else
{
    //First launch
    NSString *identifierString = [[NSUUID UUID] UUIDString];
    [[NSUserDefaults standardUserDefaults] setObject:identifierString forKey:@"uuidKey"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

NSURL *jsonFileUrl = [NSURL URLWithString:@"http://random.name/service.php"];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
[NSURLConnection connectionWithRequest:urlRequest delegate:self];

}

#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
_downloadedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_downloadedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSMutableArray *_locations = [[NSMutableArray alloc] init];

// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];

CLLocationCoordinate2D coordinate;
// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++)
{
    NSDictionary *jsonElement = jsonArray[i];
    MKPointAnnotation* marker = [[MKPointAnnotation alloc] init];

    marker.title = jsonElement[@"Name"];
    marker.subtitle = jsonElement[@"Address"];
    coordinate.latitude = [jsonElement [@"Latitude"] doubleValue];
    coordinate.longitude = [jsonElement [@"Longitude"] doubleValue];

    marker.coordinate = coordinate;
    // Add this question to the locations array
    [_locations addObject:marker];

    NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
                          [jsonElement objectForKey:@"Name"], @"uuidKey",
                          [jsonElement objectForKey:@"Latitude"], @" lat: %f",
                          [jsonElement objectForKey:@"Longitude"], @"lon: %f",
                          nil];

    //convert object to data
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    //print out the data contents
    Label1.text = [[NSString alloc] initWithData:jsonData
                                             encoding:NSUTF8StringEncoding];

    }
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
    [self.mapView setShowsUserLocation:YES];
}
}

0 个答案:

没有答案