android - 自定义信息窗口,谷歌地图
问题描述
我有一个应用程序,它将从数据库中显示地图上的一些位置。一切正常,但我不想在我的自定义信息窗口中显示评分栏。我尝试了一些教程,但问题是我使用 php 从 JSON 获取数据。它可以工作,但默认情况下,评级栏是从数据库中检索到的最后一个信息。
这是我的课,实施GoogleMap.InfoWindowAdapter
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private Activity context;
private int rating;
private RatingBar RTB;
public CustomInfoWindowAdapter(Activity context,int rating){
this.context = context;
this.rating=rating;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View view = context.getLayoutInflater().inflate(R.layout.customwindow, null);
RTB = (RatingBar) view.findViewById(R.id.mark_rating);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
TextView tvSubTitle = (TextView) view.findViewById(R.id.tv_subtitle);
RTB.setRating(rating);
tvTitle.setText(marker.getTitle());
tvSubTitle.setText(marker.getSnippet());
return view;
}
这是我添加标记的地方
for(int i=0 ; i<response.length();i++){
JSONObject person = (JSONObject) response.get(i);
String name = person.getString("nom");
String long_i = person.getString("longitude");
String lat_i = person.getString("latitude");
int rating = person.getInt("rating");
mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(lat_i) , Double.parseDouble(long_i)))
.title(name)
.snippet("Nothing")
.icon(BitmapDescriptorFactory
.fromBitmap(resizeMapIcons("doctor_icon",85,85))));
CustomInfoWindowAdapter adapter = new CustomInfoWindowAdapter(MapsActivity.this,rating);
mMap.setInfoWindowAdapter(adapter);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.6850,90.3563), 6.0f));
}
对于布局文件,我有 2 个 textview 和 1 个 ratingBar
解决方案
无需在适配器中通过等级,只需设置一次适配器。此外,将相机定位一次,因为在循环中它具有相同的效果。对于每个人的个人评分,使用 setTag() 将对象与标记相关联,如下所示:
CustomInfoWindowAdapter adapter = new CustomInfoWindowAdapter(MapsActivity.this);
mMap.setInfoWindowAdapter(adapter);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.6850,90.3563), 6.0f));
// associate objects with markers
for(int i=0 ; i<response.length();i++){
JSONObject person = (JSONObject) response.get(i);
String name = person.getString("nom");
String long_i = person.getString("longitude");
String lat_i = person.getString("latitude");
mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(lat_i) , Double.parseDouble(long_i)))
.title(name)
.snippet("Nothing")
.icon(BitmapDescriptorFactory
.fromBitmap(resizeMapIcons("doctor_icon",85,85)))).setTag(person);
}
在您的适配器中,执行此操作以在 getInfoContents() 中设置评级:
@Override
public View getInfoContents(Marker marker) {
View view = context.getLayoutInflater().inflate(R.layout.customwindow, null);
RTB = (RatingBar) view.findViewById(R.id.mark_rating);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
TextView tvSubTitle = (TextView) view.findViewById(R.id.tv_subtitle);
tvTitle.setText(marker.getTitle());
tvSubTitle.setText(marker.getSnippet());
// get json object associated with it:
JSONObject person = (JSONObject) marker.getTag();
// get rating:
int rating = person.getInt("rating");
// set it
if (rating!= null){
RTB.setRating(rating)
}
return view;
}
推荐阅读
- view - 如何在表单视图中显示来自另一个菜单模型的特定参数?(Odoo 13 企业版)
- python - Django 选择对象未将 id 传递给 URL
- java - 无法使用java客户端访问docker for windows中的rabbitmq
- android - 在 Audio Manager 对象上发送额外数据以确保 android.media.RINGER_MODE_CHANGED Action Receiver 接收到此信息
- postgresql - Postgres/jOOQ 替换 jsonb[] 元素
- excel - 堆叠数据并添加折线
- python - 如何用虚线连接散点图的点?
- symfony - Symfony 5 httpClient Windows AD 连接
- r - 将数据框中的列分成其他几列
- .net - 突然得到:HTTP 请求未经授权,客户端身份验证方案“协商”