我根据通过JSON响应从Web API收到的lat和lon绘制标记。
问题是每个标记都有自己的数据集,例如每个标记都有ID,代码,电话号码和名称。我想在每个Marker的InfoWindow中使用自定义InfoWindowAdapter显示所有这些数据,但我无法显示JSON中收到的相关数据。
这是我到目前为止所尝试的内容。在此代码中,所有标记代码段都包含相同的数据,我想在每个标记的代码段中添加不同的数据:
private class GetFixture extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg) {
ServiceHandler serviceClient = new ServiceHandler();
Log.d("url: ", "> " + URL_ITEMS);
String json = serviceClient.makeServiceCall(URL_ITEMS, ServiceHandler.GET);
// print the json response in the log
Log.d("Get match fixture resps", "> " + json);
if (json != null) {
try {
Log.d("try", "in the try");
JSONArray jsonArray = new JSONArray(json);
Log.d("jsonObject", "new json Object");
// Getting JSON Array node
for (int i = 0; i <= jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
Double matchId = Double.parseDouble(c.getString(TAG_MATCHID));
Log.d("matchId", String.valueOf(matchId));
Double teamA = Double.valueOf(c.getString(TAG_TEAMA));
Log.d("teamA", String.valueOf(teamA));
teamB = c.getString(TAG_TEAMB);
Log.d("teamB", teamB);
// hashmap for single match
HashMap<String, String> matchFixture = new HashMap<String, String>();
HashMap<String, String> cc = new HashMap<String, String>();
// adding each child node to HashMap key => value
matchFixture.put(TAG_MATCHID, String.valueOf(matchId));
matchFixture.put(TAG_TEAMA, String.valueOf(teamA));
areahash.put(TAG_TEAMB, teamB);
matchFixtureList.add(new LatLng(matchId, teamA));
cc.put(TAG_TEAMB,teamB);
area.add(teamB);
}
} catch (JSONException e) {
Log.d("catch", "in the catch");
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
int x;
//for (LatLng point : matchFixtureList) {
//for (String a:area)
for ( i = 0; i < matchFixtureList.size(); i++) {
mMap.addMarker(new MarkerOptions().position(matchFixtureList.get(i)).title(area.get(i)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(matchFixtureList.get(i), 15));
mMap.animateCamera(CameraUpdateFactory.zoomIn());
// Zoom out to zoom level 10, animating with a duration of 2 seconds.
mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
if (mMap != null) {
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.check, null);
TextView ashid = (TextView) v.findViewById(R.id.ash_id);
TextView ashcode = (TextView) v.findViewById(R.id.ash_code);
TextView Cellno = (TextView) v.findViewById(R.id.cell_id);
TextView Ashname = (TextView) v.findViewById(R.id.ash_name);
TextView NIC = (TextView) v.findViewById(R.id.nic_id);
TextView tvlat = (TextView) v.findViewById(R.id.lat_id);
TextView tvLng = (TextView) v.findViewById(R.id.lonti_id);
TextView Zmid = (TextView) v.findViewById(R.id.ZM_id);
LatLng ll = marker.getPosition();
ashid.setText(marker.getTitle());
tvlat.setText(""+ll.latitude);
tvLng.setText("" + ll.longitude);
Zmid.setText("" + ll.longitude);
NIC.setText(""+ll.latitude);
Ashname.setText("" + ll.longitude);
Cellno.setText(""+ll.latitude);
ashid.setText("" + ll.longitude);
ashcode.setText(teamB);
ashcode.setText(marker.getSnippet());
return v;
}
});
}
// mMap.addMarker(new MarkerOptions().position(pakistan).title("Marker in pakistn"));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
// mMap.animateCamera(CameraUpdateFactory.zoomIn());
// Zoom out to zoom level 10, animating with a duration of 2 seconds.
// mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
//mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(matchFixtureList(i)));
//mMap.animateCamera(CameraUpdateFactory.zoomTo(5));
}
}
答案 0 :(得分:1)
如果您需要存储的数据点多于标题和摘要,那么解决此问题的最佳方法是使用包装类来存储每个Marker
的数据,然后将数据保存在标记为关键字的HashMap
,以便您可以在InfoWindowAdapter
中获取它。
首先,为每个Marker
对应的信息创建一个持有者类(您可以根据需要对此进行扩展以包含更多信息):
public class MarkerHolder {
public String mAshId;
public String mAshCode;
public String mCellId;
public String mAshName;
public MarkerHolder(String ashId, String ashCode, String cellId, String ashName) {
mAshId = ashId;
mAshCode = ashCode;
mCellId = cellId;
mAshName = ashName;
}
}
然后创建一个HashMap<String, MarkerHolder>
,将每个标记ID映射到每个Marker
的数据,并使其成为活动的实例变量:
HashMap<String, MarkerHolder> markerHolderMap = new HashMap<String, MarkerHolder>();
当你添加每个标记时,在HashMap中添加一个条目以及一个MarkerHolder对象,该对象存储与该标记相关的所有数据:
Marker marker = mMap.addMarker(new MarkerOptions().position(matchFixtureList.get(i)).title(area.get(i)).snippet("snipp works"));
// Assuming these parameters come from your JSON:
MarkerHolder mHolder = new MarkerHolder(ashId, ashCode, cellId, ashName);
// Then add this holder reference to the HashMap
markerHolderMap.put(marker.getId(), mHolder);
然后,在InfoWindowAdapter
中,使用从MarkerHolder
获得的HashMap
获取的信息:
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker arg0) {
View v = getLayoutInflater().inflate(R.layout.check, null);
TextView ashid = (TextView) v.findViewById(R.id.ash_id);
TextView ashcode = (TextView) v.findViewById(R.id.ash_code);
TextView cellNo = (TextView) v.findViewById(R.id.cell_id);
TextView ashName = (TextView) v.findViewById(R.id.ash_name);
//Get the MarkerHolder for this Marker:
MarkerHolder markerHolder = markerHolderMap.get(arg0.getId());
//Use the data to populate the layout:
ashid.setText(markerHolder.mAshId);
ashcode.setText(markerHolder.mAshCode);
cellNo.setText(markerHolder.mCellId);
ashName.setText(markerHolder.mAshName);
//...........
return v;
}
});