我有一个应用程序,它将在数据库中显示地图上的一些位置。一切正常,但我不想在我的自定义信息窗口中显示评级栏。我已经尝试了一些教程,但问题是我从JSON获取我的数据,使用php。它工作,但评级栏默认是最后一个,从数据库中检索的信息。
这是我的班级,实施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
答案 0 :(得分:0)
问题您无法在标记
上设置任何其他内容BitmapDescriptor
解决方案这是将任何xml布局转换为位图的方法。您可以将布局设置为位图。
这是魔术
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(bitmap));
marker.setIcon(icon);
和
private Bitmap getMarkerBitmapFromView(@Nullable Bitmap loadedImage) {
View customMarkerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_marker_view, null);
ImageView markerImageView = customMarkerView.findViewById(R.id.civProfile);
if (loadedImage != null)
markerImageView.setImageBitmap(loadedImage);
customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
customMarkerView.buildDrawingCache();
Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = customMarkerView.getBackground();
if (drawable != null)
drawable.draw(canvas);
customMarkerView.draw(canvas);
return returnedBitmap;
}
和layout_marker_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_marker_view"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/ic_marker_blue"
android:gravity="center"
android:padding="4dp">
<com.baselib.view.CircleImageView
android:id="@+id/civProfile"
android:layout_width="43dp"
android:layout_height="43dp"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
/>
</LinearLayout>
答案 1 :(得分:0)
无需通过适配器的额定值,只需设置一次适配器即可。另外,将相机放置一次,因为循环具有相同的效果。对于每个人的个人评分,请使用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;
}