【发布时间】:2016-10-23 15:58:04
【问题描述】:
您好,我对位置获取经度和纬度有疑问。 在我的数据库中它总是 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);
【问题讨论】:
标签: android gps fragment android-volley android-mapview