将地图标记设置为自定义颜色Android

问题描述 投票:8回答:2

我正在创建一个应用程序,在某些点添加地图的图钉。我希望我的针脚的颜色与我们的应用程序的主题颜色相匹配。对不起,我真的是个菜鸟

int color = Color.rgb(255, 201, 14);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
final LatLng PERTH = new LatLng(40, -80);
Marker perth = mMap.addMarker(new MarkerOptions()
  .position(PERTH)
  .title("MY PIN")
  .snippet("MAGGIE EATS SNAKE SKINS")
  .draggable(true)
  .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin))
  .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.color)));

.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.color)));不起作用。它不允许我在这里插入自定义颜色。我怎样才能做到这一点?谢谢:)

android colors android-maps-v2 markers
2个回答
6
投票

defaultMarker()方法允许设置自定义颜色,但只能提供色调值。根据android文档:

(色调)值必须大于或等于0且小于360

如果您知道应用主题的十六进制或RGB值,则需要进行一些计算(请参阅example)或仅使用一些free online converter。在你的情况下,色调值将是47。

此外,您无需在代码中设置两次.icon()属性。


0
投票

我创建了这个简单的方法来获取任何颜色的标记。

 public BitmapDescriptor getMarkerIcon(int color) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    return BitmapDescriptorFactory.defaultMarker(hsv[0]);
}
© www.soinside.com 2019 - 2024. All rights reserved.