简体   繁体   中英

MapKit route over custom pin & callout

I have customized pins in my mapview with custom callout.

While drawing path on map, the route is coming over callout & pins. Image Attached.

I have used google API to get polylines and drawing it after decoding it.

在此处输入图片说明

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

}

I think your problem is that you add your routeView after adding your pin, so the routeview is the first subview. What happens if you try to add the routeView first, in your viewDidLoad method for example ?

Thanks for the responses. I have figure this out using MKPolylines. Refer code below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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