Xamarin forms saved pins near my location

AlexAbreuAlexAbreu USMember ✭✭

Hello Guys, I'm saving the locations for a list of pins in a city in a database. What I would like to do is display just the pins that are 1 to 3 miles from my locations. For example in a Taxi app you can see drivers near your locations, I'm looking to do something similar with pins near my locations.

Answers

  • ThorvaldBoeThorvaldBoe NOMember ✭✭

    In order to do this, you need to:
    1.Know your current position
    2.Be able to calculate the distance from your current position to any given position (your pins) in order to select only the nearest pins

    xam.plugin.geolocator can help you with #1

    For calculating distance, you have a few options. You could use e.g. the google api's, but I find it easier to do it locally to avoid calling external services. The simplest option is probably to use the GeoCoordinate.GetDistanceTo method:
    https://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate.aspx

    If you prefer, you can also do it yourself, by some simple geometry. Both methods are discussed in this thread:
    https://stackoverflow.com/questions/6366408/calculating-distance-between-two-latitude-and-longitude-geocoordinates

  • AlexAbreuAlexAbreu USMember ✭✭

    Thank you ThorvaldBoe, But what happens if I have thousand of records in my database I cannot compare line by line to see the list of pins near my location. how can I bring just the records near my location. I know that app like uber or google maps does the same without read the entire data.

  • seanydaseanyda GBMember ✭✭✭✭✭

    @AlexAbreu said:
    Thank you ThorvaldBoe, But what happens if I have thousand of records in my database I cannot compare line by line to see the list of pins near my location. how can I bring just the records near my location. I know that app like uber or google maps does the same without read the entire data.

    Without seeing the data structure stored in your database it would be difficult to give a suggestion.

    I think something like this might be what you're looking for.
    https://developers.google.com/maps/documentation/distance-matrix/intro

  • ThorvaldBoeThorvaldBoe NOMember ✭✭

    Choosing the right architecture for your app is a vital part of your design. This depends on factors like amount of data, frequency of requests, number of users, etc. This is impossible to give an answer to from the limited information you provide about the case. I do understand that you have some amounts of data, maybe too much to process locally. I am not sure if thousands of locations would be too much to process locally, that depends on the use case. But let's say you have tens or hundreds of thousands, or more, and you need a quick way to retrieve the locations near you.

    Let me first say, that this approach has a few drawbacks:
    1.It will cost you money, since it is based on a back end
    2.It will be dependent on network traffic and the availability of internet, but if you're in a city, that is probably not a problem. However, it will cause data traffic, which the end user will be paying for, so you would have to try to set the frequency of the calls and amount of data to a minimum to save the user from excessive spendage

    Ok, so to a possible solution:
    1.If you don't already have one, create an Azure account. Apparently, you can get a lot of free credits these days, too.
    2.Create a CosmosDB database, and store your data here. CosmosDB is a global, noSQL database that is extremely fast
    3.Take a look at geospatial queries in CosmosDB:
    https://docs.microsoft.com/en-us/azure/cosmos-db/geospatial
    Presumably, you can easily query large amounts of data to return only the small subset relevant to your position
    4.Create an Azure function (https://azure.microsoft.com/en-us/services/functions/) that takes five parameters: your current position, and the four corners of your map view. Maybe only the four corners are necessary, but you might want to e.g. highlight the closest pin, in which case you need the current location. Azure functions have CosmosDB input bindings, so it is easy for a function to perform a query against the data and return the relevant set. And it is also automatically scalable and you pay only for usage, so functions are very handy.
    5.Your app sends http requests to your Azure function at regular intervals, or if the user has moved a certain distance (remember being economic on the data traffic), and gets all pins/locations within the current map view, or within a certain radius of the current location.

    Doing this, you will have an app built for massive scale with the ability to handle millions of locations easily.

    Good luck!

Sign In or Register to comment.