排球请求时,Android MapView getlongitude和getlatitude返回0.0

时间:2016-10-23 15:58:04

标签: android gps fragment android-volley android-mapview

您好我有一个关于位置获取经度和获得纬度的问题。 我的数据库总是0.0。

它使用volley传递lngs和lats变量但是当它到达时值为0.0

变量lats和lngs应该在onLocationChanged()函数中更新

以下是我的一些代码

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);

1 个答案:

答案 0 :(得分:1)

我认为你早些时候将服务器和服务器交给了服务器。而是在从locationManager侦听器调用onLocationChanged方法后立即发送位置。

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);
    }