Snoopy Snoopy - 1 month ago 12
Android Question

Android MapView getlongitude and getlatitude returns 0.0 on volley request

Hello I have a problem about the Location get longitude and get latitude.
It is always 0.0 on my database.

It uses volley to pass the the lngs and lats variable but when it arrive the value is 0.0

The variables lats and lngs should have been updated in the onLocationChanged() function

Here is some of my code

Private double lats;
Private double lngs;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// Inflate the layout for this fragment
View myinflate = inflater.inflate(R.layout.fragment_home, container, false);
final TextView usr = (TextView)myinflate.findViewById(R.id.user);



Bundle extras = getActivity().getIntent().getExtras();
usrnme = extras.getString("user");


requestQueue = Volley.newRequestQueue(getActivity());


locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);

listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
lats=location.getLatitude();
lngs=location.getLongitude();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
};


if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

ActivityCompat.requestPermissions(getActivity(),PERMISSIONS_LOCATION,REQUEST_LOCATION);

}

}
else
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
}





StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {

}

}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {


}
}) {

@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("usr", usrnme);
parameters.put("lat", Double.toString(lats));
parameters.put("lng", Double.toString(lngs));

return parameters;
}
};
requestQueue.add(request);

Answer

I think you're handing over the lats and longs to server a bit earlier. Instead, send the location right after you have the onLocationChanged method called from locationManager listener.

Private double lats;
Private double lngs;

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View myinflate = inflater.inflate(R.layout.fragment_home, container, false);
    final TextView usr = (TextView)myinflate.findViewById(R.id.user);



    Bundle extras = getActivity().getIntent().getExtras();
    usrnme = extras.getString("user");


    requestQueue = Volley.newRequestQueue(getActivity());


    locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);

    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            lats=location.getLatitude();
            lngs=location.getLongitude();

            StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {


                }
            }) {

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> parameters = new HashMap<String, String>();
                    parameters.put("usr", usrnme);
                    parameters.put("lat", Double.toString(lats));
                    parameters.put("lng", Double.toString(lngs));

                    return parameters;
                }
            };
            requestQueue.add(request);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };


    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            ActivityCompat.requestPermissions(getActivity(),PERMISSIONS_LOCATION,REQUEST_LOCATION);

        }

    }
    else
    {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
    }
Comments