4

我試圖更改標記的顏色。更改Google地圖V2中的標記顏色

我有這樣的:

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+如果這打破了東西,將吸...

回答

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語句用於更改標記圖標。

+0

我需要得到圖標某種程度上像在我的代碼位置/標題/段工作,用getString。 – Edalol 2013-05-08 13:53:24

+0

但圖標的圖像不是字符串,而是資源。所以你應該使用BitmapDescriptorFactory.fromResource方法得到它。 – 2013-05-08 13:59:01

+0

對不起,真的很慢,但我將如何實現在我目前的代碼在這裏? 爲標題和片段我使用.title(getString(title)) 我會用什麼.icon? – Edalol 2013-05-08 14:02:21

4

下面的代碼片段的伎倆,我沒有依賴關係:

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

+0

謝謝你,這是完美的誰是懶惰的,並希望使用默認的標記,改變顏色只需調用marker.setIcon(bitmapDescriptor); :d – 2017-04-17 06:20:36