MapKit 通过自定义引脚和标注路由

Posted

技术标签:

【中文标题】MapKit 通过自定义引脚和标注路由【英文标题】:MapKit route over custom pin & callout 【发布时间】:2013-04-10 07:17:01 【问题描述】:

我在我的地图视图中使用自定义标注自定义了图钉。

在地图上绘制路径时,路线会经过标注和图钉。附上图片。

我已经使用google API获取折线并在解码后绘制它。

代码如下:

 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】:

我认为您的问题是您在添加 pin 后添加了 routeView,因此 routeview 是第一个子视图。如果您尝试首先在 viewDidLoad 方法中添加 routeView 会发生什么?

【讨论】:

【参考方案2】:

感谢您的回复。我已经使用 MKPolylines 解决了这个问题。参考下面的代码

NSInteger numberOfSteps = routes.count;

CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) 
    CLLocation *location = [routes objectAtIndex:index];
    CLLocationCoordinate2D coordinate = location.coordinate;

    coordinates[index] = coordinate;


MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mapView addOverlay:polyLine];

【讨论】:

以上是关于MapKit 通过自定义引脚和标注路由的主要内容,如果未能解决你的问题,请参考以下文章

iPhone Mapkit 将自定义图像和引脚添加到注释中

引脚上方的标注

如何在地图视图中添加自定义标注视图

地图套件:自定义地标

尝试自定义注释引脚

带有默认地图视图引脚的 Swift Mapview 自定义调出视图