gpt4 book ai didi

android - 如何在 Google map 自定义信息窗口中仅显示标题和 fragment ?

转载 作者:行者123 更新时间:2023-12-02 21:17:02 25 4
gpt4 key购买 nike

我很难处理谷歌地图的信息窗口。一切都很顺利,直到我尝试在循环中添加信息!我只是无法在循环中添加信息并将其显示在信息窗口中。这是一个自定义信息窗口,我从布局 xml 文件中扩充它。我已经成功地在循环中添加了多个标记(我收到带有信息的 jsonarray 并在循环中添加了接收到的经度和纬度的标记)。问题是Google map 只需要两种字符串,即标题和 fragment ,但我有5条信息博客要放入信息窗口,那么我该怎么办呢?默认情况下,我无法将图像添加到信息窗口,这就是我创建自定义信息窗口的目的。 (我将发布循环和信息窗口的代码)

public void addpaqs(){
try {
JSONArray array = new JSONArray(paqsresponse);

for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
tolongitude = row.getString("to_longitude");
tolatitude = row.getString("to_latitude");
creatorfirstname = row.getString("creator_first_name");
creatorlastname = row.getString("creator_last_name");
paqtype = row.getString("paq_type");
startdate = row.getString("start_date");
enddate=row.getString("end_date");
fromplace = row.getString("from_country");
toplace = row.getString("to_country");
fromcity =row.getString("from_city");
tocity = row.getString("to_city");
String price = row.getString("price");
double tolongdouble = Double.parseDouble(tolongitude);
double tolatdouble = Double.parseDouble(tolatitude);
MarkerOptions options = new MarkerOptions();
options.position(new LatLng(tolatdouble, tolongdouble));
options.Price(price);
options.icon(BitmapDescriptorFactory.fromResource(R.drawable.paqqyinactive));
options.snippet(creatorfirstname+creatorlastname);
map2.addMarker(options);

}
} catch (JSONException e) {
e.printStackTrace();
}

}

在循环内使用 addpaqs() 方法现在是我的自定义信息窗口

 addpaqs();
map2.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}

@Override
public View getInfoContents(Marker marker) {
// Setting up the infoWindow with current's marker info
//infoSnippet.setText(marker.getSnippet());
infoButtonListener.setMarker(marker);
firstnamelastname.setText(marker.getPrice());
Log.d("firstnamelastname",marker.getPrice().toString());

// We must call this to set the current marker and infoWindow references
// to the MapWrapperLayout
mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
return infoWindow;
}
});
enter code here
enter code here

这里我调用addpaqs,后来我只是不知道如何处理它!:(编辑:我的自定义信息窗口(如何设置)

 private ViewGroup infoWindow;     
this.infoWindow = (ViewGroup) getLayoutInflater().inflate(R.layout.infowindow, null);
this.firstnamelastname = (TextView) infoWindow.findViewById(R.id.firstnamelastname);
this.infoButton = (Button) infoWindow.findViewById(R.id.button);

最佳答案

好的,我得到了一个简单的示例。这里的总体思想是使用包装类来存储每个 Marker 的数据。 ,然后将数据存储在 HashMap 中以Marker ID为key,即可在 InfoWindowAdapter 中获取.

首先,为每个Marker对应的信息创建一个holder类(您可以根据需要对此进行扩展以包含更多信息):

    public class MarkerHolder {
public String startdate;
public String enddate;
public String fromplace;
public String toplace;

public MarkerHolder(String sd, String ed, String fp, String tp) {
startdate = sd;
enddate = ed;
fromplace = fp;
toplace = tp;
}
}

然后创建一个HashMap<String, MarkerHolder>这将映射每个 Marker每个 Marker 的信息 ID ,并将其设为实例变量:

HashMap<String, MarkerHolder> markerHolderMap = new HashMap<String, MarkerHolder>();

这是一个简单的示例,仅添加一个 Marker ,记下信息添加到HashMap的位置。与 Marker ID作为键:

public void addpaqs() {

//Simple example with just one Marker:
String creatorfirstname = "creator_first_name";
String creatorlastname = "creator_last_name";
String paqtype = "paq_type";
String startdate = "start_date";
String enddate = "end_date";
String fromplace = "from_country";
String toplace = "to_country";
String fromcity = "from_city";
String tocity = "to_city";
//String price = row.getString("price");
double tolongdouble = -122.417506;
double tolatdouble = 37.77657;
MarkerOptions options = new MarkerOptions();
options.position(new LatLng(tolatdouble, tolongdouble));
//options.Price(price);
//options.icon(BitmapDescriptorFactory.fromResource(R.drawable.paqqyinactive));
options.title(paqtype);
options.snippet(creatorfirstname + " " + creatorlastname);
Marker marker = mGoogleMap.addMarker(options);

MarkerHolder mHolder = new MarkerHolder(startdate, enddate, fromplace, toplace);
markerHolderMap.put(marker.getId(), mHolder); //Add info to HashMap
}

这是 InfoWindow 的自定义布局 xml ,您可以根据需要对此进行扩展:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
android:background="#d3d3d3">

<TextView
android:id="@+id/paq"
android:textColor="#D3649F"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/names"
android:textColor="#D3649F"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/dates"
android:textColor="#D3649F"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/places"
android:textColor="#D3649F"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

然后,把它们放在一起,这是 InfoWindowAdapter 。请注意,我使用了存储在 Marker 中的标题和 fragment 。 ,但也使用了从 MarkerHolder 获得的信息这是从 HashMap 获得的:

 mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

@Override
public View getInfoWindow(Marker arg0) {
return null;
}

@Override
public View getInfoContents(Marker arg0) {

View v = getLayoutInflater().inflate(R.layout.customlayout2, null);

TextView tLocation = (TextView) v.findViewById(R.id.paq);

TextView tSnippet = (TextView) v.findViewById(R.id.names);

TextView tDates = (TextView) v.findViewById(R.id.dates);

TextView tPlaces = (TextView) v.findViewById(R.id.places);

//These are standard, just uses the Title and Snippet
tLocation.setText(arg0.getTitle());

tSnippet.setText(arg0.getSnippet());

//Now get the extra info you need from the HashMap
//Store it in a MarkerHolder Object
MarkerHolder mHolder = markerHolderMap.get(arg0.getId()); //use the ID to get the info

tDates.setText(mHolder.startdate + " " + mHolder.enddate);

tPlaces.setText(mHolder.fromplace + " " + mHolder.toplace);

return v;

}
});

结果:

enter image description here

关于android - 如何在 Google map 自定义信息窗口中仅显示标题和 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30068639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com