Android MapView getlongitude 和 getlatitude returns 0.0 on volley 请求
Android MapView getlongitude and getlatitude returns 0.0 on volley request
您好,我对位置获取经度和纬度有疑问。
它在我的数据库中始终为 0.0。
它使用 volley 传递 lngs 和 lats 变量,但当它到达时值为 0.0
应该在 onLocationChanged() 函数中更新变量 lats 和 lngs
这是我的一些代码
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);
我认为您将 lats 和 longs 移交给服务器有点早。相反,在从 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);
}
您好,我对位置获取经度和纬度有疑问。 它在我的数据库中始终为 0.0。
它使用 volley 传递 lngs 和 lats 变量,但当它到达时值为 0.0
应该在 onLocationChanged() 函数中更新变量 lats 和 lngs
这是我的一些代码
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);
我认为您将 lats 和 longs 移交给服务器有点早。相反,在从 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);
}