开发者

find objects close to mine

i am building an iphone app and i am looking for an idea how to get objects close to my position. the objects have an address listed in my database. now i am interested to show the objects that are close to mine on the map. how do i get that property? at first i thought of floyd-warshall b开发者_如何学Gout is there an better solution than coding that myself?


The first thing you'll need to do is get Lat Long positions in your database instead of Addresses. There is a tool that google provides that does this. There is also a manual approach.

Then you can use the Haversine formula to work out if it is within a certain radius of your location.

Here is a example. You can cycle through your objects('s location) on the map and see how far away they are from you.

-(CGFloat) getDistance :(CGFloat)objLatitude :(CGFloat)objLongitude :(CGFloat)currentLocationLatitude :(CGFloat)currentLocationLongitude
{
    CGFloat earthRadius = 6371;

    CGFloat deltaLat = deg2rad(currentLocationLatitude - objLatitude);
    CGFloat deltaLong = deg2rad(objLongitude - currentLocationLongitude);

    CGFloat a = sin(deltaLat/2) * sin(deltaLat/2) +
        cos(deg2rad(objLatitude)) * cos(deg2rad((double)currentLocationLatitude)) * 
        sin(deltaLong/2) * sin(deltaLong/2);

    CGFloat c = 2 * atan2(sqrt(a), sqrt(1 - a));
    CGFloat d = earthRadius * c;

    d = round(d, 4); // round to 4dp
    d = d * 1000; // convert from km to m
    d = d * 0.621371192; // convert from km to miles

    return d; // m
}

You could amend this method to return TRUE if it is within a specific distance and FALSE if it does not:

-(BOOL) doesObjFallWithinMyRadius :(CGFloat)objLatitude :(CGFloat)objLongitude :(CGFloat)currentLocationLatitude :(CGFloat)currentLocationLongitude :(CGFloat)distanceLimit
{
    CGFloat earthRadius = 6371;

    CGFloat deltaLat = deg2rad(currentLocationLatitude - objLatitude);
    CGFloat deltaLong = deg2rad(objLongitude - currentLocationLongitude);

    CGFloat a = sin(deltaLat/2) * sin(deltaLat/2) +
        cos(deg2rad(objLatitude)) * cos(deg2rad((double)currentLocationLatitude)) * 
        sin(deltaLong/2) * sin(deltaLong/2);

    CGFloat c = 2 * atan2(sqrt(a), sqrt(1 - a));
    CGFloat d = earthRadius * c;

    d = round(d, 4); // round to 4dp
    d = d * 1000; // convert from km to m
    d = d * 0.621371192; // convert from km to miles

    if(d < distanceLimit) return TRUE;
    else return FALSE;
}

To find out your current location you will need to use the CLLocationManager which is part of the iOS SDK.


This may not be the solution you want but this will help you to get distance between the locations. If you are plotting them on a Map u might have the coordinates. Then u can get the distance from these points to your location using distanceFromlocation: method of CLLocation to get the distance. Then you can plot them based on the distance.


Have you seen a KD-Tree? You can use this to find places near to you.

Of course, you'd have to assume that the Earth was flat, but I can't believe that can cause a problem right? :) It's an interesting data structure to look at though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜