顯示具有 iOS English 標籤的文章。 顯示所有文章
顯示具有 iOS English 標籤的文章。 顯示所有文章

2013年5月9日 星期四

[iOS] print Log

Because We will need a lot of  information to debug,
so we can use NSLog to print log.

However, these log information we do not want are still being printed out after the release,
we can define like below in project-Prefix.pch:
1
2
3
4
5
6
7
8
//enable NSLogs
#define DEBUG_MODE
#ifdef DEBUG_MODE
    #define DLog(...) NSLog(__VA_ARGS__)
#else
    #define DLog(...) 
#endif
#define ALog(...) NSLog(__VA_ARGS__)
Then use DLog to replace NSLog which one you want control it will be printed or not,
use ALog to replace the one you want always be printed.

When you dont need printed DLog,
 just comment this line #define DEBUG_MODE.




---------------------- By the way below is something about NSLog's usage -----------------------

In order to track bug occurred,
We can add below at each important function:

- (IBAction)showRangeList:(id)sender {
    DLog(@"%s Enter", __func__);
}
When APP executed to this line will print:
-[MainViewController showRangeList:] Enter



And NSLog already provide format,
so we don't need code like this:

NSLog(@"%@",[NSString stringWithFormat:@"%@ %@, %@", 
       errorMsgFormat, 
       error, 
       [error userInfo]]);
just need:

NSLog(@"%@ %@, %@", 
   errorMsgFormat, 
   error, 
   [error userInfo]);
if we only code:

NSLog([NSString stringWithFormat:@"msg:%@",msg]);
Although it's no problem when execute, but there will be a "format not a string literal and no format arguments" warning.

2013年4月22日 星期一

[iOS] iOS Google Map SDK and route(Google Maps API v3)


 iOS Google Map SDK

Official Website:
https://developers.google.com/maps/documentation/ios/


At first, go to https://developers.google.com/maps/documentation/ios/start download latest version iOS Google Map SDK and build a google map view according to the official teaching.
Rember u need to register to get KEY.

Then we use native code to get the route path and draw on the Google Map

Please come to https://developers.google.com/maps/documentation/javascript/tutorial to get Google Maps API v3 permissions,and prepared your JSON parser(You can find a JSON parser from http://code.google.com/p/json-framework/ witch i used.).
Don't forget to check the API parameter at here https://developers.google.com/maps/documentation/javascript/reference

This is a sample:
NSString *encodedStringFrom =  (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef) @"25,120", NULL, NULL, kCFStringEncodingUTF8);
    NSString *encodedStringTo =  (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef) @"26,121"  NULL, NULL, kCFStringEncodingUTF8);
    NSString * url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false&mode=walking",from,to];
    [encodedStringFrom release];
    [encodedStringTo release];


Use any HTTP GET you liked to send url and get NSString *response to parser:

     id JSONValue = [response  JSONValue];
        if (JSONValue) {
            NSDictionary *dictionary = (NSDictionary *)JSONValue;
            if ([[dictionary objectForKey:@"status"]isEqual:@"OK"]) {
                NSArray *routes = [dictionary objectForKey:@"routes"];
                for (int i = 0; i < [routes count]; i++) {
                    NSDictionary *rout = [routes objectAtIndex:i];
                    NSDictionary *overview_polyline = [rout objectForKey:@"overview_polyline"];
                    NSString *points = [overview_polyline objectForKey:@"points"];
                    if (polyline) {
                        [polyline remove];//remove old route on map
                    }
                    GMSPolylineOptions *polylineOptions = [GMSPolylineOptions options];
                    polylineOptions.path = [self polylineWithEncodedString:points];
                    polylineOptions.color = [UIColor greenColor];
                    polylineOptions.width = 10.f;
                    polylineOptions.geodesic = YES;
                    if (polylineOptions.path != NULL) {
                        self.polyline = [mapViewRoot addPolylineWithOptions:polylineOptions];
                        didGetPath = TRUE;
                    }
                }
            }
        }


polylineWithEncodedString:

- (GMSMutablePath *)polylineWithEncodedString:(NSString *)encodedString {
    const char *bytes = [encodedString UTF8String];
    NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    NSUInteger idx = 0;
    
    GMSMutablePath *path = [GMSMutablePath path];
    float latitude = 0;
    float longitude = 0;
    while (idx < length) {
        char byte = 0;
        int res = 0;
        char shift = 0;
        
        do {
            byte = bytes[idx++] - 63;
            res |= (byte & 0x1F) << shift;
            shift += 5;
        } while (byte >= 0x20);
        
        float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
        latitude += deltaLat;
        
        shift = 0;
        res = 0;
        
        do {
            byte = bytes[idx++] - 0x3F;
            res |= (byte & 0x1F) << shift;
            shift += 5;
        } while (byte >= 0x20);
        
        float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
        longitude += deltaLon;
        
        float finalLat = latitude * 1E-5;
        float finalLon = longitude * 1E-5;
        [path addCoordinate:CLLocationCoordinate2DMake(finalLat, finalLon)];
    }
    return path;
}

remember declare below at .h
id<GMSPolyline> polyline //route
GMSMapView *mapViewRoot //google map