带按钮的斯科布勒注释



我正在创建一个Skobbler注释,如下所示:

markerCoords = mapView.pointToCoordinate(skScreenPoint); //gives us the coordinate
SKAnnotation annotation = new SKAnnotation(11);
SKAnnotationView view = new SKAnnotationView();
View v = getLayoutInflater().inflate(R.layout.annotation_marker, null, false);
v.findViewById(R.id.btn_destination).setOnClickListener(destListener);
v.findViewById(R.id.btn_origin).setOnClickListener(originListener);
view.setView(v);
annotation.setAnnotationView(view);
annotation.setLocation(mapView.pointToCoordinate(skScreenPoint));
annotation.setMininumZoomLevel(1);
mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_POP_OUT);

视图R.layout.annotation_marker包含几个按钮,但我无法点击/单击它们。我的单击会浏览注释并点击地图(我已经检测到它)。当我膨胀视图时,我尝试在视图上使用requestFocus(),但这没有任何效果。我在 xml 中也有android:clickable='true'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="200dp"
    android:layout_height="110dp"
    android:layout_gravity="center_horizontal">
    <Button
         style="?android:attr/buttonStyleSmall"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Origin"
         android:id="@+id/btn_origin"
         android:layout_gravity="right"
         android:focusable="true"
         android:clickable="true"/>
    <Button
         style="?android:attr/buttonStyleSmall"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:clickable="true"
         android:focusable="true"
         android:text="Destination"
         android:id="@+id/btn_destination" />
</LinearLayout>

如何让点击事件点击按钮而不是基础地图?

由于 onClick 事件不会发生,为了在地图上的不同视图上启用事件的解决方案是使用 annotationViews 和对 onAnnotationSelected 回调的执行操作将它们合并到不同的注释中(因此每个注释应该有一个视图)。

为了以自然的方式触发事件,必须为注释设置相应的偏移量,以便在单击实际视图时触发onAnnotationSelected回调,而不是注释的扩展。

@Override
public void onSurfaceCreated(SKMapViewHolder mapHolder) { 
…. 
SKAnnotation annotation = new SKAnnotation(137);
SKAnnotationView annotationView = new SKAnnotationView();
View v = getLayoutInflater().inflate(R.layout.bug_2_btn_layout,null,false);
annotationView.setView(v);
annotation.setAnnotationView(annotationView);
annotation.setLocation(new SKCoordinate(52.520008,13.404954));
annotation.setMininumZoomLevel(1); 

double viewToLayoutRatio = getResources().getDimension(R.dimen.originBtn_width)/getResources().getDimension(R.dimen.annotationLayout_width);
annotation.setOffset(new SKScreenPoint((float) (annotation.getOffset().getX()-(viewToLayoutRatio*getResources().getDimension(R.dimen.originBtn_width))), (float) (annotation.getOffset().getY()+(viewToLayoutRatio/3)*getResources().getDimension(R.dimen.originBtn_width)))); 
mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_POP_OUT); 
… 
} 
@Override
public void onAnnotationSelected(final SKAnnotation annotation) { 
switch (annotation.getUniqueID()) { 
… 
case 137:
 Toast.makeText(getApplicationContext(),"Annotation was clicked",Toast.LENGTH_SHORT);
 View v = getLayoutInflater().inflate(R.layout.bug_2_btn_layout,null,false);
 originClicked(v);
 break;
 }
} 

最新更新