I want to upload the longitude and latitude from my locationlistener every 5 seconds to my database. I dont know why but i cant connect to it or something else goes wrong. I dont got any errors. I put the latitude and longitude double to a String.
If you can help me guys this would be so great!
RequestQueue requestQueue; String insertUrl = "http://10.0.2.2/insertlocation.php";
...
And i put this in the oncreate method: requestQueue = Volley.newRequestQueue(getApplicationContext());
...
@Override
public void onLocationChanged(Location location) {
if (location == null)
return;
mPositionMarker = mMap.addMarker(new MarkerOptions()
.flat(true)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.motorcycle))
.anchor(0.5f, 1f)
.position(new LatLng(location.getLatitude(), location.getLongitude())));
final double latitude = location.getLatitude();
final double longitude = location.getLongitude();
String Text = latitude + " " + longitude;
//Toast.makeText(this, Text, Toast.LENGTH_SHORT).show();
Log.d("location changed lat", location.getLatitude()+" long: "+location.getLongitude());
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(Karte.this, "Connected", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Karte.this, "Not Connected", Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> parameters = new HashMap<String, String>();
String lat2string = String.valueOf(latitude);
String long2string = String.valueOf(longitude);
parameters.put("longitude", long2string);
parameters.put("latitude", lat2string);
return parameters;
}
};
requestQueue.add(request);
}
insertlocation.php
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
require 'connection.php';
createLocation();
}
function createlocation()
{
global $connect;
$latitude = $_POST["latitude"];
$longitude = $_POST["longitude"];
$query = " Insert into user_info(latitude,longitude) values ('$latitude','$longitude');";
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
}
?>