【问题标题】:How to add more than one bitmap icon in markers for Google Maps?如何在谷歌地图的标记中添加多个位图图标?
【发布时间】:2019-05-05 23:53:33
【问题描述】:
IconGenerator iconFactory = new IconGenerator(this);
hM = googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(lati1,longit1))
    .title("HostelM")
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.parkingmarker)));

hM.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.parkingmarker));

我正在尝试在单个标记上同时添加自定义标记和图标文本。但它只显示我的自定义标记或文本。任何解决方案或替代方案?

【问题讨论】:

    标签: android api google-maps marker


    【解决方案1】:

    您可以通过扩展自定义布局来做到这一点,

    第 1 步:创建您的自定义布局。 lay_marker.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <ImageView
            android:id="@+id/img_marker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:contentDescription="@string/app_name"
            android:cropToPadding="false"
            android:scaleType="fitXY"
            android:src="@drawable/map_car_running" />
    
        <TextView
            android:id="@+id/tv_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_marker_lebel"
            android:fontFamily="sans-serif-light"
            android:gravity="center"
            android:minWidth="40dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:text="text"
            android:textColor="@android:color/white" />
    </LinearLayout>
    

    第二步:获取自定义标记的位图

     public Bitmap getMarkerIconWithLabel(Context mContext,String label) {
            IconGenerator iconGenerator = new IconGenerator(mContext);
            View markerView = LayoutInflater.from(mContext).inflate(R.layout.lay_marker, null);
            ImageView imgMarker = markerView.findViewById(R.id.img_marker);
            TextView tvLabel = markerView.findViewById(R.id.tv_label);
    
            imgMarker.setImageResource(R.drawable.car);
    
    
            tvLabel.setText(label);
            iconGenerator.setContentView(markerView);
            iconGenerator.setBackground(null);
    
            return iconGenerator.makeIcon(label);
        }
    

    第 3 步:将此位图应用于标记

    googleMap.addMarker(new MarkerOptions()
        .position(new LatLng(lati1,longit1))
        .title("HostelM")
        .icon(BitmapDescriptorFactory.fromBitmap(this,"Label")));
    

    【讨论】:

      【解决方案2】:

      试试下面的代码

      LatLng source, LatLng dest;
      
      LatLngBounds.Builder builder = new LatLngBounds.Builder();
      IconGenerator iconFactory = new IconGenerator(this);
      iconFactory.setRotation(0);
      iconFactory.setContentRotation(0);
      
      
      MarkerOptions markerOptions = new MarkerOptions();
      markerOptions.position(source);
      iconFactory.setStyle(IconGenerator.STYLE_BLUE);
      markerOptions.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon("Label 1")));
      map.addMarker(markerOptions);
      builder.include(source);
      
      markerOptions = new MarkerOptions();
      markerOptions.position(dest);
      iconFactory.setStyle(IconGenerator.STYLE_GREEN);
      markerOptions.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon("Label 2")));
      map.addMarker(markerOptions);
      builder.include(dest);
      
      LatLngBounds bounds = builder.build();
      
      int width = mSupportMapFragment.getView().getMeasuredWidth();
      int height = mSupportMapFragment.getView().getMeasuredHeight();
      int padding = (int) (width * 0.15);
      
      CameraUpdate update = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
      
      map.animateCamera(update);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-16
        • 2013-04-21
        • 2021-09-12
        • 1970-01-01
        • 2020-06-08
        • 1970-01-01
        • 2021-07-22
        相关资源
        最近更新 更多