php - convert coordinates to json for upload to database -
i trying convert user's location json before can insert mysql database php, can help? new @ this, code below:
project detail: trying create code will:
- on first launch create user id
- collect location of device
- convert user id , longitude/latitude json
- print json in label "label1"
- then send json mysql database
right now, no coordinates or user id appearing on label, help?
this 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 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 json came in nserror *error; nsarray *jsonarray = [nsjsonserialization jsonobjectwithdata:_downloadeddata options:nsjsonreadingallowfragments error:&error]; cllocationcoordinate2d coordinate; // loop through json objects, create question objects , add them our questions array (int = 0; < 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 question 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 data nsdata* jsondata = [nsjsonserialization datawithjsonobject:info options:nsjsonwritingprettyprinted error:&error]; //print out data contents label1.text = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]; } } - (void)locationmanager:(cllocationmanager *)manager didchangeauthorizationstatus:(clauthorizationstatus)status { if (status == kclauthorizationstatusauthorizedwheninuse) { [self.mapview setshowsuserlocation:yes]; } }
Comments
Post a Comment