如何在 google 地图中同时显示 MarkerIcon 和标题,就像 Google 应用程序一样?
How to show both MarkerIcon and Title in google map as like Google Apps?
当我在 Google 的地图 Android 应用程序中找到最近的餐馆时,餐馆名称默认显示在标记图标附近。 (参考 Google 图片)。
但是在我的应用程序中,当我搜索最近的餐馆时我需要做同样的事情,我只能显示标记图标。 (参考我的申请图片)。
Google 图片:
我的应用图片:
部分解决方案:
在这里我找到了部分解决方案,我们通过使用 canvas 绘制文本来获得它。我通过这些链接使用了下面的代码 here and here 但是 canvas 对我的文本进行了绘图切割。请参阅附图 TextDrawn Image
Marker myLocMarker = map.addMarker(new MarkerOptions()
.position(myLocation)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here"))));
private Bitmap writeTextOnDrawable(int drawableId, String text) {
Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId)
.copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextAlign(Paint.Align.CENTER);
paint.setLinearText(true);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
paint.setTextSize(35);
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
//Calculate the positions
// int xPos = (canvas.getWidth() / 2) - 2; //-2 is for regulating the x position offset
//"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
// int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;
canvas.drawText(text, canvas.getHeight() + 2, canvas.getHeight() + 2, paint);
return bm;
}
那些不是标记。我相信它们已添加到 Google Places 数据库,使用 Places Add API.
我可以建议的是,您可以从地点检索结果,并在添加 Markers
.
时将这些结果用作 title
我对你的情况的处理方法是使用 Dynamic Icons from Google Maps。
它可能已被弃用,但目前我还不知道有更好的选择。有了这个库,你就可以创建带有图标和字母的图钉,我相信正是你想要的。
例如下面的link
https://chart.googleapis.com/chart?chst=d_map_spin&chld=2.1|35|FFFF88|11|_|A|Balloon!
将创建以下图像
图书馆内容丰富,有很多选择,这只是冰山一角。
希望对您有所帮助!
图标有多个来源,Places API 提供了足够的详细信息,因此您可以为每个地点选择正确的图标。不过,我对 Android 并不熟悉,所以我只会略微提及。
今天的情况,我想说诀窍是找到一套可靠的你可以使用的图标集。
- 最安全的选择可能是放置 API 自己的图标。至少在网络服务中,每个地方都是一个指向 URL 的
icon
字段。作为受支持的 API 的一部分,我觉得依赖这些图标最舒服 ─ 除了拥有自己的图标。这是酒吧的:
- 如果您喜欢它们,并且不介意它们有一天会消失而不另行通知,您可以使用 已弃用 图表 API 的 Freestanding Icons。至少这些只会改变一次:从可用到不再可用。不过它们看起来不是特别好:
- 如果您真的想要 Google 地图搜索结果中使用的相同图标,并享受边缘生活,您可以从 Google 地图中破解他们的 URL。当心:这些可能随时更改,因此您可能有一天醒来时没有图标! ─ 而且它可能经常改变,而不是一次。
但是,如果您将 Places API 用于 Android,我可以想象您正在寻找基于 Places API 网络服务的解决方案。
Place 对象似乎没有 getIcon()
或任何类似的东西,因此您需要从 getPlaceTypes()
to icons. I think it'd be reasonable to ask Google to add a getIcon()
method that would do this for you, so I recommend filing a feature request 的输出中为它创建自己的映射。
尝试使用自定义地图标记。您可以创建自己的视图并将该视图设置为标记。
第 1 步:
根据您的需要创建自定义布局文件并对其进行扩充。
View customMarker = LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom_marker, null);
第 2 步:
将自定义视图转换为位图。
public static 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;
}
Bitmap customeViewBitmap = createDrawableFromView(context, customMarker);
第 3 步:使用生成的位图创建标记并将其添加到 google 地图对象。
GoogleMap googleMap = googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.addMarker(new MarkerOptions()
.position(latLng) .icon(BitmapDescriptorFactory.fromBitmap(customeViewBitmap)));
第 4 步:
添加 MarkerClick 侦听器
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//Your logic here
return false;
}
});
经过几天的研究,我完成了这个解决方案。
我们可以将您自己的布局设置为 IconGenerator
View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
TextView numTxt = (TextView) marker.findViewById(R.id.num_txt);
numTxt.setText("Naveen");
final IconGenerator mIconGenerator = new IconGenerator(getApplicationContext());
mIconGenerator.setContentView(marker);
Bitmap icon = mIconGenerator.makeIcon();
customMarker = mMap.addMarker(new MarkerOptions()
.position(markerLatLng)
.title("Title")
.snippet("Description")
.icon(BitmapDescriptorFactory.fromBitmap(icon))
.anchor(0.5f, 1));
当我在 Google 的地图 Android 应用程序中找到最近的餐馆时,餐馆名称默认显示在标记图标附近。 (参考 Google 图片)。
但是在我的应用程序中,当我搜索最近的餐馆时我需要做同样的事情,我只能显示标记图标。 (参考我的申请图片)。
Google 图片:
我的应用图片:
部分解决方案: 在这里我找到了部分解决方案,我们通过使用 canvas 绘制文本来获得它。我通过这些链接使用了下面的代码 here and here 但是 canvas 对我的文本进行了绘图切割。请参阅附图 TextDrawn Image
Marker myLocMarker = map.addMarker(new MarkerOptions()
.position(myLocation)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here"))));
private Bitmap writeTextOnDrawable(int drawableId, String text) {
Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId)
.copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextAlign(Paint.Align.CENTER);
paint.setLinearText(true);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
paint.setTextSize(35);
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
//Calculate the positions
// int xPos = (canvas.getWidth() / 2) - 2; //-2 is for regulating the x position offset
//"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
// int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;
canvas.drawText(text, canvas.getHeight() + 2, canvas.getHeight() + 2, paint);
return bm;
}
那些不是标记。我相信它们已添加到 Google Places 数据库,使用 Places Add API.
我可以建议的是,您可以从地点检索结果,并在添加 Markers
.
title
我对你的情况的处理方法是使用 Dynamic Icons from Google Maps。
它可能已被弃用,但目前我还不知道有更好的选择。有了这个库,你就可以创建带有图标和字母的图钉,我相信正是你想要的。
例如下面的link
https://chart.googleapis.com/chart?chst=d_map_spin&chld=2.1|35|FFFF88|11|_|A|Balloon!
将创建以下图像
图书馆内容丰富,有很多选择,这只是冰山一角。
希望对您有所帮助!
图标有多个来源,Places API 提供了足够的详细信息,因此您可以为每个地点选择正确的图标。不过,我对 Android 并不熟悉,所以我只会略微提及。
今天的情况,我想说诀窍是找到一套可靠的你可以使用的图标集。
- 最安全的选择可能是放置 API 自己的图标。至少在网络服务中,每个地方都是一个指向 URL 的
icon
字段。作为受支持的 API 的一部分,我觉得依赖这些图标最舒服 ─ 除了拥有自己的图标。这是酒吧的:
- 如果您喜欢它们,并且不介意它们有一天会消失而不另行通知,您可以使用 已弃用 图表 API 的 Freestanding Icons。至少这些只会改变一次:从可用到不再可用。不过它们看起来不是特别好:
- 如果您真的想要 Google 地图搜索结果中使用的相同图标,并享受边缘生活,您可以从 Google 地图中破解他们的 URL。当心:这些可能随时更改,因此您可能有一天醒来时没有图标! ─ 而且它可能经常改变,而不是一次。
但是,如果您将 Places API 用于 Android,我可以想象您正在寻找基于 Places API 网络服务的解决方案。
Place 对象似乎没有 getIcon()
或任何类似的东西,因此您需要从 getPlaceTypes()
to icons. I think it'd be reasonable to ask Google to add a getIcon()
method that would do this for you, so I recommend filing a feature request 的输出中为它创建自己的映射。
尝试使用自定义地图标记。您可以创建自己的视图并将该视图设置为标记。
第 1 步: 根据您的需要创建自定义布局文件并对其进行扩充。
View customMarker = LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom_marker, null);
第 2 步: 将自定义视图转换为位图。
public static 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;
}
Bitmap customeViewBitmap = createDrawableFromView(context, customMarker);
第 3 步:使用生成的位图创建标记并将其添加到 google 地图对象。
GoogleMap googleMap = googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.addMarker(new MarkerOptions()
.position(latLng) .icon(BitmapDescriptorFactory.fromBitmap(customeViewBitmap)));
第 4 步: 添加 MarkerClick 侦听器
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//Your logic here
return false;
}
});
经过几天的研究,我完成了这个解决方案。 我们可以将您自己的布局设置为 IconGenerator
View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
TextView numTxt = (TextView) marker.findViewById(R.id.num_txt);
numTxt.setText("Naveen");
final IconGenerator mIconGenerator = new IconGenerator(getApplicationContext());
mIconGenerator.setContentView(marker);
Bitmap icon = mIconGenerator.makeIcon();
customMarker = mMap.addMarker(new MarkerOptions()
.position(markerLatLng)
.title("Title")
.snippet("Description")
.icon(BitmapDescriptorFactory.fromBitmap(icon))
.anchor(0.5f, 1));