我正在制作谷歌地图应用程序。由于Geocoder类返回空(在[0]处越界),我目前正在尝试HTTPS Google映射反向GeoCoding。这是我的onClick呼叫:
public JSONArray getAddress(LatLng latLng) throws IOException {
URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng="+latLng.latitude+","+latLng.longitude+"&key=AIza***********************");
Async async =new Async();
async.execute(url);
JSONArray response = async.jsonArray;
return response;
}
这是异步子类:
public class Async extends AsyncTask<URL, Void, JSONArray> {
public JSONArray jsonArray;
@Override
protected JSONArray doInBackground(URL... params) {
BufferedReader reader;
InputStream is;
try {
StringBuilder responseBuilder = new StringBuilder();
HttpURLConnection conn = (HttpURLConnection) params[0].openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
for (String line; (line = reader.readLine()) != null; ) {
responseBuilder.append(line).append("n");
}
jsonArray = new JSONArray(responseBuilder.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonArray;
}
和manifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
点击方式:
@Override
public void onMapClick(LatLng latLng) {
JSONArray js=null;
try {
js = getAddress(latLng);
mMap.addMarker(new MarkerOptions().position(latLng).title(js.getString(0)).draggable(true));//NullPointer
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException p) {
//todo
}
4天来,我一直在尝试获得不同于NullPointerException的东西。知道我做错了什么吗?在调试模式下连接的平板电脑上进行测试。任何帮助都是有用的。我非常绝望。
由于AsyncTask是Activity的子类,您只需要将LatLng分配给Activity的成员变量,以便在AsyncTask中可以访问它。
然后,在onPostExecute()中添加Marker。
首先,对"活动代码"的更改。除了JSONArray:之外,还制作一个LatLng成员变量
public class MyMapActivity extends AppCompatActivity implements OnMapReadyCallback
{
JSONArray js;
LatLng currLatLng;
//...............
然后,修改onMapClick将LatLng分配给实例变量:
@Override
public void onMapClick(LatLng latLng) {
JSONArray js=null;
try {
//assign to member variable:
currLatLng = latLng;
//don't use the return value:
getAddress(latLng);
//remove this:
//mMap.addMarker(new MarkerOptions().position(latLng).title(js.getString(0)).draggable(true));//NullPointer
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException p) {
//todo
}
然后,使用onPostExecute在获得响应后放置标记。另一个主要问题是JSON响应是一个包含JSONArray的JSONObject。在以下代码中修复:
public class Async extends AsyncTask<URL, Void, JSONArray> {
public JSONArray jsonArray;
@Override
protected JSONArray doInBackground(URL... params) {
BufferedReader reader;
InputStream is;
try {
StringBuilder responseBuilder = new StringBuilder();
HttpURLConnection conn = (HttpURLConnection) params[0].openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
for (String line; (line = reader.readLine()) != null; ) {
responseBuilder.append(line).append("n");
}
JSONObject jObj= new JSONObject(responseBuilder.toString());
jsonArray = jObj.getJSONArray("results");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonArray;
}
@Override
protected void onPostExecute(JSONArray jsonArrayResponse) {
js = jsonArrayResponse;
try {
if (js != null) {
JSONObject jsFirstAddress = js.getJSONObject(0);
mMap.addMarker(new MarkerOptions().position(currLatLng).title(jsFirstAddress.getString("formatted_address")).draggable(true));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
请注意,您不需要从getAddress()返回任何内容,所以只需使返回类型为void
,并让AsyncTask在执行后完成其余操作:
public void getAddress(LatLng latLng) throws IOException {
URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?latlng="+latLng.latitude+","+latLng.longitude+"&key=AIza***********************");
Async async =new Async();
async.execute(url);
//JSONArray response = async.jsonArray;
//return response;
}