Reputation: 1
Ok so we established that GEOCODER dose not work on some mobiles
So I fond this solution, from a guy called @Filip, which seems legit solution
package com.something.something;
import android.os.AsyncTask;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetLocationDownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
String result = "";
URL url;
HttpURLConnection urlConnection;
try {
url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(is);
int data = inputStreamReader.read();
while(data != -1){
char curr = (char) data;
result += curr;
data = inputStreamReader.read();
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
try {
JSONObject locationObject = new JSONObject(result);
JSONObject locationGeo = locationObject.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getJSONObject("lat");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
And I have to run it from my MapsActivity by this coding this:
String searchString = mSearchText.getText().toString();
String link = "https://maps.googleapis.com/maps/api/geocode/json?address=" +
searchString + "&key=MY API KEY";
GetLocationDownloadTask getLocation = new GetLocationDownloadTask();
getLocation.execute(link);
This is all fine, but what I didn't understand is what happens next ? I need those lat lng to process further, how do I get them after EXECUTING(link) ?
Any help is appreciated, it is mind boggling issue. Geocoding needs a backend, and not all devices support.
Upvotes: 0
Views: 58
Reputation: 49986
This is all fine, but what I didn't understand is what happens next ? I need those lat lng to process further, how do I get them after EXECUTING(link) ?
well, you have result in : JSONObject locationGeo
, use getDouble
to parse is as double if that is the correct type. Actually looking at the end of this long line its latitude, so you need also get longitude. Your further processing should be in onPostExecute (its called on UI thread).
example:
In order to provide lat
to your activity you should pass a reference to it to your AsyncTask.:
public class GetLocationDownloadTask extends AsyncTask<String, Void,
String> {
MapsActivity act;
GetLocationDownloadTask(MapsActivity act) {
this.act = act;
}
Then in you onPostExecute
you use act
like this:
@Override protected void onPostExecute(String result) { super.onPostExecute(result);
if (result != null) {
try {
JSONObject locationObject = new JSONObject(result);
JSONObject locationGeo = locationObject.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getJSONObject("lat");
act.passLat(locationGeo.getDouble("Longitude")); // not sure if this is correct
then inside your activity execute correct constructor:
GetLocationDownloadTask getLocation = new GetLocationDownloadTask(this);
I would suggest using here WeakReference to keep activity reference in AsyncTask - but this is more advanced and you are not after that - I suppose.
Upvotes: 1