<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Geotrigger only works once (when device's position starts inside geofence) in ArcGIS Runtime SDK for Android Questions</title>
    <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227911#M5876</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/633015"&gt;@Hudson_Miears&lt;/a&gt;. Thanks for your answer.&amp;nbsp;&lt;SPAN&gt;GeotriggerMonitor is running in a fragment, this fragment is mounted in a FrameLayout inside main Activity. Should this situation make Geotrigger get backgrounded? (Sorry, I'm not an expert android developer.)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;No background location is needed at this point (maybe in a future version of this app) but , I could try this approach.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 02 Nov 2022 14:47:50 GMT</pubDate>
    <dc:creator>PabloAndrésDiazMazzaro</dc:creator>
    <dc:date>2022-11-02T14:47:50Z</dc:date>
    <item>
      <title>Geotrigger only works once (when device's position starts inside geofence)</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227852#M5872</link>
      <description>&lt;P&gt;Helo there.&lt;/P&gt;&lt;P&gt;I'm working with ArcGIS Runtime for Android (written in Kotlin) and most of basic functions works fine. I can show device position (with high accuracy) and load a WebMap where are two FeatureServices. Those services are providing features for geofences.&lt;/P&gt;&lt;P&gt;First test looks like it was working fine, the device is inside one of the geofences a triggers GeotriggerMonitor notification for ENTER event. Continuing with the test, the device leaves&amp;nbsp; geofence and nothing happen.&lt;/P&gt;&lt;P&gt;Also, I tested the opposite situation, starts out of geofence and then enter but, nothing happen either.&lt;/P&gt;&lt;P&gt;The source is similar to this example:&amp;nbsp;&lt;A title="example" href="https://developers.arcgis.com/android/device-location/work-with-geotriggers/" target="_blank" rel="noopener"&gt;this&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;There're same syntax differences but, the main difference cloud be related with where MapView is placed. It's in a Fragmente instead&amp;nbsp; of in an Activity.&lt;BR /&gt;&lt;BR /&gt;My code:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;GeotriggerMonitor setup:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="kotlin"&gt;private fun setupComplejoGeotriggers(){
        val locationFeed = LocationGeotriggerFeed(mapView.locationDisplay.locationDataSource)
        //Geofences Layer
        complejosLayer?.let { layer -&amp;gt;

            val fenceParameters = FeatureFenceParameters(layer.featureTable)
            val predioGeotrigger = FenceGeotrigger(locationFeed, FenceRuleType.ENTER_OR_EXIT,fenceParameters, null, "EnterComplejo")

            val geotriggerMonitor = GeotriggerMonitor(predioGeotrigger)
            geotriggerMonitor.addGeotriggerMonitorNotificationEventListener(onComplejoEnterEvent)
            geotriggerMonitor.addGeotriggerMonitorWarningChangedEventListener(onGeotriggerEvent)

            val future = geotriggerMonitor.startAsync()
            // check to see if the GeotriggerMonitor failed to start
            future.addDoneListener {
                if (geotriggerMonitor.status == GeotriggerMonitorStatus.FAILED_TO_START) {
                    val message = "GeotriggerMonitor failed to load: " +
                            geotriggerMonitor.warning.message +
                            geotriggerMonitor.warning.additionalMessage
                    Log.e("GEOPDM", message)
                }
            }
            Log.d(TAG, "Complejo dection ready")
        }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Notifications Handler:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="kotlin"&gt;private val onComplejoEnterEvent =
        GeotriggerMonitorNotificationEventListener { complejoEnterEvt -&amp;gt;
            val notifInfo =
                complejoEnterEvt.geotriggerNotificationInfo as FenceGeotriggerNotificationInfo
            val fence = notifInfo.fenceGeoElement
            val truck = notifInfo.feedLocation.position

            val prefdioFeature = fence as ArcGISFeature
            val idLugar = prefdioFeature.attributes["ID_LUGAR"]
            showTost("Destino ${notifInfo.fenceNotificationType} - $idLugar")
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Warning Handler:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="kotlin"&gt;    private val onGeotriggerEvent =
        GeotriggerMonitorWarningChangedEventListener { warnEvt -&amp;gt;
            Log.d(TAG, "Trigger w")

            Log.d(TAG, "Predio $warnEvt")
            Log.d(TAG, "Predio $warnEvt", warnEvt.warning)
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;&lt;P&gt;Hope somebody can help me!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 12:36:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227852#M5872</guid>
      <dc:creator>PabloAndrésDiazMazzaro</dc:creator>
      <dc:date>2022-11-02T12:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: Geotrigger only works once (when device's position starts inside geofence)</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227874#M5873</link>
      <description>&lt;P&gt;Hi - thanks for posting.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking at your code, I think the issue is that you are letting the geotrigger monitor go out of scope here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;val geotriggerMonitor = GeotriggerMonitor(predioGeotrigger)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I suspect the monitor is getting garbage collected and then stops sending notifications because it is at local scope. Can you try changing your code to persist the geotrigger monitor? You need to hang on to it for as long as you want to monitor conditions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope that helps,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Luke&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 13:25:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227874#M5873</guid>
      <dc:creator>LukeSmallwood</dc:creator>
      <dc:date>2022-11-02T13:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Geotrigger only works once (when device's position starts inside geofence)</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227875#M5874</link>
      <description>&lt;P&gt;Hi Luke, thank you for your answer.&lt;/P&gt;&lt;P&gt;That idea also came to me and I already tried it(fragment level variable) but, same result sadly.&lt;BR /&gt;&lt;BR /&gt;Also, I tried to create an instance of GeotriggerMonitor in a main dispatcher coroutine scope but, didn't works, same issue (only works first time with device inside geofence).&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 13:40:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227875#M5874</guid>
      <dc:creator>PabloAndrésDiazMazzaro</dc:creator>
      <dc:date>2022-11-02T13:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: Geotrigger only works once (when device's position starts inside geofence)</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227890#M5875</link>
      <description>&lt;P&gt;Hi Pablo! Would love to help you more. Just so I understand, could you explain what happens in your test? Does the activity running the geotrigger get backgrounded during the test?&lt;BR /&gt;&lt;BR /&gt;We actually recommend keeping your GeotriggerMonitor in a Foreground Service if you need to keep it running when the app is in the background. &lt;A href="https://developers.arcgis.com/android/device-location/work-with-geotriggers/#working-with-geotriggers-when-your-app-is-in-the-background" target="_self"&gt;This section&lt;/A&gt; of the docs explains it further.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 14:28:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227890#M5875</guid>
      <dc:creator>Hudson_Miears</dc:creator>
      <dc:date>2022-11-02T14:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Geotrigger only works once (when device's position starts inside geofence)</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227911#M5876</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/633015"&gt;@Hudson_Miears&lt;/a&gt;. Thanks for your answer.&amp;nbsp;&lt;SPAN&gt;GeotriggerMonitor is running in a fragment, this fragment is mounted in a FrameLayout inside main Activity. Should this situation make Geotrigger get backgrounded? (Sorry, I'm not an expert android developer.)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;No background location is needed at this point (maybe in a future version of this app) but , I could try this approach.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 14:47:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227911#M5876</guid>
      <dc:creator>PabloAndrésDiazMazzaro</dc:creator>
      <dc:date>2022-11-02T14:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: Geotrigger only works once (when device's position starts inside geofence)</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227946#M5878</link>
      <description>&lt;P data-unlink="true"&gt;If background location is not needed at this point, it would be helpful to understand a few more things. If you plug directly into your LocationDataSource, are you receiving LocationChangedEvents as expected?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 15:44:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227946#M5878</guid>
      <dc:creator>Hudson_Miears</dc:creator>
      <dc:date>2022-11-02T15:44:06Z</dc:date>
    </item>
    <item>
      <title>Re: Geotrigger only works once (when device's position starts inside geofence)</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227959#M5879</link>
      <description>&lt;P&gt;Yes. I recibe position, velocity and course correctly.&lt;/P&gt;&lt;LI-CODE lang="kotlin"&gt;        var criteria = Criteria()
        criteria.powerRequirement = Criteria.POWER_HIGH
        criteria.accuracy = Criteria.ACCURACY_FINE
        criteria.isSpeedRequired = true
        criteria.isAltitudeRequired = false
        criteria.isBearingRequired = true
        criteria.isCostAllowed = true

        locationDataSource = AndroidLocationDataSource(context, criteria, 1000, 2.0F)
        locationDataSource.addLocationChangedListener(LocationDataSource.LocationChangedListener {
            Log.d(TAG, "Pos: ${it.location?.position}")
            setVelocidadToVelocidadView(it.location.velocity.toInt(), MainActivity.viewModel?.maxVelocidad)
            tv_debug.text = "Pos: ${it.location?.position}\n${it.location.velocity}\n" +
                    " ${it.location.course}"
        })&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 02 Nov 2022 15:56:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1227959#M5879</guid>
      <dc:creator>PabloAndrésDiazMazzaro</dc:creator>
      <dc:date>2022-11-02T15:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: Geotrigger only works once (when device's position starts inside geofence)</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1228016#M5880</link>
      <description>&lt;P&gt;Finally, It's works. Thanks you guys. I have used a combination of both answers. First off all, I defined a GeotriggerMonitor as a fragmente level variable and then, I moved all Monitor setup code inside of a coroutine main scope.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="kotlin"&gt;class MapFragment : Fragment(), TokenizedFragment, PatenteComponent {
    /******** Internals ********/
    private var geotriggerMonitorPredios : GeotriggerMonitor?=null
    private var prediosLayer: FeatureLayer? = null
    /******** Fragment Lifecycle ********/

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_map, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // Map
        mapView = view.findViewById&amp;lt;MapView&amp;gt;(R.id.mapView)

        tv_debug = view.findViewById&amp;lt;TextView&amp;gt;(R.id.tv_debug)

        /***** Live location *****/
        var criteria = Criteria()
        criteria.powerRequirement = Criteria.POWER_HIGH
        criteria.accuracy = Criteria.ACCURACY_FINE
        criteria.isSpeedRequired = true
        criteria.isAltitudeRequired = false
        criteria.isBearingRequired = true
        criteria.isCostAllowed = true

        locationDataSource = AndroidLocationDataSource(context, criteria, 1000, 1.0F)
        locationDataSource.addLocationChangedListener(LocationDataSource.LocationChangedListener {
            Log.d("GEOPDM", "Pos: ${it.location?.position}")
            var speed = it.location?.velocity?.times(3.6)
            if(speed == null){
                speed = 0.0
            }
            tv_debug.text = "Pos: ${it.location?.position}\n${it.location.velocity}\n" +
                    " ${it.location.course}"
        })
        mapView.locationDisplay.addDataSourceStatusChangedListener {
            // if LocationDisplay isn't started or has an error
            if (!it.isStarted &amp;amp;&amp;amp; it.error != null) {
                // check permissions to see if failure may be due to lack of permissions
                requestPermissions(it)
            }
        }

        mapView.locationDisplay.locationDataSource = locationDataSource
        //mapView.locationDisplay.locationDataSource = sim

        mapView.locationDisplay.autoPanMode = LocationDisplay.AutoPanMode.COMPASS_NAVIGATION

        initOnlineMap()
    }

    private fun initOnlineMap(){        
        val portal = Portal(Copiloto.PORTAL_URL, false)
        val portalItem = PortalItem(portal, Copiloto.MAP_ITEMID)
    
        mapView.apply {
            map = ArcGISMap(portalItem)
        }
    
        mapView.map?.addDoneLoadingListener {
            Log.d("PDM", "[MapFragment] Map loaded")
    
            initUI()
        }   
    }

    private fun initUI(){
        setOwnPositionSymbol()
        MainActivity.viewModel?.fences?.let { fences -&amp;gt;
            filterPredios(fences)
            val ioScope = CoroutineScope(Dispatchers.Main)
            ioScope.launch {
                setupPredioGeotriggers()
            }
        }
    }

     /******** Map - Geotriggers ********/
     private val onPredioEnterEvent =
     GeotriggerMonitorNotificationEventListener { predioEnterEvt -&amp;gt;
         val notifInfo =
             predioEnterEvt.geotriggerNotificationInfo as FenceGeotriggerNotificationInfo
         val fence = notifInfo.fenceGeoElement
         val truck = notifInfo.feedLocation.position

         val prefdioFeature = fence as ArcGISFeature
         val idLugar = prefdioFeature.attributes["ID_LUGAR"]
         showTost("Origen ${notifInfo.fenceNotificationType}")
     }

     private val onGeotriggerEvent =
     GeotriggerMonitorWarningChangedEventListener { warnEvt -&amp;gt;
         Log.d("GEOPDM", "Trigger w")

         Log.d("GEOPDM", "Predio $warnEvt")
         Log.d("GEOPDM", "Predio $warnEvt", warnEvt.warning)
     }

    private fun setupPredioGeotriggers(){
        val ioScope = CoroutineScope(Dispatchers.Main)
        ioScope.launch {
            //val locationFeed = LocationGeotriggerFeed(mapView.locationDisplay.locationDataSource)
            var criteria = Criteria()
            criteria.powerRequirement = Criteria.POWER_HIGH
            criteria.accuracy = Criteria.ACCURACY_FINE
            criteria.isSpeedRequired = true
            criteria.isAltitudeRequired = false
            criteria.isBearingRequired = true
            criteria.isCostAllowed = true
            val locationDs = AndroidLocationDataSource(context, criteria, 1000, 1.0F)
            locationDs.startAsync()
            val locationFeed = LocationGeotriggerFeed(locationDs)
            //mapView.locationDisplay.locationDataSource.startAsync()
            prediosLayer?.let { layer -&amp;gt;

                val fenceParameters = FeatureFenceParameters(layer.featureTable)
                val predioGeotrigger = FenceGeotrigger(locationFeed, FenceRuleType.ENTER_OR_EXIT,fenceParameters, ArcadeExpression("\$fenceFeature.ID_LUGAR"), "EnterPredio")

                geotriggerMonitorPredios = GeotriggerMonitor(predioGeotrigger)
                geotriggerMonitorPredios!!.addGeotriggerMonitorNotificationEventListener(onPredioEnterEvent)
                geotriggerMonitorPredios!!.addGeotriggerMonitorWarningChangedEventListener(onGeotriggerEvent)

                val future = geotriggerMonitorPredios!!.startAsync()
                // check to see if the GeotriggerMonitor failed to start
                future.addDoneListener {
                    if (geotriggerMonitorPredios!!.status == GeotriggerMonitorStatus.FAILED_TO_START) {
                        val message = "GeotriggerMonitor Predios failed to load: " +
                                geotriggerMonitorPredios!!.warning.message +
                                geotriggerMonitorPredios!!.warning.additionalMessage
                        Log.e("GEOPDM", message)
                    }
                }
                Log.d("GEOPDM", "Predios deteccion ready")
            }
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2022 18:23:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1228016#M5880</guid>
      <dc:creator>PabloAndrésDiazMazzaro</dc:creator>
      <dc:date>2022-11-02T18:23:21Z</dc:date>
    </item>
    <item>
      <title>Re: Geotrigger only works once (when device's position starts inside geofence)</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1228233#M5881</link>
      <description>&lt;P&gt;That's great to hear it's working! Thank you for sharing your code as well. If you have any other issues, do let us know!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2022 09:19:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-android-questions/geotrigger-only-works-once-when-device-s-position/m-p/1228233#M5881</guid>
      <dc:creator>Hudson_Miears</dc:creator>
      <dc:date>2022-11-03T09:19:43Z</dc:date>
    </item>
  </channel>
</rss>

