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

沒有留言:

張貼留言