Get the distance from the user's location to the venue


/ Published in: Objective C
Save to your folder(s)

This is the code i wrote for clubplanet to get the distance from the user's location to the venues.


Copy this code and paste it in your HTML
  1. //Get the distance from the user's location to the venue
  2. CLLocation *userLoc = [[CLLocation alloc] initWithLatitude:userLocation.latitude longitude:userLocation.longitude];
  3.  
  4. CLLocationCoordinate2D venueCoord = [self addressLocation:venueAddress];
  5. CLLocation *venueLoc = [[CLLocation alloc] initWithLatitude:venueCoord.latitude longitude:venueCoord.longitude];
  6.  
  7. double dist = [userLoc getDistanceFrom:venueLoc] / 1609.344;
  8.  
  9. //NSLog(@"%.2f miles", dist);
  10.  
  11. lblapproxdistance.text = [NSString stringWithFormat:@"%.1f miles",dist];
  12.  
  13.  
  14.  
  15.  
  16.  
  17. #pragma mark addressLocation
  18.  
  19. //method to convert address to longitude and latitude
  20. -(CLLocationCoordinate2D) addressLocation:(NSString *)address {
  21. NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv&key=ABQIAAAA-d08Ko4-kHQWa1zY6Y4n2BQJqvlAqZSQuvHwEwe-mLrrdxxNhxRp0156iscZ4xaIZTeX_4YwLHlJEA",
  22. [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  23. NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
  24. NSArray *listItems = [locationString componentsSeparatedByString:@","];
  25.  
  26. double _latitude = 0.0;
  27. double _longitude = 0.0;
  28.  
  29. if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
  30. _latitude = [[listItems objectAtIndex:2] doubleValue];
  31. _longitude = [[listItems objectAtIndex:3] doubleValue];
  32. }
  33. else {
  34. //Show error
  35. }
  36. CLLocationCoordinate2D _location;
  37. _location.latitude = _latitude;
  38. _location.longitude = _longitude;
  39.  
  40. //NSLog(@"%f - %f", latitude, longitude);
  41.  
  42. return _location;
  43. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.