Android Google Map V2:点击另一个标记时如何更改之前点击的标记图标 [英] Android Google Map V2: How to change previous clicked marker's icon when clicked on another marker

查看:71
本文介绍了Android Google Map V2:点击另一个标记时如何更改之前点击的标记图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我已经通过添加previousMarker对象解决了性能问题。因此只有以前点击过的标记将被删除并替换为默认图标。然而,当我点击标记时信息窗口仍然不显示。






我有地图视图并设置了一些标记在上面。我想要的是当我点击一个标记时,它将它的图标更改为不同的图标,而当我点击另一个标记时,上一个标记的图标应该更改为原始图标。



我所做的就是这样,但只要点击标记就可以更改标记图标。

  @Override 
public boolean onMarkerClick(Marker marker){//当点击或点击标记时调用。

LatLng markerPos = marker.getPosition();
String markerLocationName = marker.getTitle();
String markerSubCategoryName = marker.getSnippet();

marker.remove();

MarkerOptions markerOptions =
新MarkerOptions()。position(markerPos)
.title(markerLocationName)
.snippet(markerSubCategoryName)
.icon(BitmapDescriptorFactory .fromResource(R.drawable.new_icon)); //更改标记图标
mMap.addMarker(markerOptions);
Log.d(marker,更改标记图标); //可以在这里打开对话窗口
返回false;

$ / code>

所以如果我点击2个标记,我会得到2个新图标,同时我想要的只是当前点击的标记更改其图标。



所以我也通过添加2行代码来完成这样的事情。

  @Override 
public boolean onMarkerClick(标记标记){//当点击或点击标记时调用。

mMap.clear();
populateAllMarkersOnMap(); //在地图上重新生成标记

LatLng markerPos = marker.getPosition();
String markerLocationName = marker.getTitle();
String markerSubCategoryName = marker.getSnippet();

marker.remove(); //删除当前点击的标记

MarkerOptions markerOptions =
new MarkerOptions()。position(markerPos)
.title(markerLocationName)
.snippet(markerSubCategoryName)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon)); //更改标记图标
mMap.addMarker(markerOptions); //用新图标添加标记到地图
返回false;
}

缺点是1 /它禁用信息窗口也以第一种方式发生)。 2 /清除地图上的所有标记并再次设置所有标记。想象一下,我有100个标记,应该是每次点击时的性能问题?



populateAllMarkersOnMap()现在可以像这样简单: p>

  private void populateAllMarkersOnMap(){
setMarker(latA1,lonA1,A1,A1.1);
setMarker(latA2,lonA2,A2,A2.1);
// ...(100次或通过循环填充)
};

那么有没有办法让我以前点击的标记将它的图标变回默认值新标记?为我的英语道歉,如果你认为我应该为我的问题换个标题,请帮助。

最简单的方法。我创建了一个previousMarker对象并存储了当前点击的标记:

  @Override 
public boolean onMarkerClick(Marker marker){ //当点击或点击标记时调用。
if(previousMarker!= null){
previousMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.dot_icon));
}
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ct_icon));
previousMarker = marker; //现在点击标记变成previousMarker
return false;
}


UPDATE: I have solved the performance issue by adding a previousMarker object. So only the previous clicked marker will be remove and replaced with the default icon. However the info window is still not shown when I click on the marker.


I have a map view and set some markers on it. What I want is when I clicked on a marker, it changes its icon to be a different icon, and when I click on another marker, the previous marker's icon should change to its original one.

What I've done is something like this but it just simply changes the marker icon whenever I click on the marker.

@Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.

    LatLng markerPos=marker.getPosition();
    String markerLocationName=marker.getTitle();
    String markerSubCategoryName=marker.getSnippet();

    marker.remove();

    MarkerOptions markerOptions =
            new MarkerOptions().position(markerPos)
                    .title(markerLocationName)
                    .snippet(markerSubCategoryName)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon
    mMap.addMarker(markerOptions);
    Log.d("marker","change marker icon"); // can open a dialog window here
    return false;
}

So if I click 2 markers, I will get 2 new icons appears, meanwhile what I want is only the current clicked marker changes its icon.

So I've also done something like this by adding 2 more lines of code. It succeeds doing what I want but it has some drawback (see below).

@Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.

    mMap.clear();
    populateAllMarkersOnMap();//repopulate markers on map

    LatLng markerPos=marker.getPosition();
    String markerLocationName=marker.getTitle();
    String markerSubCategoryName=marker.getSnippet();

    marker.remove(); //remove the current clicked marker

    MarkerOptions markerOptions =
            new MarkerOptions().position(markerPos)
                    .title(markerLocationName)
                    .snippet(markerSubCategoryName)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon
    mMap.addMarker(markerOptions); //add marker with new icon into map
    return false;
}

The drawback is 1/ it "disable" the info window (the same thing also happen in the first way). 2/ it clear all the markers on map and set all the markers again. Imagine I have 100 markers, should that be a performance problem on every click I do?

The populateAllMarkersOnMap() can be something as simple like this at the moment:

private void populateAllMarkersOnMap(){
    setMarker(latA1, lonA1, "A1","A1.1"); 
    setMarker(latA2, lonA2, "A2","A2.1"); 
    // ... (100 times or populated via a loop) 
};

So is there a way to get previous clicked marker to change its icon back to default when I click a new marker? Apologise for my English, if you think I should put another title for my question, please help.

解决方案

Finally I found the best and most simple way. I made a previousMarker object and store the current clicked marker:

@Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
    if(previousMarker!=null){
        previousMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.dot_icon));
    }
    marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ct_icon));
    previousMarker=marker; //Now the clicked marker becomes previousMarker
    return false;
}

这篇关于Android Google Map V2:点击另一个标记时如何更改之前点击的标记图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆