开发者

How to find nearest locations like banks, restaurant in iPhone

I am making an application in 开发者_高级运维which i am getting the current location, name and address, latitude and longitude, however how to find the nearest places like banks, restaurant,bus stand near my current location in iPhone.


You can use google's service like so and call it in a loop changing the pointOfInterest string each time:

CLLocationCoordinate2D coordinate = location.coordinate;
NSString *pointOfInterest = @"banks";
NSString *URLString = [NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&rsz=small&sll=%f,%f&q=%@",coordinate.latitude,coordinate.longitude, pointOfInterest];

URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]];

// Perform request and get JSON back as a NSData object
NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if(error != nil) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" 
                                                         message:[error localizedDescription] 
                                                        delegate:self 
                                               cancelButtonTitle:@"Done" 
                                               otherButtonTitles:nil] autorelease]; 
        [alert show];
}
else {
// Get JSON as a NSString from NSData response
    NSString *jsonString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    //Return the values in NSDictionary format  
    SBJsonParser *parser = [[SBJsonParser alloc] init];

    NSDictionary *jsonResponse = [parser objectWithString:jsonString error:nil];
    NSDictionary *responseData = [jsonResponse objectForKey:@"responseData"];
    NSArray *results = [responseData objectForKey:@"results"];
}

You can get JSON API here:

https://github.com/stig/json-framework/


You will need a API which will give you these things, there isn't one in the standard SDK.


You should have an object that represents a bank, restaurant or bus (Station in the example). You'll probably want it to implement the MKAnnotation protocol as you may want to add them to a MKMapView. Each of these objects need a coordinate property (CLLocationCoordinate2D). When I'm doing things like this, I also add a distance property (CLLocationDistance). When instantiating these objects in your viewcontroller, you'd add them to an array.

Now, when the user's application updates, you let each object calculate the distance from that location to itself.

For example:

- (void)calculateDistance:(CLLocation *)location {
    CLLocation *stationLocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    distance = [location distanceFromLocation:stationLocation];
    [stationLocation release];
}

Once you let every object calculate its distance, you can now sort your array with objects by distance.

[stations sortUsingSelector:@selector(compareDistance:)]

This requires that your objects have this method implemented:

- (NSComparisonResult)compareDistance:(Station *)aStation {
    if (distance < aStation.distance) return NSOrderedAscending;
    if (distance > aStation.distance) return NSOrderedDescending;
    return NSOrderedSame;
}

Now you should have your array with objects representing banks etc sorted by distance.

[stations objectAtIndex:0] would be the closes to your location.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜