如何在android中使用mapfragment绑定触摸事件

时间:2015-10-25 13:42:45

标签: android google-maps

我想检测两种情况  1.当用户点击谷歌地图时  2.当用户从地图中释放该点击时  它就像我们用于其他视图的touchevent,如TextView,Linearlayout等...... -谢谢 enter image description here

2 个答案:

答案 0 :(得分:1)

您可以在MapFragment上创建某种叠加层,并为其设置onTouchListener

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

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <View
        android:id="@+id/mapOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

Java代码:

View mapOverlay = (View) findViewById(R.id.mapOverlay);        
mapOverlay.setOnTouchListener(new View.OnTouchListener() {
    @Override public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // handle tap
                break;
            case MotionEvent.ACTION_UP:
                // handlerelease
                break;
        }

        return false; // return false if you want propagate click to map
    }
});

答案 1 :(得分:0)

对于第一种情况,请使用this。为了使它工作是非常自我解释,但这是一个方便的例子。

gMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {
            // TODO Auto-generated method stub
            Log.d("LatLng", arg0.latitude + "-" + arg0.longitude);
        }
    });