Daniel
Daniel

Reputation: 572

Android - get GPS coordinates and location

I try to develop an application to track the GPS data from User. If I have the longitude and latitude i want to convert these in a location. (That is working with static data as you can see in my code)

My problem is to get the variables from longitude and latitude into the JSON-request - geocode/json?latlng=42.774955,18.955061",

At the end there should stay "...geocode/json?latlng"+lat+","+lng+...

Every time I try this I get NULL values. Please help me.

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) {

    }
}

Upvotes: 1

Views: 11227

Answers (1)

AkashBhave
AkashBhave

Reputation: 749

You are getting null values for your Location object because you are initializing an object every time the Button is clicked, and getting the values of that object. When a Location object is created, it has no properties, unless you set them using location.setLatitude() or location.setLongitude(). To solve your problem, just remove the lines:

Location location = new Location("");
lng = location.getLongitude();
lat = location.getLatitude();

When you get the coordinates to log them, you will not get a null value since you are setting them in your onLocationChanged() listener. You should also add an initial check in onCreate() to check for the location.
After doing this, you should be able to write "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng" for the String you are passing to you JSONObjectRequest.

Upvotes: 3

Related Questions