Application to notify the user when reaching closer to predefined destination

AshaarAshaar Ashaar MohamedUSMember

I am developing an App to notify the users when they reach few Meters closer to where I have already predefined locations. Can you please guide me on right approach and appreciate if you could share any sample.

Answers

  • ClintStLaurentClintStLaurent Clint StLaurent USUniversity ✭✭✭✭✭

    Have you walked through the intro to Xamarin tutorials?
    Do you have an understanding of XAML/C# and MVVM design pattern?
    What is your previous experience level with coding?
    Have you built any Xamarin apps previously... Any mobile apps previous... Any programs at all?

    • Just trying to ascertain a starting point - You don't mention if this is your first ever project, or if you have 20 years of coding at 5 years of Xamarin.

    FYI: You're not going going to get "a few meters" with most devices. Their GPS chips just aren't that accurate. The cheaper the device the cheaper the chips. I've seen Samsung Tab5 bounce 20-50 meters while sitting still in the parking lot.

    The reason turn-by-turn direction app seem to have good accuracy is because they are constantly getting new points while in motion over a known route and use a fair amount of logic to throw out points that can't be valid.

  • AshaarAshaar Ashaar Mohamed USMember

    @ClintStLaurent said:
    Have you walked through the intro to Xamarin tutorials?
    Do you have an understanding of XAML/C# and MVVM design pattern?
    What is your previous experience level with coding?
    Have you built any Xamarin apps previously... Any mobile apps previous... Any programs at all?

    • Just trying to ascertain a starting point - You don't mention if this is your first ever project, or if you have 20 years of coding at 5 years of Xamarin.

    FYI: You're not going going to get "a few meters" with most devices. Their GPS chips just aren't that accurate. The cheaper the device the cheaper the chips. I've seen Samsung Tab5 bounce 20-50 meters while sitting still in the parking lot.

    The reason turn-by-turn direction app seem to have good accuracy is because they are constantly getting new points while in motion over a known route and use a fair amount of logic to throw out points that can't be valid.

    Yes, I went through few Xamarin articles and this is my first Xamarin/Mobile App.

    I am having few years of experience and having hands on experience with all most all of your questions above.

    Yes, it is my fault instead of saying the distance in KM I defined as Meters.

  • ClintStLaurentClintStLaurent Clint StLaurent USUniversity ✭✭✭✭✭

    having hands on experience with all most all of your questions above

    That's Great!

    What is it you need help with? Specifically.

  • AshaarAshaar Ashaar Mohamed USMember
    edited March 20

    @ClintStLaurent said:

    having hands on experience with all most all of your questions above

    That's Great!

    What is it you need help with? Specifically.

    I am planning to develop an App where when a user moving around to different location it should notify the predefined locations within few KMs radius i.e. Schools, Day Care ... etc.

    1. I'll be locally store latitude and longitude of all the identified location's(only necessary places)
    2. When I am moving around, based on my current Geolocation the application should return list of predefined locations from the database within few KMs

    How can I get the closest locations based on latitude and longitude from my GeoLocation? Consider as I mentioned above I already stored all the places which I should query(latitude and longitude) in my local database

  • ClintStLaurentClintStLaurent Clint StLaurent USUniversity ✭✭✭✭✭
    edited March 20

    So you're basically asking...

    Will you design the entire app for me, then explain to me how to make it?

    Is that about right? Or do you have a specific question?

  • AshaarAshaar Ashaar Mohamed USMember

    @ClintStLaurent said:
    So you're basically asking...

    Will you design the entire app for me, then explain to me how to make it?

    Is that about right? Or do you have a specific question?

    Did I ask anywhere to develop the app for me? are you trying to answer the question or trying to deviate? I have clearly mentioned the questioned on my last reply right?

    If I am not wrong, the information which I have already provided good enough to answer the question, if you are intelligent enough and an experienced developer.

  • ClintStLaurentClintStLaurent Clint StLaurent USUniversity ✭✭✭✭✭

    I'm sorry... Maybe I missed it. I have only seen "Guide me on how to make my app". Did I miss a specific question?

  • AshaarAshaar Ashaar Mohamed USMember
    edited March 20

    I am not sure is there any better approach, currently this is what I am going to do.

    1. I'll retrieve all the locations from the database which I have already stored and put it inside the collection
    2. I can filter-out the above object by calling the below method via loop.

      public double Distance(double sourceLat, double sourceLog, double destinationLat, double destinationLog, char unit = 'K')
      {            
          double rlat1 = Math.PI * sourceLat/ 180;
          double rlat2 = Math.PI * destinationLat / 180;
          double theta = sourceLog - destinationLog;
          double rtheta = Math.PI * theta / 180;
          double dist =
              Math.Sin(rlat1) * Math.Sin(rlat2) + Math.Cos(rlat1) *
              Math.Cos(rlat2) * Math.Cos(rtheta);
          dist = Math.Acos(dist);
          dist = dist * 180 / Math.PI;
          dist = dist * 60 * 1.1515;
      
          switch (unit)
          {
              case 'K': //Kilometers -> default
                  return dist * 1.609344;
              case 'N': //Nautical Miles 
                  return dist * 0.8684;
              case 'M': //Miles
                  return dist;
          }
      
          return dist;
      }
      
  • ClintStLaurentClintStLaurent Clint StLaurent USUniversity ✭✭✭✭✭

    You could use the Google.Maps distance API call.
    Its more internet traffic if you hammer it 10 times a second, but you'd get driving distance between A and B, instead of line-of-site distance. If you make the called on a recurring basis like once every 30 seconds it shouldn't amount to a ton of data.
    https://developers.google.com/maps/documentation/distance-matrix/intro

  • AshaarAshaar Ashaar Mohamed USMember

    @ClintStLaurent said:
    You could use the Google.Maps distance API call.
    Its more internet traffic if you hammer it 10 times a second, but you'd get driving distance between A and B, instead of line-of-site distance. If you make the called on a recurring basis like once every 30 seconds it shouldn't amount to a ton of data.
    https://developers.google.com/maps/documentation/distance-matrix/intro

    Thanks for the reply, I feel performance wise/practically the method which I have post might be better isn't.
    If not let's say if we have 1000+ locations in our database and if we want to filter out the locations which reside within 2KM or etc from my current point, we have to call the API 1000+ times to get the distance in-order to filter right.

  • ClintStLaurentClintStLaurent Clint StLaurent USUniversity ✭✭✭✭✭

    Your title says "When user gets to A predefined destination" - That sounds like you're driving and want to know when you are approaching the end of the trip: The Destination. That doesn't sound like being in the vacintity of any out of a 1,000 GPS points.

    if we want to filter out the locations which reside within 2KM

    Has nothing to do with the title of the post.

    Sorry if this sounds mean, but when you only take 10 seconds to write a 1-sentence question you're not likely to get an answer that actually applies to your situation.

    notify the users when they reach few Meters closer to where I have already predefined locations.

  • DavidDancyDavidDancy David Dancy AUMember ✭✭✭✭

    @AshaarMohamed what you've described sounds to me like Geofencing. If you search that up, you should find that there's already a Xamarin plugin that will help you out a lot here: https://github.com/domaven/xamarin-plugins/tree/master/Geofence.

    There's even a sample project that should help you get going.

  • mikeacostamikeacosta Michael Acosta USUniversity ✭✭✭
    @AshaarMohamed I was going to suggest just what DavidDancy did. I've actually developed a cross-platform app for delivery drivers using that plug-in to monitor a collection of circular regions and can attest to its reliability on iOS & Android, at least in my experience.
  • AshaarAshaar Ashaar Mohamed USMember

    @DavidDancy said:
    @AshaarMohamed what you've described sounds to me like Geofencing. If you search that up, you should find that there's already a Xamarin plugin that will help you out a lot here: https://github.com/domaven/xamarin-plugins/tree/master/Geofence.

    There's even a sample project that should help you get going.

    @DavidDancy, thank you for your response. I'll go through the article and get back to you

  • AshaarAshaar Ashaar Mohamed USMember
    edited March 21

    @Ashaar said:

    @DavidDancy said:
    @AshaarMohamed what you've described sounds to me like Geofencing. If you search that up, you should find that there's already a Xamarin plugin that will help you out a lot here: https://github.com/domaven/xamarin-plugins/tree/master/Geofence.

    There's even a sample project that should help you get going.

    @DavidDancy, thank you for your response. I'll go through the article and get back to you

    I am running the sample App which you have shared above, but I couldn't create Geofencing area.
    The application is continuously showing the 'Loading' message after proving the Radius, is there anything I am missing.
    Please note, I have created the API key and update on AndroidManifest file.

    Exception is throwing from here

    CrossGeofence.Current.StartMonitoring(new GeofenceCircularRegion (place.Name,place.Latitude,place.Longitude,place.Radius) {
    
                        NotifyOnStay=true,
                        StayedInThresholdDuration=TimeSpan.FromMinutes(5)
    
                    });
    
  • AshaarAshaar Ashaar Mohamed USMember

    @DavidDancy , @mikeacosta
    Can you please tell how device gets notification when passing the Geofencing area, if I am not wrong I think we need to send the messages via push notification.
    If so where is the integration point … etc. I went through few articles but still I am not quite clear on how the integration and messaging part works.

  • mikeacostamikeacosta Michael Acosta USUniversity ✭✭✭
    edited March 21

    Your StartMonitoring code looks correct. What exception are you getting?
    I don't recall needing an API key for the plug-in. Maybe you're referring to something else.
    You don't need to do anything with notifications - the plug-in does it.

    Here's a sol'n I created when I was testing out the plug-in. You can replace the regions in Services/PlacesService.cs with your coordinates.

    https://github.com/mikeacosta/xamarin-geofence

  • AshaarAshaar Ashaar Mohamed USMember
    edited March 21

    @mikeacosta said:
    Your StartMonitoring code looks correct. What exception are you getting?
    I don't recall needing an API key for the plug-in. Maybe you're referring to something else.
    You don't need to do anything with notifications - the plug-in does it.

    Here's a sol'n I created when I was testing out the plug-in. You can replace the regions in Services/PlacesService.cs with your coordinates.

    https://github.com/mikeacosta/xamarin-geofence

    Thank you for your sample, I can run the application where I have replaced the regions closest to me. As a result it shows 3 List Item in the home page and I have selected one out of three but nothing changed.

    Can you please tell now how the notification will come and how that happens.

  • mikeacostamikeacosta Michael Acosta USUniversity ✭✭✭

    It just shows up as a push notification just like any other notification.

    On Android, when I'm already inside a geofence and I click to have the app start monitoring that location, I immediately get an "entered" notification. Maybe you can set up a geofence where you live and do some debugging.

    This doesn't happen on iOS. For that platform, I have to actually enter/exit to get a notification.

  • AshaarAshaar Ashaar Mohamed USMember
    edited March 21

    @mikeacosta said:
    It just shows up as a push notification just like any other notification.

    On Android, when I'm already inside a geofence and I click to have the app start monitoring that location, I immediately get an "entered" notification. Maybe you can set up a geofence where you live and do some debugging.

    This doesn't happen on iOS. For that platform, I have to actually enter/exit to get a notification.

    I have created geofence which includes my current location, but I am not getting anything when I am running the app, as you mentioned above the push notification here in my case doing service integration or even without pushing any messages how can I expect. I am afraid may be I am missing something, also you said you are getting "entered" when you are inside the geofence, have you already configured the service and message somewhere to push. Please advice further.

    Also, if I am not wrong the sample code which you have provided doesn't have any methods to receive the messages right. it only calls monitoring isn't.

     public void SelectPlace(Place place) 
        {
            if (place == null) { return; }
    
            CrossGeofence.Current.StopMonitoringAllRegions();
    
            GeofenceCircularRegion region = new GeofenceCircularRegion(place.Name, place.Latitude, place.Longitude, place.Radius);
            var crossGeofenceInstance = CrossGeofence.Current;
            crossGeofenceInstance.StartMonitoring(region);
        }
    
  • mikeacostamikeacosta Michael Acosta USUniversity ✭✭✭
    edited March 21

    I've done nothing more than create that test app, following the guidelines in the README for the plugin's repo. What are you using for place.Radius? Maybe try expanding that. Also fwiw, I get lat/long values from this site:

    http://www.latlong.net/convert-address-to-lat-long.html

    Also, you've likely noticed from the code, but for that test app, it only monitors the currently selected location.

  • DavidDancyDavidDancy David Dancy AUMember ✭✭✭✭

    IIRC the plugin has a callback (event) that tells any registered subscriber when the region is entered or exited.

  • AshaarAshaar Ashaar Mohamed USMember

    @DavidDancy said:
    IIRC the plugin has a callback (event) that tells any registered subscriber when the region is entered or exited.

    Thanks David, what do you mean by 'registered subscriber', as a developer do we need to subscribe with any messaging service or etc, if so how and where, also if we want to push the message to target devices is there any interface to do or we have to implement all above by ourself

  • DavidDancyDavidDancy David Dancy AUMember ✭✭✭✭

    @Ashaar OK, so I was remembering wrong. I think I must have adapted the plugin and given it an event myself.

    If you have a look at the example usage, you need to create an object that implements IGeofenceListener and register it in your platform startup classes. That object will get the callbacks from the Geofence library to tell it about region entry and exit.

  • AshaarAshaar Ashaar Mohamed USMember
    edited March 22

    @DavidDancy said:
    @Ashaar OK, so I was remembering wrong. I think I must have adapted the plugin and given it an event myself.

    If you have a look at the example usage, you need to create an object that implements IGeofenceListener and register it in your platform startup classes. That object will get the callbacks from the Geofence library to tell it about region entry and exit.

    There are 2 issues prompt while running the sample code

    • When application load it throws below error from MapPage.xaml.cs/onAppearing but still the app is running without crashing

      mapView.MoveToRegion (
              MapSpan.FromCenterAndRadius (
                  new Xamarin.Forms.Maps.Position (lat, lon), Distance.FromMiles (1)).WithZoom (20));
      
    • When adding geofence radius and press OK, the below code snippet throws exception saying
      'System.NullReferenceException: Object reference not set to an instance of an object' and it continuously displaying 'loading' icon

      CrossGeofence.Current.StartMonitoring(new GeofenceCircularRegion (place.Name,place.Latitude,place.Longitude,place.Radius) {
      
                  NotifyOnStay=true,
                  StayedInThresholdDuration=TimeSpan.FromMinutes(5)
          });
      
  • DavidDancyDavidDancy David Dancy AUMember ✭✭✭✭

    Sorry, I have no idea about the Map exception.

    As for the NullReferenceException, are you familiar with the process for tracking those down? Usually I go to the Breakpoints Pad in the IDE and add a NullReferenceException catch point. Then when it gets thrown the IDE should show you the place where it's happening, and you can go on from there to fix the problem.

  • ClintStLaurentClintStLaurent Clint StLaurent USUniversity ✭✭✭✭✭

    @DavidDancy said:
    As for the NullReferenceException, are you familiar with the process for tracking those down?

    Debugging tutorials:
    1. What is this error and how do I fix it?
    2. Debugging tips and tricks

  • AshaarAshaar Ashaar Mohamed USMember

    @DavidDancy said:
    Sorry, I have no idea about the Map exception.

    As for the NullReferenceException, are you familiar with the process for tracking those down? Usually I go to the Breakpoints Pad in the IDE and add a NullReferenceException catch point. Then when it gets thrown the IDE should show you the place where it's happening, and you can go on from there to fix the problem.

    The exception was thrown from 'new GeofenceCircularRegion()', anyway now it is working fine.

  • AshaarAshaar Ashaar Mohamed USMember

    @DavidDancy, @mikeacosta, @ClintStLaurent
    Thank you for your support on Geofence, specially @DavidDancy and @mikeacosta shared information up-to the bottom.

    Good news is, my POC application is working as expected.

  • AshaarAshaar Ashaar Mohamed USMember

    @DavidDancy , @mikeacosta , @ClintStLaurent
    Can you please tell whether we customize the message from notification panel which is generated via geofence. Currently I am getting
    Entered geofence region: xxxx
    Stayed in geofence region: xxxx
    Exited geofence region: xxxx

    Also, how can we navigate to another page when a user tab on any of the above notification. Can you please share the code sample or tutorials if you have, thanks in advance.

  • DavidDancyDavidDancy David Dancy AUMember ✭✭✭✭

    @Ashaar sorry I don't have code for that. But you can find code for it in anything that handles Push Notifications, because Local Notifications are almost identical.

    As for the Notification itself and how to customise it, I think you'll need to look in the source code. I just had a quick check of the iOS version and the Notification stuff is generated by a custom function called CreateNotification. This is internal to the code, so if you want to change the information that gets included in the Notification you'll need to uninstall the NuGet package and install the source code instead.

  • mikeacostamikeacosta Michael Acosta USUniversity ✭✭✭

    xxxx is the value of the Name property you've assigned for GeofenceCircularRegion. One possibility would be to change that value in the shared GeofenceListenter class' OnRegionStateChanged method based on id and event type (entering or exiting).

    I'm pretty sure the notification behavior you're after would need to be implemented per platform and is not necessarily unique to this plug in. Nevertheless, maybe try putting a breakpoint in OnResume and see if the api exposes anything useful.

  • AshaarAshaar Ashaar Mohamed USMember
    edited March 24

    @DavidDancy said:
    @Ashaar sorry I don't have code for that. But you can find code for it in anything that handles Push Notifications, because Local Notifications are almost identical.

    As for the Notification itself and how to customise it, I think you'll need to look in the source code. I just had a quick check of the iOS version and the Notification stuff is generated by a custom function called CreateNotification. This is internal to the code, so if you want to change the information that gets included in the Notification you'll need to uninstall the NuGet package and install the source code instead.

    @DavidDancy Thanks for the prompt response, I'll download the source and try to play-around.

    Again, for my second question also I think I have to check the NuGet package source isn't it?
    'Can we navigate to another page when a user tab on any of the above notification'

  • AshaarAshaar Ashaar Mohamed USMember

    @mikeacosta said:
    xxxx is the value of the Name property you've assigned for GeofenceCircularRegion. One possibility would be to change that value in the shared GeofenceListenter class' OnRegionStateChanged method based on id and event type (entering or exiting).

    I'm pretty sure the notification behavior you're after would need to be implemented per platform and is not necessarily unique to this plug in. Nevertheless, maybe try putting a breakpoint in OnResume and see if the api exposes anything useful.

    @mikeacosta Thanks for your prompt response, let me try first what David suggested :)

Sign In or Register to comment.