简体   繁体   English

Android Google Map V2:如何在点击其他标记时更改以前点击的标记图标

[英]Android Google Map V2: How to change previous clicked marker's icon when clicked on another marker

UPDATE: I have solved the performance issue by adding a previousMarker object. 更新:我通过添加previousMarker对象解决了性能问题。 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. 因此,如果我单击2个标记,我将显示2个新图标,同时我想要的只是当前单击的标记更改其图标。

So I've also done something like this by adding 2 more lines of code. 所以我也做了类似的事情,增加了2行代码。 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). 缺点是1 /它“禁用”信息窗口(同样的事情也发生在第一种方式)。 2/ it clear all the markers on map and set all the markers again. 2 /清除地图上的所有标记并再次设置所有标记。 Imagine I have 100 markers, should that be a performance problem on every click I do? 想象一下,我有100个标记,如果每次点击都会出现性能问题吗?

The populateAllMarkersOnMap() can be something as simple like this at the moment: populateAllMarkersOnMap()目前可以像这样简单:

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: 我制作了一个previousMarker对象并存储了当前点击的标记:

@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;
}

你可能正在寻找这种方法

 Called when the marker's info window is closed. 
optional public func mapView(mapView: GMSMapView, didCloseInfoWindowOfMarker marker: GMSMarker)

I found the best and most simple way. 我找到了最好,最简单的方法。 I made another marker object and store the current clicked marker enter code here 我制作了另一个标记对象并存储当前点击的标记,在此输入代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道在 Google Maps v2 for Android 上点击了哪个标记? - How to know which Marker was clicked on Google Maps v2 for Android? 第二次点击标记时隐藏infoWindow(Google Map Android API V2) - Hide infoWindow when clicked marker second time (Google Map Android API V2) 标记图标出现Android Google Map v2错误 - Android google map v2 error at marker's icon Android 谷歌地图 API v2 - 如何更改标记图标 - Android Google Maps API v2 - how to change marker icon 单击特定图标时,我是否可以更改地图标记图标,然后单击另一个图标时以此类推? - Is it possible for me to change a map marker icon when the specific icon is clicked and then to toggle when another one is clicked and so on? Android Google Map v2 - 将标记移动到点击位置并更新地理坐标 - Android Google Map v2 - move marker to clicked position and update geo coordinates 在Android中的Google Map v2中将标记圆形图标 - Round icon to the marker in Google map v2 in android 地图标记图标动画(适用于Android的Google Maps API V2) - Map Marker icon animation (Google Maps API V2 for Android) 使标记带有自定义信息,以供单击时使用(Android,Google Maps v2) - Making a marker carry custom info to be used when clicked (Android, Google Maps v2) 如何在Google Map API V2上旋转标记图标? - How to rotate marker icon on google map api v2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM