Created
November 26, 2014 01:35
-
-
Save devrath/de8433b8147a7f1992b2 to your computer and use it in GitHub Desktop.
Getting address and the LatLng from GoogleMap API using Geocoding by passing address as parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.reversegeocoding; | |
| import java.io.BufferedReader; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.io.UnsupportedEncodingException; | |
| import java.net.URLEncoder; | |
| import java.util.Locale; | |
| import org.apache.http.HttpEntity; | |
| import org.apache.http.HttpResponse; | |
| import org.apache.http.client.HttpClient; | |
| import org.apache.http.client.methods.HttpPost; | |
| import org.apache.http.impl.client.DefaultHttpClient; | |
| import org.json.JSONArray; | |
| import org.json.JSONException; | |
| import org.json.JSONObject; | |
| import android.app.Activity; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import com.google.gson.JsonElement; | |
| import com.google.gson.JsonObject; | |
| public class MainActivity extends Activity { | |
| JSONObject jsonObject; | |
| JSONArray jsonArray= null; | |
| JsonElement gsonJelement; | |
| JsonObject gsonJobject; | |
| String country,pin,state,city,area,latitude,longitude; | |
| String url="http://maps.googleapis.com/maps/api/geocode/json?sensor=false"; | |
| String locationName="Outer ringroad, springfield apartments, bangalore"; | |
| //API CALL:http://maps.google.com/maps/api/geocode/json?address=mumbai&sensor=false | |
| //BEST CASE URL:http://maps.google.com/maps/api/geocode/json?sensor=false&address=outer%20ring%20road,springfield%20apartments,sarjapur,bangalore | |
| //WORST CASE URL:http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=bangalore | |
| StringBuilder finalRequest, finalLocality; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| try { | |
| finalRequest = new StringBuilder(url); | |
| finalLocality = new StringBuilder(); | |
| //Encode the String params | |
| finalRequest.append("&language=").append(Locale.getDefault().getLanguage()); | |
| finalRequest.append("&address=").append(URLEncoder.encode(locationName, "UTF-8")); | |
| //Get JSON response from the request | |
| jsonObject=getJSONfromURL(finalRequest.toString()); | |
| jsonArray=jsonObject.getJSONArray("results"); | |
| jsonObject=jsonArray.getJSONObject(0); | |
| JSONObject jsonObjectLatLng=jsonObject.getJSONObject("geometry"); | |
| jsonObjectLatLng=jsonObjectLatLng.getJSONObject("location"); | |
| String referenceOne=jsonObject.getString("formatted_address"); | |
| jsonArray=jsonObject.getJSONArray("address_components"); | |
| jsonObject=jsonArray.getJSONObject(0); | |
| finalLocality.append(jsonObject.getString("long_name")); | |
| finalLocality.append(", "); | |
| jsonObject=jsonArray.getJSONObject(1); | |
| finalLocality.append(jsonObject.getString("long_name")); | |
| finalLocality.append(", "); | |
| jsonObject=jsonArray.getJSONObject(2); | |
| finalLocality.append(jsonObject.getString("long_name")); | |
| finalLocality.append(", "); | |
| jsonObject=jsonArray.getJSONObject(3); | |
| finalLocality.append(jsonObject.getString("long_name")); | |
| String[] parts = referenceOne.split(", "); | |
| country=parts[parts.length-1]; | |
| String stateRef=parts[parts.length-2]; | |
| String[] statePinParts=stateRef.split(" "); | |
| pin=statePinParts[statePinParts.length-1]; | |
| state=statePinParts[statePinParts.length-2]; | |
| city=parts[parts.length-3]; | |
| area=finalLocality.toString(); | |
| latitude=jsonObjectLatLng.getString("lat"); | |
| longitude=jsonObjectLatLng.getString("lng"); | |
| } catch (UnsupportedEncodingException e) { | |
| e.printStackTrace(); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static JSONObject getJSONfromURL(String url) { | |
| InputStream is = null; | |
| String result = ""; | |
| JSONObject jArray = null; | |
| // Download JSON data from URL | |
| try { | |
| HttpClient httpclient = new DefaultHttpClient(); | |
| HttpPost httppost = new HttpPost(url); | |
| HttpResponse response = httpclient.execute(httppost); | |
| HttpEntity entity = response.getEntity(); | |
| is = entity.getContent(); | |
| } catch (Exception e) { | |
| Log.e("log_tag", "Error in http connection " + e.toString()); | |
| } | |
| // Convert response to string | |
| try { | |
| BufferedReader reader = new BufferedReader(new InputStreamReader( | |
| is, "iso-8859-1"), 8); | |
| StringBuilder sb = new StringBuilder(); | |
| String line = null; | |
| while ((line = reader.readLine()) != null) { | |
| sb.append(line + "\n"); | |
| } | |
| is.close(); | |
| result = sb.toString(); | |
| } catch (Exception e) { | |
| Log.e("log_tag", "Error converting result " + e.toString()); | |
| } | |
| try { | |
| jArray = new JSONObject(result); | |
| } catch (JSONException e) { | |
| Log.e("log_tag", "Error parsing data " + e.toString()); | |
| } | |
| return jArray; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment