在地图上选择一个位置查看Touch

时间:2016-06-27 12:41:11

标签: android osmdroid android-event

在我的应用中,我希望用户通过点击MapView顶部的视图来选择地图上的位置。
这是我的代码:

map_fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal|center_vertical"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <org.osmdroid.views.MapView
            android:id="@+id/mapview"
            tilesource="Mapnik"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <View
        android:id="@+id/view_choose"
        android:layout_width="50dp"
        android:layout_height="70dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:background="@drawable/choose" />

</FrameLayout>

MapFragment.java

import butterknife.Bind;
import butterknife.ButterKnife;
import org.osmdroid.bonuspack.overlays.MapEventsReceiver;
import org.osmdroid.bonuspack.overlays.MapEventsOverlay;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import android.view.MotionEvent;
import android.view.View;
import android.support.v4.app.Fragment;

public class MapFragment extends Fragment {

    public static final String TAG = "MapFragment";

    @Bind(R.id.mapview)
    MapView mapView;
    @Bind(R.id.view_choose)
    View mChooseView;

    boolean isChooseTouched = false;

    public MapFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        mapView.setTileSource(TileSourceFactory.MAPNIK);
        mapView.getOverlays().add(0, new MapEventsOverlay(getActivity(), new MyMapEventsReceiver()));

        mChooseView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                    isChooseTouched = true;
                    int artificialX = (view.getWidth() / 2);
                    int artificialY = (view.getHeight());
                    event.setLocation(artificialX, artificialY);
                    return false;
            }
        });
    }

    class MyMapEventsReceiver implements MapEventsReceiver {

        @Override
        public boolean singleTapConfirmedHelper(GeoPoint geoPoint) {

            if (isChooseTouched) {
                Log.d(TAG,"location selected");
                // some other calculation
                isChooseTouched = false;
                return true;
            } 
            return false;
        }

        @Override
        public boolean longPressHelper(GeoPoint geoPoint) {
            return false;
        }
    }

}

我已在4台设备上测试过此代码 在其中两个中,它有效。和日志打印
 但在其他两个设备中,它不起作用。它只是移动mapview并且不打印日志 关键是我删除这一行:

event.setLocation(artificialX, artificialY);
从代码

,它适用于所有设备 再次,当我将上面的行设置为这样的:

event.setLocation(artificialX, artificialY-30);

它再次起作用。

但是我需要将事件的位置更改为视图的底部,所以我需要这一行。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

好吧我用这个解决了它:

mChooseView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        isChooseTouched = true;
        int artificialX = (view.getWidth() / 2);
        int artificialY = (view.getHeight()) - 5;
        float x = view.getLeft() + artificialX;
        float y = view.getTop() + artificialY;
        event.setLocation(x, y);
        mapView.dispatchTouchEvent(event);
        return true;
    }
});