Showing posts with label iOS. Show all posts
Showing posts with label iOS. Show all posts

Saturday, 20 February 2016

Geocoding Google map in iOS

#define GEOCODING_URI @"https://maps.googleapis.com/maps/api/geocode/json?key=&language=en-US&sensor=true&address=" // TODO: add DEV_KEY for Geocoding
NSString *actionURI = [NSString stringWithFormat:@"%@%@",GEOCODING_URI, self.geocodeTextField.text];
NSLog(@"ACTION URL: %@",actionURI);
NSString *encodedURI = [actionURI stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManager manager];
[manager GET:encodedURI parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject); 
NSDictionary *jsonDict = (NSDictionary *)responseObject;
if (!jsonDict)
   return;
if ([jsonDict isKindOfClass:[NSDictionary class]] == NO)
   NSAssert(NO, @"Expected a dictionary, got %@", NSStringFromClass([jsonDict class]));     
if ([jsonDict[@"status"] isEqualToString:@"OK"]) {
    NSArray *locGeometryResult=[[jsonDict valueForKeyPath:@"results.geometry"] objectAtIndex:0];
    NSDictionary *locationDict = [locGeometryResult valueForKey:@"location"];
    NSLog(@"JSON: %@", locationDict.description);
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([locationDict[@"lat"] doubleValue], [locationDict[@"lng"] doubleValue]);
    GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:coordinate zoom:17];
    [self.mapView animateWithCameraUpdate:updatedCamera];
} else {
        NSLog(@"No location found.");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@"); 
}];

Thursday, 4 July 2013

Crash on route map | Map route Directions

Actually, if your current location is too far from destination, then google api response returns
{tooltipHtml:" (0.0 km / 0 secs)"}
My app was crash, because there is no handling. If you are using encoded NSString parameter accepted in your decodePolyline function, replace it with NSMutableString.
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded :(CLLocationCoordinate2D)f to: (CLLocationCoordinate2D) t 
{
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
  range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
printf("[%f,", [latitude doubleValue]);
printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
[array addObject:loc];
}
    CLLocation *first = [[[CLLocation alloc] initWithLatitude:[[NSNumber numberWithFloat:f.latitude] floatValue] longitude:[[NSNumber numberWithFloat:f.longitude] floatValue] ] autorelease];
    CLLocation *end = [[[CLLocation alloc] initWithLatitude:[[NSNumber numberWithFloat:t.latitude] floatValue] longitude:[[NSNumber numberWithFloat:t.longitude] floatValue] ] autorelease];
[array insertObject:first atIndex:0];
    [array addObject:end];
return array;
}

-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t 
{
    
NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];
NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
DLog(@"api url: %@", apiUrl);
NSError *error;
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];
    NSString *apiResponseString = [apiResponse mutableCopy];
NSString* encodedPoints = [apiResponseString stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];
    return [self decodePolyLine:[encodedPoints mutableCopy]:f to:t];
}

Wednesday, 1 May 2013

Find iOS versions programmatically

#define iOS_VERSION_EQUAL_TO(version) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedSame)
#define iOS_VERSION_GREATER_THAN(version) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedDescending)
#define iOS_VERSION_GREATER_THAN_OR_EQUAL_TO(version) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] != NSOrderedAscending)
#define iOS_VERSION_LESS_THAN(version) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedAscending)
#define iOS_VERSION_LESS_THAN_OR_EQUAL_TO(version) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] != NSOrderedDescending)


Let say check for iOS 5.0
if (iOS_VERSION_LESS_THAN(@"5.0"))