Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public void getLocationFromAddress(String address) {
  2. String url = "https://maps.googleapis.com/maps/api/geocode/json?address="
  3. + Uri.encode(address) + "&sensor=true&key=API_KEY";
  4. RequestQueue queue = Volley.newRequestQueue(this);
  5. JsonObjectRequest stateReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
  6. @Override
  7. public void onResponse(JSONObject response) {
  8. JSONObject location;
  9. try {
  10. // Get JSON Array called "results" and then get the 0th
  11. // complete object as JSON
  12. location = response.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
  13. // Get the value of the attribute whose name is
  14. // "formatted_string"
  15. if (location.getDouble("lat") != 0 && location.getDouble("lng") != 0) {
  16. LatLng latLng = new LatLng(location.getDouble("lat"), location.getDouble("lng"));
  17.  
  18. //Do what you want
  19. }
  20. } catch (JSONException e1) {
  21. e1.printStackTrace();
  22.  
  23. }
  24. }
  25.  
  26. }, new Response.ErrorListener() {
  27. @Override
  28. public void onErrorResponse(VolleyError error) {
  29. Log.d("Error.Response", error.toString());
  30. }
  31. });
  32. // add it to the queue
  33. queue.add(stateReq);
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement