android SetOnInfo WindowClickListener-传递更多值
我在我的android应用程序中使用谷歌地图,上面有很多标记。单击标记后,将显示标题。但我需要在点击该标题后打开一个具有更多值的新活动-它将显示在文本视图中 我当前的解决方案现在可以工作了,但每次都会打开相同的信息-如果单击任何标记,就会传递上一项的值。我需要每个标记传递它的值android SetOnInfo WindowClickListener-传递更多值,android,google-maps,onclicklistener,Android,Google Maps,Onclicklistener,我在我的android应用程序中使用谷歌地图,上面有很多标记。单击标记后,将显示标题。但我需要在点击该标题后打开一个具有更多值的新活动-它将显示在文本视图中 我当前的解决方案现在可以工作了,但每次都会打开相同的信息-如果单击任何标记,就会传递上一项的值。我需要每个标记传递它的值 void createMarkersFromJson(String json) throws JSONException { JSONObject jsonObj = new JSONObject(js
void createMarkersFromJson(String json) throws JSONException {
JSONObject jsonObj = new JSONObject(json);
JSONArray actors = jsonObj.getJSONArray("result");
for (int i = 0; i < actors.length(); i++) {
final JSONObject c = actors.getJSONObject(i);
final double g1 = Double.parseDouble(c.getString("gps1"));
final double g2 = Double.parseDouble(c.getString("gps2"));
LatLng poss = new LatLng(g1,g2);
final String title = c.getString("name");
final String place = c.getString("place");
final String place2 = c.getString("place2");
final String perexfull = c.getString("perexfull");
final String img1 = c.getString("img1");
final String info = c.getString("info");
mMap.addMarker(new MarkerOptions().position(poss).title(title).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, SingleitemView.class);
intent.putExtra("place", place);
intent.putExtra("place2", place2);
intent.putExtra("perexfull", perexfull);
intent.putExtra("name", title);
intent.putExtra("img1", img1);
intent.putExtra("info", info);
startActivity(intent);
}
});
}
}
void createMarkersFromJson(字符串json)抛出JSONException{
JSONObject jsonObj=新的JSONObject(json);
JSONArray actors=jsonObj.getJSONArray(“结果”);
对于(int i=0;i
我还尝试将SetOnInfo WindowClickListener从for循环中移出,但随后的变量(如place、info)未知。不确定是否完全正确,但此解决方案最终对我有效!我将SetOnInfo WindowClickListener从循环中取出,然后:
HashMap<Marker, String> values1 = new HashMap<Marker, String>();
HashMap<Marker, String> values2 = new HashMap<Marker, String>();
values1.put(marker, c.getString("place"));
values1.put(marker, c.getString("place2"));
首先,定义一个MarkerInfo类,该类将保存每个标记的所有信息:
public class MarkerInfo {
public String mTitle;
public String mPlace;
public String mPlace2;
public String mPerexfull;
public String mImg1;
public String mInfo;
public MarkerInfo(String title, String place, String place2, String perexfull, String img1, String info) {
mTitle = title;
mPlace = place;
mPlace2 = place2;
mPerexfull = perexfull;
mImg1 = img1;
mInfo = info;
}
}
然后,将HashMap定义为成员变量,Marker作为键,MarkerInfo对象作为值:
GoogleMap mMap;
Map<Marker, MarkerInfo> mMarkerMap = new HashMap<>();
GoogleMap-mMap;
Map mMarkerMap=newhashmap();
然后,为每个标记创建一个MarkerInfo对象,并将其添加到HashMap中。然后,单击信息窗口时,从相应的MarkerInfo对象检索信息:
void createMarkersFromJson(String json) throws JSONException {
JSONObject jsonObj = new JSONObject(json);
JSONArray actors = jsonObj.getJSONArray("result");
for (int i = 0; i < actors.length(); i++) {
final JSONObject c = actors.getJSONObject(i);
final double g1 = Double.parseDouble(c.getString("gps1"));
final double g2 = Double.parseDouble(c.getString("gps2"));
LatLng poss = new LatLng(g1,g2);
final String title = c.getString("name");
final String place = c.getString("place");
final String place2 = c.getString("place2");
final String perexfull = c.getString("perexfull");
final String img1 = c.getString("img1");
final String info = c.getString("info");
Marker marker = mMap.addMarker(new MarkerOptions().position(poss).title(title).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));
MarkerInfo markerInfo = new MarkerInfo(title, place, place2, perexfull, img1, info);
mMarkerMap.put(marker, markerInfo);
}
//Set this only once:
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
MarkerInfo markerInfo = mMarkerMap.get(marker);
Intent intent = new Intent(MapsActivity.this, SingleitemView.class);
intent.putExtra("place", markerInfo.mPlace);
intent.putExtra("place2", markerInfo.mPlace2);
intent.putExtra("perexfull", markerInfo.mPerexfull);
intent.putExtra("name", markerInfo.mTitle);
intent.putExtra("img1", markerInfo.mImg1);
intent.putExtra("info", markerInfo.mInfo);
startActivity(intent);
}
});
}
void createMarkersFromJson(字符串json)抛出JSONException{
JSONObject jsonObj=新的JSONObject(json);
JSONArray actors=jsonObj.getJSONArray(“结果”);
对于(int i=0;i
您的思路是正确的,但请参阅我的答案,以获得更干净的解决方案。您不需要为每个数据项单独设置一个HashMap。完美,看起来更好,效果也很好!谢谢解释:)
void createMarkersFromJson(String json) throws JSONException {
JSONObject jsonObj = new JSONObject(json);
JSONArray actors = jsonObj.getJSONArray("result");
for (int i = 0; i < actors.length(); i++) {
final JSONObject c = actors.getJSONObject(i);
final double g1 = Double.parseDouble(c.getString("gps1"));
final double g2 = Double.parseDouble(c.getString("gps2"));
LatLng poss = new LatLng(g1,g2);
final String title = c.getString("name");
final String place = c.getString("place");
final String place2 = c.getString("place2");
final String perexfull = c.getString("perexfull");
final String img1 = c.getString("img1");
final String info = c.getString("info");
Marker marker = mMap.addMarker(new MarkerOptions().position(poss).title(title).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));
MarkerInfo markerInfo = new MarkerInfo(title, place, place2, perexfull, img1, info);
mMarkerMap.put(marker, markerInfo);
}
//Set this only once:
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
MarkerInfo markerInfo = mMarkerMap.get(marker);
Intent intent = new Intent(MapsActivity.this, SingleitemView.class);
intent.putExtra("place", markerInfo.mPlace);
intent.putExtra("place2", markerInfo.mPlace2);
intent.putExtra("perexfull", markerInfo.mPerexfull);
intent.putExtra("name", markerInfo.mTitle);
intent.putExtra("img1", markerInfo.mImg1);
intent.putExtra("info", markerInfo.mInfo);
startActivity(intent);
}
});
}