如何在Google地图中同时显示所有标记标签? [英] How to show all marker labels in Google Maps simultaenously?
问题描述
我正在编写一个代码来显示所有朋友的标记。这是我在我的地图上添加标记的代码。 我想同时显示所有标记标题。在下面的示例图片中,我可以看到每个CCD商店的标题都是同时显示的。
GoogleMap mMap = //在此初始化地图
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat,lng))
.title(My Nearby Friend)
.icon(BitmapDescriptorFactory.fromResource(R.drawable .ic_profile)));
有人可以告诉我如何达到这个目的吗?
现在需要的是以可自定义布局开始渲染标记图标,该布局将包含可绘制(图标)和文本,如图所示。请注意,标记仍然像标记一样 - 在点击时有标题/ infowindow - 所以在设计布局时需要考虑到这一点。所以如果标题只是复制文本,那么不要设置标题。
test_marker_icon.xml:
<?xml version =1.0encoding =utf-8?>
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/tools
android:orientation =horizontalandroid:layout_width =match_parent
android:layout_height =match_parent
android:layout_centerHorizontal =true
android:layout_centerInParent =true
android:layout_centerVertical =true>
< ImageView
android:layout_width =wrap_content
android:layout_height =wrap_content
android:id =@ + id / testiconimage/> ;
< TextView
android:layout_toRightOf =@ + id / testiconimage
android:id =@ + id / marker_text1
android:layout_width = wrap_content
android:layout_height =wrap_content
android:textAlignment =viewEnd
android:textAppearance =@ style / TextAppearance.AppCompat.Medium
android:textColor = @ color / black
tools:text =test/>
< TextView
android:id =@ + id / marker_text2
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_below =@ id / marker_text1
android:layout_toRightOf =@ + id / testiconimage
android:textAppearance =@ style / TextAppearance.AppCompat.Medium
android :textColor =@ color / black
android:textStyle =italic
tools:text =more text/>
< / RelativeLayout>
并且渲染它的代码 - 在此示例中,两个标记添加了相同的图标, text:
MarkerOptions mo = new MarkerOptions();
mo.position(new LatLng(latLng.latitude + 5,latLng.longitude -.2));
LayoutInflater inflater =(LayoutInflater)getApplicationContext()。getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看v = inflater.inflate(R.layout.test_marker_icon,null,false);
ImageView iv =(ImageView)v.findViewById(R.id.testiconimage);
iv.setImageDrawable(getResources()。getDrawable(R.drawable.face));
TextView tv =(TextView)v.findViewById(R.id.marker_text1);
tv.setText(Marker 1);
tv =(TextView)v.findViewById(R.id.marker_text2);
tv.setText(一个非常好的地方);
标记m = mMap.addMarker(mo);
m.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(MapsActivity.this,v)));
MarkerOptions mo2 = new MarkerOptions();
mo2.position(new LatLng(latLng.latitude + 4.8,latLng.longitude -.2));
tv =(TextView)v.findViewById(R.id.marker_text1);
tv.setText(Marker 2);
tv =(TextView)v.findViewById(R.id.marker_text2);
tv.setText(不如(1));
m = mMap.addMarker(mo2);
m.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(MapsActivity.this,v)));
以及创建位图的支持方法:
//将视图转换为位图
public Bitmap createDrawableFromView(Context context,View view){
DisplayMetrics displayMetrics = new DisplayMetrics();
MapsActivity.this.getWindowManager()。getDefaultDisplay()。getMetrics(displayMetrics);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels,displayMetrics.heightPixels);
view.layout(0,0,displayMetrics.widthPixels,displayMetrics.heightPixels);
view.buildDrawingCache();
位图位图= Bitmap.createBitmap(view.getMeasuredWidth(),view.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
画布画布=新画布(位图);
view.draw(canvas);
返回位图;
}
和样本输出:
点击标记2后输出:
参考资料(没有什么是原创的(笑脸和演示代码除外)): b
$ b
I am writing a code to show all my friends on a marker. This is my code to add markers on my map. I want to show all marker titles simultaneously. In the sample image below, I can see every CCD shop's title is shown simultaneously.
GoogleMap mMap = // map initialized here
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("My Nearby Friend")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_profile)));
Can someone tell me how to achieve this?
Esstentially what is needed is to render the marker icon starting with a customizable layout which will include the drawable(icon) and text as shown.
Note that the marker still behaves like a marker - has a title/infowindow on click - so that needs to be accommodated when designing your layout. So if the title is just duplicating the text then don't set the title.
test_marker_icon.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/testiconimage"/>
<TextView
android:layout_toRightOf="@+id/testiconimage"
android:id="@+id/marker_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="viewEnd"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/black"
tools:text="test" />
<TextView
android:id="@+id/marker_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/marker_text1"
android:layout_toRightOf="@+id/testiconimage"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/black"
android:textStyle="italic"
tools:text="more text" />
</RelativeLayout>
And the code to render it - in this example two markers are added with same icon and each has unique text:
MarkerOptions mo = new MarkerOptions();
mo.position(new LatLng(latLng.latitude+5, latLng.longitude-.2));
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.test_marker_icon, null, false);
ImageView iv = (ImageView) v.findViewById(R.id.testiconimage);
iv.setImageDrawable(getResources().getDrawable(R.drawable.face));
TextView tv = (TextView) v.findViewById(R.id.marker_text1);
tv.setText("Marker 1");
tv = (TextView) v.findViewById(R.id.marker_text2);
tv.setText("A really nice place");
Marker m = mMap.addMarker(mo);
m.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(MapsActivity.this, v)));
MarkerOptions mo2 = new MarkerOptions();
mo2.position(new LatLng(latLng.latitude+4.8, latLng.longitude-.2));
tv = (TextView) v.findViewById(R.id.marker_text1);
tv.setText("Marker 2");
tv = (TextView) v.findViewById(R.id.marker_text2);
tv.setText("Not quite as nice as (1)");
m = mMap.addMarker(mo2);
m.setIcon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(MapsActivity.this, v)));
And the supporting method to create the bitmap:
// Convert a view to bitmap
public Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
MapsActivity.this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.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;
}
And sample output:
And output after clicking on marker 2:
References (nothing is original (except smiley face and demo code)):
这篇关于如何在Google地图中同时显示所有标记标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!