1

我正在使用谷歌地图 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);
}   
4

2 回答 2

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 等):

//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 坐标和您必须考虑的图像视图之间可能存在偏移。

于 2013-09-10T19:45:23.107 回答
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);
}
于 2013-07-06T00:11:13.107 回答