【问题标题】:Change marker color in Google Maps V2在 Google Maps V2 中更改标记颜色
【发布时间】:2013-05-02 18:16:17
【问题描述】:

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

我有这个:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我需要某种方式让图标像我的代码中的位置/标题/sn-p 一样使用 getString。
    • 但是图标的图像不是字符串而是资源。所以你应该使用 BitmapDescriptorFactory.fromResource 方法来获取它。
    • 对不起,速度真的很慢,但我将如何在我当前的代码中实现它?对于标题和 sn-p,我使用 .title(getString(title)) 对 .icon 使用什么?
    • 如果“int icon”是你图标的资源id,那么你应该这样做:BitmapDescriptorFactory.fromResource(icon)
    • 好的,.icon(BitmapDescriptorFactory.fromResource(icon)) 似乎工作正常,但我现在应该在“addMarker”中添加什么?对于标题和片段,我写 R.string.title/sn-p,如果我想要 HUE_AZURE,我应该放什么?非常感谢您的帮助:)
    【解决方案2】:

    以下 sn-p 对我来说没有依赖关系:

    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

    【讨论】:

    • 谢谢你,这对于那些懒惰并想使用默认标记的人来说是完美的,改变颜色只需调用 marker.setIcon(bitmapDescriptor); :D
    猜你喜欢
    • 2021-09-22
    • 2013-08-19
    • 2013-05-11
    • 2012-12-29
    • 2013-01-28
    • 2021-09-27
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    相关资源
    最近更新 更多