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

2013年5月9日 星期四

[iOS] Log的使用

開發APP時為了debug需要大量的資訊,
可以使用NSLog打印Log。

然而這些Log資訊我們並不想在發佈後仍被印出來,
可在 project-Prefix.pch 中用以下code將NSLog作一個中介的define:
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__)
之後想看情況印的NSLog用DLog取代,
總是要印出的就用ALog取代。
這樣當不想印出DLog時就把 #define DEBUG_MODE 註解掉就行了。





----------------------以下順便提一下NSLog用法-----------------------

為了方便追蹤bug發生地點,
可於每個重要function加入:

- (IBAction)showRangeList:(id)sender {
    DLog(@"%s Enter", __func__);
}
這樣執行至此行時便會打印出如下資訊
-[MainViewController showRangeList:] Enter


另外NSLog本身有format功能
因此不需要寫成

NSLog(@"%@",[NSString stringWithFormat:@"%@ %@, %@", 
       errorMsgFormat, 
       error, 
       [error userInfo]]);
只需要

NSLog(@"%@ %@, %@", 
   errorMsgFormat, 
   error, 
   [error userInfo]);
即可。
若是只寫

NSLog([NSString stringWithFormat:@"msg:%@",msg]);
則雖然編譯沒問題但會出現"format not a string literal and no format arguments"的warning。

2013年4月22日 星期一

[iOS] iOS Google Map SDK 與 路徑導航API(Google Maps API v3)

 iOS Google Map SDK

官方網址:
https://developers.google.com/maps/documentation/ios/


首先到https://developers.google.com/maps/documentation/ios/start頁面下載最新版iOS Google Map SDK並照官方教學建立google map,
這部分由於官方教學已夠清楚不再詳述,
記得要註冊取得授權碼。

接者我們使用原生code去接取路徑並繪製到MAP上

前置工作請到https://developers.google.com/maps/documentation/javascript/tutorial打開Google Maps API v3權限,並取得JSON解析器(這邊使用的JSON解析器可以從http://code.google.com/p/json-framework/取得)
之後稍微看一下各參數定義https://developers.google.com/maps/documentation/javascript/reference

這邊提供簡單的範例:
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];


使用你習慣的HTTP去傳送url並取回NSString *response後進行解析:

     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];//從地圖上移除舊的路線
                    }
                    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;
}


記得要再.h宣告
id<GMSPolyline> polyline //路線
GMSMapView *mapViewRoot //google map