Search code examples
androidazure-mobile-serviceshaversine

reading data using a server side script with Azure Mobile Services


Hi guys I have an android application which uses Azure mobile services to store data. I have a query which involves the haversine algorithm. The query works perfectly as it gets the nearest restaurant locations relative to the longitude and latitude inserted into the algorithm. However I have no idea how to use the query. Should i use it within the android app or should I use a server side script. The query is as follows

Select * from
(
 SELECT *,( 6371 * acos( cos( radians(BOurGuest.Restaurants.latitude) ) *    cos( radians( -6.391252 ) )
 * cos( radians( 53.284412 ) - radians(BOurGuest.Restaurants.longitude) ) +
 sin( radians(BOurGuest.Restaurants.latitude) ) * sin( radians( -6.391252 ) ) ) ) 
 AS Distance FROM BOurGuest.Restaurants
) as inner_table
WHERE Distance < 5;

If i were to use the query within the app or as a server side script could someone show me how to do it.


Solution

  • Azure Mobile Services doesn't have built in support for Geo data types right now, however, you can get around this without too much trouble. If you're using the JavaScript backend, you'll want to use the MSSQL module to execute custom SQL statements (or create stored procedures and call those). You can use this approach to save geography datatypes into your database (as Azure SQL Database DOES support geography). You can then use the same module to execute the SQL statement you have above. Alternatively, if you're using the .NET backend, you'll have the same ability to call custom SQL but should also be able to use Entity Framework to handle geography types.

    You can see a full demo of how to do this here: https://code.msdn.microsoft.com/windowsapps/Geolocation-sample-end-to-5d9ee245