【发布时间】:2018-11-18 19:17:11
【问题描述】:
好的,所以我们确定 GEOCODER 在某些手机上不起作用
所以我喜欢这个解决方案,来自一个叫 @Filip 的人,这似乎是合法的解决方案
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();
}
}
}
}
我必须通过以下编码从我的 MapsActivity 运行它:
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);
这一切都很好,但我不明白接下来会发生什么?我需要那些 lat lng 进一步处理,在 EXECUTING(link) 之后如何获取它们?
感谢任何帮助,这是令人难以置信的问题。地理编码需要后端,并非所有设备都支持。
【问题讨论】:
标签: android google-maps google-places-api geocoding