Android - 获取GPS坐标和位置
我尝试开发一个应用程序来跟踪用户的GPS数据。如果我有经度和纬度我想要在一个位置转换这些。 (这是静态的数据时,你可以在我的代码中看到)Android - 获取GPS坐标和位置
我的问题是,从经度得到的变量和纬度为JSON请求 - 地理编码/ JSON经纬度= 42.774955,18.955061" ,
?底有应留 “......地理编码/ JSON?经纬度 ”+纬度+“,” + LNG + ...
每次我试试这个,我得到NULL值。请帮帮我。
public class MatchActivity extends AppCompatActivity {
private Button button;
private TextView textView;
private TextView textViewCity;
private LocationManager locationManager;
private LocationListener locationListener;
private RequestQueue requestQueue;
private double lat;
private double lng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match);
button = (Button) findViewById(R.id.button_location);
textView = (TextView) findViewById(R.id.textView_Coordinates);
textViewCity = (TextView) findViewById(R.id.textCity);
requestQueue = Volley.newRequestQueue(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new myLocationlistener();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Location location = new Location("");
lng = location.getLongitude();
lat = location.getLatitude();
Log.e("Latitude", String.valueOf(lat));
Log.e("Longitude", String.valueOf(lng));
JsonObjectRequest request = new JsonObjectRequest("https://maps.googleapis.com/maps/api/geocode/json?latlng=42.774955,18.955061", new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String address = response.getJSONArray("results").getJSONObject(0).getString("formatted_address");
textViewCity.setText(address);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
});
}
private class myLocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if(location != null){
lat = location.getLatitude();
lng = location.getLongitude();
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
您正在为Location
对象获取null
值beca使用你是每次点击时,初始化一个对象,并获取该对象的值。当创建Location
对象时,它没有属性,除非使用location.setLatitude()
或location.setLongitude()
进行设置。为了解决你的问题,只是删除行:
Location location = new Location("");
lng = location.getLongitude();
lat = location.getLatitude();
当你的坐标记录它们,你就不会因为你在你的onLocationChanged()
监听器设置他们得到null
值。您还应该在onCreate()
中添加初始支票以检查位置。
完成此操作后,您应该能够为您传递给的String
编写"https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng"
。
非常感谢你,现在它的工作! :)没有认识到我得到onLocationChanged()方法中的值! – Daniel
太棒了,很高兴我帮助了! – AkashBhave
您正在创建一个新的'Location'对象,并使用它的坐标而不是使用'onLocationChanged(...)'回调中设置的'lat'和'lng'值。 – Titus
好的,但我已经创建了一个新的Location对象'Location location = new Location(“”);'@Titus – Daniel
是的,那就是问题所在,您应该使用位置更改时设置的值。 – Titus