如何在谷歌地图上放置"various"点击监听器实例?



示例图像

我在地图上放了很多圈子,我希望这些圈子有自己的 OncircleClick Listener

但问题是,我只能在googleMap上注册一个圈子点击监听器,并且这个监听器由每个圈子共享,所以每次我点击任何圈子时,都会发生相同的监听器事件。

你能告诉我如何让每个圈子都有自己的圈子点击听众吗?谢谢。

这是我尝试过的代码:

private void drawCircle(LatLng point, boolean isOverThreshold, final Bundle results){
GoogleMap.OnCircleClickListener onCircleClickListener = new GoogleMap.OnCircleClickListener() {
@Override
public void onCircleClick(Circle circle) {
int sixty_one = results.getInt("60hz_1");
int eighty_one = results.getInt("180hz_1");
int sixty_two = results.getInt("60hz_2");
int eighty_two = results.getInt("180hz_2");
int sixty_three = results.getInt("60hz_3");
int eighty_three = results.getInt("180hz_3");
double speed = results.getDouble("speed");
Toast.makeText(ReplayActivity.this, "CH1: ("+sixty_one+","+eighty_one+") n"+" CH2: (" + sixty_two+","+eighty_two+") n" + "CH3: ("+sixty_three+","+eighty_three+")n speed:"+speed+"m/s )",Toast.LENGTH_SHORT).show();
Log.d("gd","circle clicked!");
}
};
// Instantiating CircleOptions to draw a circle around the marker
CircleOptions circleOptions = new CircleOptions();
// Specifying the center of the circle
circleOptions.center(point);
(....)

// Adding the circle to the GoogleMap
map.addCircle(circleOptions);
map.setOnCircleClickListener(onCircleClickListener); 
//but this oncircleListener is shared by every circle click event..
}

您可以使用创建 circle 对象Circle.setTag()方法来设置,例如 Circle 对象(或更复杂的结构(的 ID 作为 Tag,然后在您的onCircleClickListener中使用Circle.getTag()来获取此 ID(或更复杂的结构(。像这样:

private void drawCircle(LatLng point, boolean isOverThreshold, final Bundle results){
GoogleMap.OnCircleClickListener onCircleClickListener = new GoogleMap.OnCircleClickListener() {
@Override
public void onCircleClick(Circle circle) {
// get stored Tag object from passed circle object
YOUR_TAG_OBJECT_CLASS yourTagObjectName = (YOUR_TAG_OBJECT_CLASS)circle.getTag();
if (yourTagObjectName != null) {
// parse it
switch (yourTagObjectName.id) {
case ID_1: ... break;
case ID_2: ... break; 
...
}
}
int sixty_one = results.getInt("60hz_1");
int eighty_one = results.getInt("180hz_1");
int sixty_two = results.getInt("60hz_2");
int eighty_two = results.getInt("180hz_2");
int sixty_three = results.getInt("60hz_3");
int eighty_three = results.getInt("180hz_3");
double speed = results.getDouble("speed");
Toast.makeText(ReplayActivity.this, "CH1: ("+sixty_one+","+eighty_one+") n"+" CH2: (" + sixty_two+","+eighty_two+") n" + "CH3: ("+sixty_three+","+eighty_three+")n speed:"+speed+"m/s )",Toast.LENGTH_SHORT).show();
Log.d("gd","circle clicked!");
}
};
// Instantiating CircleOptions to draw a circle around the marker
CircleOptions circleOptions = new CircleOptions();
// Specifying the center of the circle
circleOptions.center(point);
(....)

// Adding the circle to the GoogleMap
Circle circle = map.addCircle(circleOptions);  // <- store circle object
circle.setTag(YOUR_TAG_ID_OR_OBJECT);          // <- set Tag to stored circle object
map.setOnCircleClickListener(onCircleClickListener); 
}

相关内容

  • 没有找到相关文章

最新更新