如何在 Google 地图的标记中添加多个位图图标?
How to add more than one bitmap icon in markers for Google Maps?
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));
我正在尝试在单个标记上同时添加自定义标记和图标文本。但它只显示我的自定义标记或我的文本。任何解决方案或替代方案?
您可以通过扩充自定义布局来完成此操作,
第 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>
第 2 步:获取自定义标记的位图
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")));
尝试下面的代码
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);
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));
我正在尝试在单个标记上同时添加自定义标记和图标文本。但它只显示我的自定义标记或我的文本。任何解决方案或替代方案?
您可以通过扩充自定义布局来完成此操作,
第 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>
第 2 步:获取自定义标记的位图
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")));
尝试下面的代码
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);