【问题标题】:Custom info window, google maps自定义信息窗口,谷歌地图
【发布时间】:2019-06-07 05:52:05
【问题描述】:

我有一个应用程序,它将从数据库中显示地图上的一些位置。一切正常,但我不想在我的自定义信息窗口中显示评分栏。我尝试了一些教程,但问题是我使用 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

【问题讨论】:

  • 自定义信息窗口到底是什么问题?

标签: android google-maps


【解决方案1】:

问题你不能在标记上设置其他任何东西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>

【讨论】:

  • 谢谢,但我想你没有理解我的问题。我想要的只是从数据库中检索每个用户的评分并将其添加到评分栏。正如您在图片中看到的那样,它获取最后一个评级并将其设置为所有 ratikg 条
  • 我还是没听懂你,你想要一个可触摸的评分栏吗?
  • 一点也不。正如您在屏幕上看到的那样,我已经在自定义信息窗口中有评分栏,但问题是。它对所有标记显示相同的评分。
【解决方案2】:

无需在适配器中通过等级,只需设置一次适配器。此外,将相机定位一次,因为在循环中它具有相同的效果。对于每个人的个人评分,使用 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;
}

【讨论】:

    猜你喜欢
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2020-05-29
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多