拖动ImageView边界时删除地图标记

时间:2013-07-04 00:47:45

标签: android android-maps-v2

我正在使用谷歌地图api v2。我将标记放在地图上长按,我想在拖动我在地图左侧设置的android“ic_delete”可绘制边界时删除它们(因为它是在我们删除启动器中的应用程序时完成的)。救命 !

这是我的代码:

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    markers = new ArrayList<Marker>();
    boolean mReturn = false;
    if (map != null) {
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mapSettings = map.getUiSettings();
        mapSettings.setCompassEnabled(true);
        mapSettings.setRotateGesturesEnabled(true);
        mapSettings.setZoomControlsEnabled(true);
        map.setMyLocationEnabled(true);


        map.setOnMapLongClickListener(this);
        map.setOnMarkerDragListener(this);
        mReturn = true;
    }


    @Override
    public void onMapLongClick(LatLng position) {
    // TODO Auto-generated method stub

    final LatLng point = position;

    Marker marker = map.addMarker(createMarkerOptions("test", "snippet", point, true));
    markers.add(marker);

    // ... 
}

    @Override
public void onMarkerDrag(Marker marker) {
    // TODO Auto-generated method stub

}

@Override
public void onMarkerDragEnd(Marker marker) {
    // TODO Auto-generated method stub
    ImageView deleteMarker = (ImageView) findViewById(R.id.deleteMarker); 
    deleteMarker.setVisibility(View.GONE);

        // I want to delete that "marker" if dragged over the "deleteMarker" imageview

}

@Override
public void onMarkerDragStart(Marker marker) {
    // TODO Auto-generated method stub
    findViewById(R.id.deleteMarker).setVisibility(View.VISIBLE);
}   

2 个答案:

答案 0 :(得分:2)

我认为我对你发布的答案有更好的看法 我试图做同样的事情,我做的方式是在活动的布局中放置一个ImageView。
当您单击标记时,手机会很快振动,如果您将标记拖到垃圾箱图像上,图像将变为红色。当您停止拖动时,如果标记位于垃圾箱顶部,则标记将被删除,否则它将返回其初始位置。
我的代码是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/customMapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.myapp.CustomMapFragment" />

    <ImageButton
        android:id="@+id/ibDeleteMarker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_menu_delete"
        android:layout_marginRight="8dp"
        android:visibility="gone" />
</RelativeLayout>

(将删除图标放在屏幕的中间/右侧)

之后我将我的标记设置为draggable并且我已经覆盖了相应的方法(在设置GoogleMap.OnMarkerDragListener之后)。我拿标记屏幕坐标(实际的x,y而不是lat,lng),然后检查它们是否重叠(以下代码在我的MapFragment中 - 在托管片段的活动中,我做了所需的findViewById等): / p>

//This is in the activity
   deleteMarker = (ImageButton) findViewById(R.id.ibDeleteMarker);

    //This is in the MapFragment
    vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); 
//you need also the permission in AndroidManifest
    @Override
    public void onMarkerDragStart(Marker marker) {
        ((MainActivity) getActivity()).deleteMarker.setVisibility(View.VISIBLE);
        origMarkerPos = marker.getPosition();
        vibrator.vibrate(100);
    }

@Override
public void onMarkerDrag(Marker marker) {
    final ImageView deleteMarker = ((MainActivity) getActivity()).deleteMarker;
    Point markerScreenPosition = getMap().getProjection().toScreenLocation(marker.getPosition());
    if (overlap(markerScreenPosition, deleteMarker)) {
        deleteMarker.setImageResource(R.drawable.ic_menu_delete_red);
    } else {
        deleteMarker.setImageResource(R.drawable.ic_menu_delete);
    }
}

@Override
public void onMarkerDragEnd(Marker marker) {
    final ImageView deleteMarker = ((MainActivity) getActivity()).deleteMarker;
    deleteMarker.setVisibility(View.GONE);
    Point markerScreenPosition = getMap().getProjection().toScreenLocation(marker.getPosition());
    if (overlap(markerScreenPosition, deleteMarker)) {
        marker.remove();
    } else
        marker.setPosition(origMarkerPos);
}

private boolean overlap(Point point, ImageView imgview) {
    int[] imgCoords = new int[2];
    imgview.getLocationOnScreen(imgCoords);
    Log.e(TAG, " ****** Img x:" + imgCoords[0] + " y:" + imgCoords[1] + "    Point x:" + point.x + "  y:" + point.y + " Width:" + imgview.getWidth() + " Height:" + imgview.getHeight());
    boolean overlapX = point.x < imgCoords[0] + imgview.getWidth() && point.x > imgCoords[0] - imgview.getWidth();
    boolean overlapY = point.y < imgCoords[1] + imgview.getHeight() && point.y > imgCoords[1] - imgview.getWidth();
    return overlapX && overlapY;
}

我不知道它是否是最佳解决方案,但它确实有效。

注意:如果你有一个操作栏,y坐标之间可能存在偏移您需要考虑的标记和图像视图。

答案 1 :(得分:0)

问题已部分解决,我试过这个:

    LatLng markerLocation = marker.getPosition();
    LatLng mapCenter = map.getCameraPosition().target;

将删除图标定位在地图中心,我可以测试上述两个对象的纬度和经度属性。

    if((markerLocation.latitude > mapCenter.latitude - MINLAT  && markerLocation.longitude > mapCenter.longitude - MINLNG) 
            && (markerLocation.latitude < mapCenter.latitude + MAXLAT && markerLocation.longitude < mapCenter.longitude + MAXLNG)){
       marker.remove();
    }

编辑:

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/map"
    android:name="sn.gotech.trafficjammeu.TransparentSupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true"/>

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_delete_marker"
    android:layout_centerInParent="true"
    android:contentDescription="@string/app_name"
    android:visibility="gone"
    android:padding="10dp"
    android:id="@+id/deleteMarker"
    />

OnMarkerDragListener重写方法:

@Override
public void onMarkerDragStart(Marker marker) {
    // TODO Auto-generated method stub
    deleteMarker.setVisibility(View.VISIBLE);
    // ....
}

@Override
public void onMarkerDragEnd(Marker marker) {
    // TODO Auto-generated method stub
    LatLng markerLocation = marker.getPosition();
    LatLng mapCenter = map.getCameraPosition().target;

    float[] results = new float[1];
    Location.distanceBetween(markerLocation.latitude,
            markerLocation.longitude, mapCenter.latitude,
            mapCenter.longitude, results);
    if (results[0] / 1000 < RADIUS) // the RADIUS depends on Camera Zoom
    {
        // ... remove marker here
    }
    deleteMarker.setVisibility(View.GONE);
}