Saturday, 15 May 2010

Update coordinates every 5 secs by sending Post data to mysql - Android -



Update coordinates every 5 secs by sending Post data to mysql - Android -

i m totally newbie in android programming, , develop app send realtime location coordinates google map mysql every 5 seconds. problem dont know should set timer resend info each iteration. timer should placed within onlocationchanged method? or within httppost info method? thanks

@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // create locationrequest object mlocationrequest = locationrequest.create(); // utilize high accuracy mlocationrequest.setpriority(locationrequest.priority_high_accuracy); // set update interval 5 seconds mlocationrequest.setinterval(update_interval); // set fastest update interval 1 sec mlocationrequest.setfastestinterval(fastest_interval); if (mmap == null) { // seek obtain map supportmapfragment. mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)) .getmap(); // check if successful in obtaining map. if (mmap != null) { mmap.setmylocationenabled(true); } } mmap.setmylocationenabled(true); locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.network_provider, min_time, min_distance, this); } @override public void onlocationchanged(location location) { double latitude = location.getlatitude(); final double longitude = location.getlongitude(); latlng = new latlng(latitude, longitude); cameraupdate center= cameraupdatefactory.newlatlng(latlng); cameraupdate zoom=cameraupdatefactory.zoomto(16); mmap.movecamera(center); mmap.animatecamera(zoom); postdata(userid, latitude, longitude); } public void postdata( int a, double b, double c) { // create new httpclient , post header httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url_create_user); seek { // add together info list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(3); namevaluepairs.add(new basicnamevaluepair("userid", string.valueof(a))); namevaluepairs.add(new basicnamevaluepair("latitude", string.valueof(b))); namevaluepairs.add(new basicnamevaluepair("longitude", string.valueof(c))); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); } grab (clientprotocolexception e) { // todo auto-generated grab block } grab (ioexception e) { // todo auto-generated grab block }

php file linking app , mysql:

$userid = $_post['userid']; $latitude = $_post['latitude']; $longitude = $_post['longitude']; // include db connect class require_once __dir__ . '/db_connect.php'; // connecting db $db = new db_connect(); $con = $db->connect(); // mysql inserting new row , update existing row $result = mysqli_query($con,"insert `data` (`userid`, `latitude`, `longitude`) values ('".$userid."', '".$latitude."', '".$longitude."')"); $result = mysqli_query($con,"update info set latitude = '$latitude', longitude = '$longitude' userid = $userid")or die(mysql_error());

thank attending :))

android mysql gps http-post google-maps-api-2

No comments:

Post a Comment