4

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

我有这个:

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+ 时遇到了很多问题,如果这会破坏某些东西会很糟糕......

4

2 回答 2

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 语句用于更改标记图标。

于 2013-05-08T13:36:27.763 回答
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

于 2013-08-09T08:45:09.187 回答