2013-04-10 2 views
2

사용자 정의 콜 아웃을 사용하여 내 mapview에 핀을 사용자 정의했습니다.맞춤 핀 및 콜 아웃을 통한 MapKit 경로

지도의 경로를 그리는 동안 경로는 설명 선인 & 핀을 전달합니다. 첨부 된 이미지.

Google 코드를 사용하여 폴리 라인을 만들고 디코딩 한 후 그립니다. 여기

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

} 

답변

0

나는 당신의 문제는 당신이 당신의 핀을 추가 한 후 routeView를 추가하는 것이 생각, 그래서 routeview 첫 번째 서브 뷰입니다. 예를 들어 viewDidLoad 메서드에서 routeView를 먼저 추가하려고하면 어떻게됩니까?

0

감사합니다. 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]; 
+0

이 방법이 효과가 있습니까? –