Android - 获取GPS坐标和位置

时间:2016-05-22 11:36:43

标签: java android json gps

我尝试开发一个应用程序来跟踪用户的GPS数据。如果我有经度和纬度,我想在一个位置转换这些。 (正如我在代码中看到的那样使用静态数据)

我的问题是将经度和纬度的变量变为JSON请求 - 地理编码/ json?latlng = 42.774955,18.955061",

最后应该留下" ... geocode / json?latlng" + lat +"," + 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) {

    }
}

1 个答案:

答案 0 :(得分:3)

您的null对象获得Location值,因为您每次点击Button 初始化对象并获取其值宾语。创建Location对象时,它没有属性,除非您使用location.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" String写一封JSONObjectRequest来传递给您make