在Google Maps V2中更改标记颜色

时间:2013-05-08 13:27:42

标签: java android google-maps google-maps-markers google-maps-android-api-2

我正在尝试更改标记的颜色。

我有这个:

private void addMarker(GoogleMap map, double lat, double lon,
                         int title, int snippet) {
    map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
                                     .title(getString(title))
                                     .snippet(getString(snippet)));

然后再添加一个标记:

addMarker(map, 40.748963847316034, -73.96807193756104,
                R.string.title, R.string.snippet);

我想改变标记的颜色,我觉得它很容易,只需像这样实现:

private void addMarker(GoogleMap map, double lat, double lon,
                         int title, int snippet, int icon) {
    map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
                                     .title(getString(title))
                                     .snippet(getString(snippet))
                                         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.(getString(icon)));

addMarker(map, 40.748963847316034, -73.96807193756104,
                R.string.title, R.string.snippet, HUE_AZURE);

但我显然不能将“getString”与“.icon”一起使用。

我该怎么做?

另外,API 8+支持的改变颜色的方法是什么?我在支持API 8+时遇到了很多问题,如果这样做会破坏它会很糟糕......

2 个答案:

答案 0 :(得分:5)

这是一个循环,我在地图上设置了不同颜色的标记,它对我很有用:

for (Task tempTask : TasksListAppObj.getInstance().tasksRepository.getTasksRepository())
                {
                    LatLng latlng = new LatLng(tempTask.getLatitude(), tempTask.getLongtitude());
                    if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_WAITING))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_blue)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_IN_PROGRESS))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_bordo)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_ON_THE_WAY))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_turkiz)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_COMPLETE))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_orange)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_FAILED))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_purpul)));
                    }
}

看看它是否对你有帮助。

if语句用于更改标记图标。

答案 1 :(得分:5)

以下代码片段为我提供了没有依赖关系的技巧:

BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE);

myMap.addMarker(new MarkerOptions()
    .position(point)
    .icon(bitmapDescriptor)
    .title(point.toString()));

在此处找到:http://android-er.blogspot.de/2013/01/change-marker-color-of-googlemaps-v2.html