Java Android-获取GPS坐标和位置
我试图开发一个应用程序来跟踪来自用户的GPS数据。如果我有经度和纬度,我想在一个位置转换它们。(正如您在我的代码中所看到的那样,它正在处理静态数据) 我的问题是将经度和纬度的变量放入JSON请求-geocode/JSON?latlng=42.774955,18.955061“ 最后应该是“…地理编码/json?latlng”+lat+,“+lng+ 每次我尝试这个,我都会得到空值。请帮助我Java Android-获取GPS坐标和位置,java,android,json,gps,Java,Android,Json,Gps,我试图开发一个应用程序来跟踪来自用户的GPS数据。如果我有经度和纬度,我想在一个位置转换它们。(正如您在我的代码中所看到的那样,它正在处理静态数据) 我的问题是将经度和纬度的变量放入JSON请求-geocode/JSON?latlng=42.774955,18.955061“ 最后应该是“…地理编码/json?latlng”+lat+,“+lng+ 每次我尝试这个,我都会得到空值。请帮助我 public class MatchActivity extends AppCompatActivity
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) {
}
}
公共类MatchActivity扩展了AppCompatActivity{
私人按钮;
私有文本视图文本视图;
私有文本视图文本视图城市;
私人场所经理场所经理;
私有位置侦听器位置侦听器;
私有请求队列请求队列;
私人双lat;
私人双液化天然气;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match);
按钮=(按钮)findViewById(R.id.button\u位置);
textView=(textView)findViewById(R.id.textView\u坐标);
textViewCity=(TextView)findViewById(R.id.textCity);
requestQueue=Volley.newRequestQueue(this);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationListener=新建myLocationlistener();
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
返回;
}
locationManager.RequestLocationUpdate(locationManager.GPS_提供程序,1000,0,locationListener);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
位置=新位置(“”);
lng=location.getLongitude();
lat=位置。getLatitude();
Log.e(“纬度”,String.valueOf(lat));
Log.e(“经度”,String.valueOf(lng));
JsonObjectRequest=新的JsonObjectRequest(“https://maps.googleapis.com/maps/api/geocode/json?latlng=42.774955,18.955061“,新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
字符串地址=response.getJSONArray(“结果”).getJSONObject(0).getString(“格式化的_地址”);
textViewCity.setText(地址);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
添加(请求);
}
});
}
私有类myLocationlistener实现LocationListener{
@凌驾
已更改位置上的公共无效(位置){
如果(位置!=null){
lat=位置。getLatitude();
lng=location.getLongitude();
}
}
@凌驾
状态已更改的公共void(字符串s、int i、Bundle){
}
@凌驾
已提供已启用的公共void(字符串s){
}
@凌驾
公共无效onProviderDisabled(字符串s){
}
}
您正在为您的位置
对象获取null
值,因为您每次单击按钮
时都会初始化一个对象,并获取该对象的值。创建位置
对象时,该对象没有属性,除非您使用位置.setLatitude()
或location.setLongitude()
。要解决您的问题,只需删除以下行:
Location location = new Location("");
lng = location.getLongitude();
lat = location.getLatitude();
当您获取坐标以记录它们时,将不会获得null
值,因为您正在onLocationChanged()
侦听器中设置它们。您还应该添加一个初始签入onCreate()
以检查位置。完成此操作后,您应该能够编写
”https://maps.googleapis.com/maps/api/geocode/json?latlng=“+lat+”,“+lng”
对于要传递给您的字符串
JSONObjectRequest您正在创建一个新的位置
对象,并使用它的坐标,而不是使用onLocationChanged(…)中设置的lat
和lng
值
callback。好的,但是我已经创建了一个新的位置对象Location Location=new Location(“”;
@TitusYes,这就是问题所在,你应该使用位置更改时设置的值。但是如果我这样做的话,这是不起作用的lng=myLocationlistener(new myLocationlistener(lng))
如果位置更改,则将通过onLocationChanged(…)
方法设置lng
和lat
值您无需设置此值,只需检查它们是否已设置。非常感谢您,现在它正在工作!:)不知道我在onLocationChanged()方法中获得了值!太好了,很高兴我提供了帮助!