简体   繁体   English

Mapkit绘制路线问题

[英]Mapkit draw route problems

I am developing an application in which I need to draw a route between two points using lat/long. 我正在开发一个应用程序,其中需要使用纬度/经度在两点之间绘制路线。

I have used google API to get polylines and drawing it after decoding it. 我已经使用谷歌API来获取折线并在对其进行解码后进行绘制。

Problems: 问题:

  1. Route is not in the middle of the road (Attached image_1) 路线不在路中间(附有图片_1)

  2. While zooming in/out route doesn't draw completely (Attached image_2) 放大/缩小路线无法完全绘制时(附有image_2)

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Below is the code: 下面是代码:

    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);

}

One possible reason that your route is misaligned is that your route is coming rom Google ( http://maps.apple.com/maps redirects to http://maps.google.com/maps ) and you are drawing it on Apple's maps. 路线未对齐的一种可能原因是您的路线来自Google( http://maps.apple.com/maps重定向到http://maps.google.com/maps ),并且正在Apple的地图上绘制。 Their data is slightly different. 他们的数据略有不同。

The partially drawn route I can't solve, but I would suggest you add an MKPolyline rather than a static image overlay. 我无法解决部分绘制的路线,但我建议您添加MKPolyline而不是静态图像叠加层。 They invented that line drawing tool for a good reason. 他们有充分的理由发明了这种画线工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM