编辑!
我看到我在它们到达 onPostExecute 之前覆盖了我的 lat long 值。有人建议我在传递给 onPostExecute 之前使用一个数组来存储信息。我认为数组每个索引只能容纳 1 个对象。由于我想为每个索引保留多个联系人项目(电话、地址、电子邮件等),我该怎么办?例如,对于每个标记点,我不仅要提取 lat long 值,还要提取要添加到标记的名称。
/编辑!
我正在尝试通过 maps api v2 添加多个标记。我通过 JSON 字符串提取地址。当我运行它时,我只会得到最后一个地址以在地图上显示为标记。我将如何更改下面的循环以使其显示每个标记。(添加标记位于 onPostExecute)谢谢!
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
public Long doInBackground(Context... contexts) {
Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
String apples = endpoint.listContactInfo().execute().toString();
JSONObject jObject = new JSONObject(apples);
JSONArray jsonArr = jObject.getJSONArray("items");
for(int i =0 ; i<jsonArr.length() ;i++ ){
JSONObject jsonObj1 = jsonArr.getJSONObject(i);
// Storing each json item in variable
String id = jsonObj1.getString(TAG_ID);
String nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
String nameLast1 = jsonObj1.getString(TAG_LASTNAME);
String emailAddress1 = jsonObj1.getString(TAG_EMAIL);
String streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
String phone1 = jsonObj1.getString(TAG_PHONE);
//test to see if made it to string
Log.d("YOUR_TAG", "First Name: " + nameFirst1 + " Last Name: " + nameLast1);
address = coder.getFromLocationName(streetAddress1,5);
Address location1 = address.get(0);
// SET LAT LNG VALUES FOR MARKER POINT
lati = location1.getLatitude();
longi = location1.getLongitude();
Log.d("Location", "Location:" + lati + " " + longi);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (long) 0;
}
// ADD MARKER TO MAP UI
protected void onPostExecute(Long result) {
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.title("Hello world"));
}
}