【问题标题】:how to attach a TextView on a google map Marker如何在谷歌地图标记上附加 TextView
【发布时间】:2014-12-09 20:12:27
【问题描述】:

我可能已经问过一个类似的问题,但没有得到任何答案,因为我对自己想要做什么并不十分准确。 所以基本上我正在使用 Chris Broadfoot 的 android-maps-utils (https://github.com/googlemaps/android-maps-utils) 在我的 Android 应用程序的 Google 地图上的 BubbleIcons 中显示简短的信息消息。

问题是我想将 TextView 附加到地图上标记的相对位置,以显示一些其他动态信息(与这个伟大的库生成的位图图片相反);就我而言,这是一种倒计时。

有没有人知道我如何以编程方式做到这一点。

提前致谢,祝你有美好的一天!

【问题讨论】:

    标签: android google-maps google-maps-markers google-maps-api-2


    【解决方案1】:

    为标记创建自定义布局,如下所示

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/tv_marker_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:textSize="12sp"
            android:gravity="center_horizontal"
            android:textColor="@color/white"
            android:background="@drawable/ic_center_pin_green_black"/>
    </FrameLayout>
    

    充气并将其添加为标记图标

    View marker = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custome_proximity_pin_green, null);
    TextView tv_marker_text = (TextView) marker.findViewById(R.id.tv_marker_text);
    tv_marker_text.setText("20");
    
    
    meterMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(meterModel.getLatitude(),meterModel.getLongitude())).icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(getContext(), marker))));
    

    createDrawableFromView 方法是

    private Bitmap createDrawableFromView(Context context, View view) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
    
        return bitmap;
    }
    

    【讨论】:

      【解决方案2】:

      该库有一个类来处理标记图标,它称为 BubbleIconFactory。默认情况下,它会更改标记内的文本,但您可以将其更改为您想要的任何内容,只需像这样设置内容视图:

      BubbleIconFactory mBubbleFactory = new BubbleIconFactory(this);
      mBubbleFactory.setContentView(mImageView);
      mImageView.setImageBitmap(.....);
      
      Bitmap iconBitmap = mBubbleFactory.makeIcon();
      
      mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(iconBitmap)).position(MyPosition).title("My current position"));
      

      您可以在标记内使用 Image 来执行此操作,但它应该与您想要的所有内容相同,因为您在 setContentView 方法中将视图设置为参数。请注意,地图中的标记和 InfoWindows 不是实时视图,但它们像图像一样渲染,因此无法“刷新”内容而不再次渲染。 This 视频将帮助您解决这个问题,我不确定,但我猜 BubbleIconFactory 现在可能已被弃用,您应该改用 IconGenerator,在库代码中查找它。希望对您有所帮助。

      编辑:

      还可以看看 plains google maps 是如何工作的,以便更好地了解图书馆正在使用什么:

      https://developers.google.com/maps/documentation/android/marker?hl=es

      https://developers.google.com/maps/documentation/android/infowindows?hl=es#custom_info_windows

      【讨论】:

      • 非常感谢,我确实使用 IconGenerator。我已经看过您链接的视频,我在问自己应该将哪一部分代码放入 AssyncTask 以使图标生成加载更快。再次感谢。
      猜你喜欢
      • 1970-01-01
      • 2016-03-16
      • 2017-12-25
      • 1970-01-01
      • 2019-03-05
      • 2017-03-27
      • 2021-09-12
      • 1970-01-01
      相关资源
      最近更新 更多