dji-sdk / Mobile-SDK-Android

DJI Mobile SDK for Android: http://developer.dji.com/mobile-sdk/
Other
996 stars 581 forks source link

How do I delete markers? #1308

Open CaioZen opened 5 months ago

CaioZen commented 5 months ago

Hello, I am creating a waypointv3 application and I wonder how can I delete the waypoint markers the application draws me. The code I got mostly from the sample app.

` fun markWaypoints() { // version参数实际未用到 //Lista de waypoints val parseInfo = JNIWPMZManager.getWaylines("1.0.0", curMissionPath) //Lista de waylines var waylines = parseInfo.waylines waylines.forEach() { waypoints.addAll(it.waypoints) markLine(it.waypoints) } waypoints.forEach() { markWaypoint(DJILatLng(it.location.latitude, it.location.longitude), it.waypointIndex) }

    val firstWaypoint = waypoints.first()
    val djilatlng1 = DJILatLng(firstWaypoint.location.latitude, firstWaypoint.location.longitude)
    val djilatlng2 = DJILatLng(firstWaypoint.location.latitude, firstWaypoint.location.longitude)
    val localWaypoint = DJILatLngBounds(djilatlng1, djilatlng2)
    map_widget.map?.animateCamera(DJICameraUpdateFactory.newLatLngBounds(localWaypoint, 0))
}

fun clearWaypoints() {
    // Limpa a lista de waypoints
    waypoints.clear()

    // Remove todos os marcadores e linhas do mapa
    //removeAllLines()
}

fun removeAllLines() {
    //deletes all markers, even the ones I dont want to
    map_widget.map?.clear()

}

fun markWaypoint(latlong: DJILatLng, waypointIndex: Int) : DJIMarker?{
    var markOptions = DJIMarkerOptions()
    markOptions.position(latlong)
    markOptions.icon(getMarkerRes(waypointIndex, 0f))
    markOptions.title(waypointIndex.toString())
    markOptions.isInfoWindowEnable = true
    //map_widget.map?.animateCamera(CameraUpdateFactory.newLatLngZoom(markOptions, 15),2000, null);
    return map_widget.map?.addMarker(markOptions)
}

fun markLine(waypoints: List<WaylineExecuteWaypoint>) {

    var djiwaypoints = waypoints.filter {
        true
    }.map {
        DJILatLng(it.location.latitude, it.location.longitude)
    }
    var lineOptions = DJIPolylineOptions()
    lineOptions.width(5f)
    lineOptions.color(Color.GREEN)
    lineOptions.addAll(djiwaypoints)
    map_widget.map?.addPolyline(lineOptions)
}

/**
 * Convert view to bitmap
 * Notice: recycle the bitmap after use
 */
fun getMarkerBitmap(
    index: Int,
    rotation: Float,
): Bitmap? {
    // create View for marker
    @SuppressLint("InflateParams") val markerView: View =
        LayoutInflater.from(activity)
            .inflate(R.layout.activity_waypoint_marker_style_layout, null)
    val markerBg = markerView.findViewById<ImageView>(R.id.image_content)
    val markerTv = markerView.findViewById<TextView>(R.id.image_text)
    markerTv.text = index.toString()
    markerTv.setTextColor(AndUtil.getResColor(R.color.uxsdk_blue))
    markerTv.textSize =
        AndUtil.getDimension(R.dimen.mission_waypoint_index_text_large_size)

    markerBg.setImageResource(R.mipmap.mission_edit_waypoint_normal)

    markerBg.rotation = rotation
    // convert view to bitmap
    markerView.destroyDrawingCache()
    markerView.measure(
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
    )
    markerView.layout(0, 0, markerView.measuredWidth, markerView.measuredHeight)
    markerView.isDrawingCacheEnabled = true
    return markerView.getDrawingCache(true)
}

private fun getMarkerRes(
    index: Int,
    rotation: Float,
): DJIBitmapDescriptor? {
    return DJIBitmapDescriptorFactory.fromBitmap(
        getMarkerBitmap(index , rotation)
    )
}

fun showWaypoints(){
    var loction2D = showWaypoints.last().waylineWaypoint.location
    val waypoint =  DJILatLng(loction2D.latitude , loction2D.longitude)
    var pointMarker =  markWaypoint(waypoint , getCurWaypointIndex())
    pointMarkers.add(pointMarker)
}

fun getCurWaypointIndex():Int{
    if (showWaypoints.size <= 0) {
        return 0
    }
    return showWaypoints.size
}`
dji-dev commented 5 months ago

Agent comment from yating.liao in Zendesk ticket #108922:

If you have the marker you want to delete, you can use marker.remove to clear it. Please note that this removal interface is from the map library.

°°°