Mapkit绘制路线问题

时间:2013-04-04 11:31:06

标签: iphone ios objective-c mkmapview mapkit

我正在开发一个应用程序,我需要使用lat / long在两点之间绘制路径。

我使用谷歌API获取折线并在解码后绘制它。

问题:

  1. 路线不在路中间(附图image_1)

  2. 虽然放大/缩小路线未完全绘制(附图像_2)

  3. enter image description here

    enter image description here

    enter image description here

    以下是代码:

        if(!routeView)
            routeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.mapView.frame.origin.y, mapView.frame.size.width, self.mapView.frame.size.height)];
        routeView.userInteractionEnabled = NO;
        [mapView addSubview:routeView];
    
        [self.lat1 resignFirstResponder];
        [self.long1 resignFirstResponder];
        [self.lat2 resignFirstResponder];
        [self.long2 resignFirstResponder];
    
        NSString* saddr = [NSString stringWithFormat:@"%@,%@",self.lat1.text,self.long1.text];
    
        NSString* daddr = [NSString stringWithFormat:@"%@,%@",self.lat2.text,self.long2.text];
    
        NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.apple.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr];
    
        NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
    
        NSError *error;
        NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];
    
    
        NSData *responseData = [apiResponse dataUsingEncoding:NSUTF8StringEncoding];
    
    
        NSError* error1;
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
                                                             options:NSJSONReadingMutableLeaves
                                                               error:&error1];
        NSLog(@"Error: %@\n%@",[error1 localizedDescription],[error1 localizedFailureReason]);
    
    
       if([[json objectForKey:@"status"] isEqualToString:@"OK"])
        {
            NSArray *routes1 = [json objectForKey:@"routes"];
            NSDictionary *route = [routes1 lastObject];
    
            if (route)
            {
                NSString *overviewPolyline = [[route objectForKey: @"overview_polyline"] objectForKey:@"points"];
    
                routes = [self decodePolyLine:overviewPolyline];
    
                //NSLog(@"%@",[routes objectAtIndex:0]);
    
                [self updateRouteView];
                [self centerMap];
            }
        }
    
    
    -(void) updateRouteView
    {
     CGContextRef context =     CGBitmapContextCreate(nil,
                                                     routeView.frame.size.width,
                                                  routeView.frame.size.height,
                                                  8,
                                                  4 * routeView.frame.size.width,
                                                  CGColorSpaceCreateDeviceRGB(),
                                                  kCGImageAlphaPremultipliedLast);
    
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 3.0);
    
    for(int i = 0; i < routes.count; i++) {
        CLLocation* location = [routes objectAtIndex:i];
        CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:routeView];
    
        if(i == 0) {
            CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
        } else {
            CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
        }
    }
    
    CGContextStrokePath(context);
    
    CGImageRef image = CGBitmapContextCreateImage(context);
    UIImage* img = [UIImage imageWithCGImage:image];
    
    routeView.image = img;
    CGContextRelease(context);
    
    }
    

1 个答案:

答案 0 :(得分:0)

您的路线未对齐的一个可能原因是您的路线即将到来(http://maps.apple.com/maps重定向到http://maps.google.com/maps)并且您正在Apple的地图上绘制它。他们的数据略有不同。

部分绘制的路线我无法解决,但我建议您添加MKPolyline而不是静态图像叠加。他们发明了这种画线工具是有充分理由的。