2013-04-04 172 views
1

我正在開發一個應用程序,我需要使用lat/long在兩點之間繪製路線。Mapkit繪製路徑問題

我已經使用谷歌API獲取折線並解碼後繪製它。

問題:

  1. 路線是不是在路上(附IMAGE_1)的中間

  2. 雖然放大/縮小路線不徹底劃清(附IMAGE_2)

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

} 
+0

使用maps.apple.com合法使用地圖工具包,因爲它重定向到谷歌地圖? – 2013-06-09 07:25:59

回答

0

一個可能的原因是你的路線是錯位的是,你的路線是未來ROM谷歌(http://maps.apple.com/maps重定向到http://maps.google.com/maps)和你上繪製它蘋果的地圖。他們的數據稍有不同。

我無法解決部分繪製的路線,但我建議您添加MKPolyline而不是靜態圖像疊加層。他們發明了這種線條繪畫工具是有原因的。

+0

謝謝克雷格。使用MKPolyline解決了過度圖像問題,但路線錯位仍然存在。 – DAddict 2013-04-12 05:27:25