1

I am making location aware application. I have XML file on server containing information of different stores with latitude and longitude coordinates. Now in my application i can get my current latitude and longitude coordinates and can parse XML file as well.

But how can i figure out nearest store according to my current coordinates from XML file?

If we use google Api it returns you xml file containing nearest locations according to query. but here in my case i am using xml file on server.

Please suggest

Mann
  • 5,477
  • 6
  • 45
  • 57

2 Answers2

3

I think you'd be better off converting your data into a DB format (sqlite/mysql) so that the user can submit their lat/Lng point and just get the correct item returned...otherwise you'd need to parse and compare the whole file each time...but, you can still use that formula (it's really just shifting the comparison step to the DB query rather than within your app)

Edit: this has an example of a SQL query that implements haversine and returns x results: MySQL Great Circle Distance (Haversine formula)

Community
  • 1
  • 1
Robot Woods
  • 5,677
  • 2
  • 21
  • 30
  • 1
    +1 for minimizing traffic and minimizing work on the device. Did not think about that :) – SideSwipe Aug 13 '11 at 14:36
  • So you think i have to send my latitude and longitude to the sql server database. and then calculation has to be done on server side? – Mann Aug 13 '11 at 14:40
  • One more question arises. would it be safe to connect application with sql server? how can i connect it securely? – Mann Aug 13 '11 at 14:44
  • well, create a web service (php is my go-to) that acts as an intermediary, don't literally connect directly to your DB...but, if you are using sqlite, which is ON the device, you don't need that...but if you want to frequently edit the data, I'd house the data centrally with a web service. if it's fairly static, I'd ship the data with the app – Robot Woods Aug 13 '11 at 20:28
  • Hello robot woods and sideSwipe. I was wondering u suggest me to implement heversine formula to server side. but there will be thousands of users accessing application same time. then how many xml file database has to generate at the same time? is it possible in database prospective? – Mann Aug 17 '11 at 11:08
  • the web service won't be generating databases, it'll just echo out the nearest store (in XML format [or whatever format you choose]). Look at this tutorial: http://code.google.com/apis/maps/articles/phpsqlsearch.html – Robot Woods Aug 17 '11 at 14:00
2

This is the haversine formula for calculating distance between two points on the earth:

a = sin²(Δlat/2) + cos(lat1)*cos(lat2)*sin²(Δlong/2);
c = 2*atan2(√a, √(1−a));
d = R*c;

You can easily adapt it in obj-c.

Please note: R is radius of the earth = 6,371km;

So just calculate the latitude and the longitude difference of both points and calculate d based on it.

Hope this helps.

SideSwipe
  • 559
  • 3
  • 15
  • Do this for every store, and then check for smallest d :) – SideSwipe Aug 13 '11 at 14:15
  • Thanks for the information. Can you please explain it a bit. like i will have two coordinates. lat,long of my current location and lat,long of stores how can i impliment this formula – Mann Aug 13 '11 at 14:17
  • You can find some C Code here: http://www.jaimerios.com/?p=39 Just runt he second C function ("converted" to obj-c at best) with the coordinates of the user and the ones for each store, and store the distance as the object to the key of the store in an nsdictionary, then check which store has the smallest distance. – SideSwipe Aug 13 '11 at 14:19