Skip to main content

Geocoder is not working, instead use google maps

I noticed that Geocoder does not work for a month in my app. I tested it different simulator and device. But i never get a address. After test i search another method for find address. While i search this, i notice i am not alone with geocoder issue. Other developers use a work arround. I added a few line of code for encoding UTF-8 characters.

public static List<Address> getStringFromLocation(double lat, double lng)
        throws ClientProtocolException, IOException, JSONException {
String address = String.format(Locale.ENGLISH, "http://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=false", lat, lng);
HttpGet httpGet = new HttpGet(address);
HttpClient client = new DefaultHttpClient();

client.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
List<Address> retList = null
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();

InputStream stream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String line;
while ((line = reader.readLine()) != null)
stringBuilder.append(line);
    JSONObject jsonObject = new JSONObject();
jsonObject = new JSONObject(stringBuilder.toString());

retList = new ArrayList<Address>();
if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
   JSONArray results = jsonObject.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
    JSONObject result = results.getJSONObject(i);
    String indiStr = result.getString("formatted_address");
   Address addr = new Address(Locale.getDefault());
    addr.setAddressLine(0, indiStr);
    retList.add(addr);
}  
}
return retList;
}

I hope you can use this helpful. And i update Show The Way. 
Here is change log;
#2.3
FC Bugs fixed
No location issue solved
Unnecessary dialogs removed
No address issue solved
Reduce ads show
Refactor location code
Library update



If you find my blog or apps usefull, please consider donation.


 


Comments

Popular posts from this blog

Meteor Crush

You are the last hope of world Hit meteors and save everyone Accelerate satellites and crush meteors.  Clear endless danger waves. Tear meteors to pieces so planet and all living things can live. Smash meteorites and help world. Meteor Crush https://play.google.com/store/apps/details?id=com.kozaxinan.meteorcrush       

Using Accelerometer when Screen Off

Introduction Android Phones have a lot of built-in sensor such as gyro, proximity and accelerometer. Using Accelerometer in Android is a known thing. For listening sensors, class or service needs a SensorEventListener implementation. An Activity is only exists when you are seeing it. When phone enters standby mode, activity which was recently active frozen and restored whenever needed. While this frozen time, activity doesn’t run. Because of that it doesn’t need sensor updates. And Android stop to listen some sensors for power management. Services have this condition like classes (activity). In contrast to class, a service run every time, even when phone is in standby mode. Therefore Services needs sensor updates for some situations. But android stop to listen some sensor(accelerometer) in some different phones. There is a solution for that. This is not an exact solution but it work with a lot of phones. With a BroadcastReceiver listen   Intent. ACTION...

Finally I released my First Game; The Red Button

Finally I released  "The Red Button Don't Touch It" . My First Game for Android. Game has only one objective; be fast enough to react and click the green button. Playstore Link :  https://play.google.com/store/apps/details?id=com.kozaxinan.redbutton Please don't hesitate to give me feedback!