【发布时间】:2013-03-22 18:15:02
【问题描述】:
我的谷歌地图上有一个 onMarkerClickListener,它会在按下标记时触发。
标记是在一个 Tag 类中创建的,该类在传递的地图上在自身内部创建标记:
地图活动开始时的列表实例:
//tags
List<Tag> tags = new ArrayList<Tag>();
在包含 googleMap 的活动的 onCreate() 中,我将标记添加到列表中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....other code here
//add the markers to the map
tags.add(new Tag(map,1,"City Of Dundee", "Home Of The Jakey", DUNDEE, this.getResources(),R.drawable.ic_launcher));
tags.add(new Tag(map,2,"Some Place","This is some place",LOCATION_Z,this.getResources(),R.drawable.ic_launcher));
....other code here
}//end on create
标签类的构造函数:
public Tag(GoogleMap map, int atagID,String tagTitle, String tagSnippet, LatLng tagPosition, Resources resource, int id){
this.tagID = atagID;
this.position = tagPosition;
this.title = tagTitle;
this.snippet = tagSnippet;
this.icon = BitmapDescriptorFactory.fromResource(id);
this.theTag = map.addMarker(new MarkerOptions()
.position(tagPosition)
.title(tagTitle)
.snippet(tagSnippet)
.icon(icon));
}
这会创建标签并正确显示在地图上
在 onMarkerClickedListener 的侦听器中,我将在地图上单击的标记与列表中的标记进行比较,但 if 语句永远不会通过,即使我比较相同的标题也是如此。
聆听者:
onMarkerClickListener = new OnMarkerClickListener(){
@Override
public boolean onMarkerClick(Marker marker) {
//for loop that goes over array or marker
//if marker is equal to mark in array
//do marker functionality
for(Tag currentTag : tags){
if(currentTag.theTag == marker){
switch(currentTag.tagID){
case 1:
//do something for that button
Toast.makeText(getApplicationContext(), "marker 1", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(getApplicationContext(), "marker 2", Toast.LENGTH_SHORT).show();
return false;
default:
Toast.makeText(getApplicationContext(), "default", Toast.LENGTH_SHORT).show();
return false;
}
}else{
Toast.makeText(getApplicationContext(), "theTag " + currentTag.tagID + ": " + currentTag.theTag.getTitle(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "marker: " + marker.getTitle(), Toast.LENGTH_SHORT).show();
}
}
return false;
}
};
我不知道为什么它从来没有到达 switch 语句任何想法?
【问题讨论】:
标签: android if-statement google-maps-markers google-maps-api-2