简体   繁体   English

在Android中的地图上更改自定义标记的颜色

[英]Change color of Custom marker on Map in Android

I'm trying to change the color of markers on Map. 我正在尝试更改地图上标记的颜色。 I want shape of a common marker but with custom colors. 我想要一个普通标记的形状,但使用自定义颜色。 I've tried the following code but it doesn't make any change in color and doesn't give any error as well. 我已经尝试了以下代码,但它没有做任何颜色变化,也没有给出任何错误。 Can you please guide any solution or any alternative approach? 您能指导任何解决方案或任何替代方法吗? Any help would be highly appreciated. 任何帮助将受到高度赞赏。

Code: 码:

Drawable background = context.getResources().getDrawable(R.drawable.pink_map);
    if (background instanceof ShapeDrawable) {
        ((ShapeDrawable)background).getPaint().setColor(getResources().getColor(R.color.theme_color));
    } else if (background instanceof GradientDrawable) {
        ((GradientDrawable)background).setColor(getResources().getColor(R.color.theme_color));
    }

    // convert drawable to bitmap
    Bitmap bitmap = ((BitmapDrawable) background).getBitmap();

    Marker marker = mMap.addMarker(new MarkerOptions().position(sydney)
                    .title("My Location")
                    .icon(BitmapDescriptorFactory.fromBitmap(bitmap))

This is working for me: 这对我有用:

mMap.addMarker(new MarkerOptions()
                    .position(contactLatLng)
                    .title(contactName)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_blue))
                    );

The important thing is to import the image asset as 'Action Bar and Tab Icons' option. 重要的是将图像资源导入为“操作栏和选项卡图标”选项。 Then you choose the theme CUSTOM and you can select a custom color to apply your image. 然后选择主题CUSTOM,您可以选择自定义颜色来应用图像。 在此输入图像描述

please try this code 请试试这段代码

  BitmapDescriptor bitmapDescriptor
            = BitmapDescriptorFactory.fromResource(
            (int) BitmapDescriptorFactory.HUE_AZURE);

     mMap.addMarker(new MarkerOptions()
            .position(sydney)
            .title("My Location")
            .snippet("and snippet")
            .icon(bitmapDescriptor);

if it doesn't works please check this alternative library from GDC 如果它不起作用,请从GDC检查这个替代库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM