Select to view content in your preferred language

Guidance required for drag and drop a pin in appropriate location in ArcGIS map - Android OS

702
1
11-04-2022 01:20 AM
MohanNarasimman
New Contributor

Hi Team,

We are using ArcGIS map in Android OS. We were able to plot a pin on map. But unable to drag and place it on the appropriate map location. We need guidance for Drag and Move.

Your help is much needed to sort out this feature.

 

Thanks,
Mohan Narasimman

 

0 Kudos
1 Reply
Shubham_Sharma
Esri Contributor

To move the location of a graphic on the map, use the following steps:

  1. Implement the "mapView.onTouchListener" to listen for "onDoubleTouchDrag" events
  2. Get the "screenPoint" and the "mapPoint" of the event
  3. Determine if the motion event is press, drag, release
    1. Press: Find the graphic that is location at the "screenPoint", and keep the instance of the graphic
    2. Drag: Update its location by adding the graphic to the dragged location
    3. Release: Remove the instance of the tracked graphic

 

Below is an example of a "PictureMarkerSymbol" that is placed on the map on a single tap, and can be dragged to a new location on a double-tap drag. 

 

mapView.onTouchListener =
    object : DefaultMapViewOnTouchListener(this@MainActivity, mapView) {
        // drag a graphic to a new point on map
        override fun onDoubleTouchDrag(e: MotionEvent): Boolean {
            // identify the pixel at the given screen point
            val screenPoint = android.graphics.Point(e.x.roundToInt(), e.y.roundToInt())
            // create a map point from the screen point
            val mapPoint: Point = mapView.screenToLocation(screenPoint)
            // find the map view interaction type
            when (e.action) {
                // user selected the graphic
                MotionEvent.ACTION_DOWN -> {
                    // from the graphics overlay, get the graphics near the tapped location
                    val identifyResultsFuture: ListenableFuture<IdentifyGraphicsOverlayResult> =
                        mapView.identifyGraphicsOverlayAsync(
                            graphicsOverlay,
                            screenPoint,
                            10.0,
                            false
                        )
                    identifyResultsFuture.addDoneListener {
                        // identify the selected graphic
                        val identifyGraphicsOverlayResult: IdentifyGraphicsOverlayResult =
                            identifyResultsFuture.get()
                        val graphics = identifyGraphicsOverlayResult.graphics
                        if (graphics.isEmpty()) {
                            showError("No graphic at point")
                        } else {
                            // clear the current graphic position
                            graphicsOverlay.graphics.clear()
                            // get the first graphic identified
                            identifiedGraphic = graphics[0]
                        }
                    }
                }
                // user is moving the graphic
                MotionEvent.ACTION_MOVE -> {
                    // clear the current graphic position
                    graphicsOverlay.graphics.clear()
                    val graphic = Graphic(mapPoint, pictureMarkerFromUri)
                    graphicsOverlay.graphics?.add(graphic)
                }
                // user released the graphic
                MotionEvent.ACTION_UP -> {
                    // release selected graphic
                    identifiedGraphic = null
                }
            }
            return true
        }

        // place a new graphic on map
        override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
            // clear the current graphic position
            graphicsOverlay.graphics.clear()
            // create a screen point from where the user tapped
            val screenPoint = android.graphics.Point(e.x.roundToInt(), e.y.roundToInt())
            // create a map point from the screen point
            val mapPoint: Point = mapView.screenToLocation(screenPoint)
            // create graphic with the location and symbol and add it to the graphics overlay
            val graphic = Graphic(mapPoint, pictureMarkerFromUri)
            graphicsOverlay.graphics?.add(graphic)
            return true
        }
    }

 

0 Kudos