mianlaoshu
mianlaoshu

Reputation: 2592

What's the best way to do reverse geocoding in Android

I want to do reverse geocoding in Android, and sometimes find the Geocoder is not quite reliable. So I researched to use the google Geocoding API.

I found there are two kinds such API: the client-side geocoding (JavaScript calls to google.maps.Geocoder) and server-side geocoding (HTTP requests to /maps/api/geocode).

Seems client-side geocoding is usually the most appropriate approach as it executes in the browser, so I think it's not suitable for Android. Am I right?

If using the server-side geocoding approach, Android would send the http requests to maps/api/geocode. Then I need to store the api key to the remote server and request it each time when app starts. Is is the best way to do so? Has any one did it this way?

===================================

Another question: should I use the geocoding result combined with google map? Can I just display the result to the end user without showing the map? What rules should I follow?

Upvotes: 1

Views: 1631

Answers (2)

AndrewR
AndrewR

Reputation: 10889

The JavaScript geocoding libraries are not entirely "client-side", they still need to make an RPC to Google to get the geocode results.

You can make HTTP calls to https://maps.googleapis.com/maps/api/geocode from within your Android app easily enough using Volley and then parse the JSON response.

Something like (incomplete example):

Listener<JSONObject> listener = new Listener<JSONObject>() {      
  @Override 
  public void onResponse(JSONObject response) { 
   try { 
    JSONArray jsonArray = response.getJSONArray("results"); 
    if (jsonArray.length() > 0) { 
     JSONObject jsonObject = jsonArray.getJSONObject(0); 
     // Do something...
    } 
   } catch (JSONException e) { 
    e.printStackTrace(); 
   } 
  } 
}; 

String url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&latlng=" 
    + lat + "," + lng + "&key=" + API_KEY; 
JsonObjectRequest request = new JsonObjectRequest(url, null, listener, errorListener); 
mRequestQueue.add(request); 

Upvotes: 0

Bui Minh Duc
Bui Minh Duc

Reputation: 132

Google has provided the Geocoder class for handling geocoding and reverse geocoding for android device. You can take a look in this Geocoder class in this article https://developer.android.com/reference/android/location/Geocoder.html

Upvotes: 1

Related Questions