在iPhone MapView中,绘制路线部分未获得所需的输出 [英] Drawing route partially not getting required output in iPhone MapView

查看:94
本文介绍了在iPhone MapView中,绘制路线部分未获得所需的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用多段线在两点之间绘制路线。我有些路线,但不正确,我认为它会在转弯时感到困惑。

   - (IBAction)onclickDrawButton:这是我使用google api获取路线的点数。以下是代码我试过的请检查。 (id)发送者

{
flag = TRUE;
NSString * startpoint = [satrtTextfield text];
NSString * endpoint = [endTextfield text];

NSMutableString * urlString = [NSMutableString stringWithFormat:@http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=假时,起始点,终点]。
NSURL * url = [NSURL URLWithString:urlString];


NSURLRequest * request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if(connection)
{
NSLog(@connectin done);
}

}



<$ p (NSURLConnection *)连接didReceiveResponse:(NSURLResponse *)响应
{
if(flag)
{
recievedRoutes = [[NSMutableData alloc] init];






$ b

   - (void)connectionDidFinishLoading:(NSURLConnection *)连接
{
if(flag){
NSString * jsonResult = [[NSString alloc ] initWithData:recievedRoutes encoding:NSUTF8StringEncoding];
NSLog(@json response%@,jsonResult);

NSDictionary * partialJsonDict = [jsonResult JSONValue];
NSArray * items = [partialJsonDict valueForKey:@routes];
$ b $ // NSLog(@responsed valued%@,[[items objectAtIndex:0] valueForKey:@legs]);


NSArray * aary = [[items objectAtIndex:0] valueForKey:@legs];
NSLog(@legs array wuth polyline%@,[[aary objectAtIndex:0] valueForKey:@steps]);
NSArray * steps = [[aary objectAtIndex:0] valueForKey:@steps];
NSLog(@steps%@,[[steps objectAtIndex:1] objectForKey:@polyline]);
NSMutableString * string = [[NSMutableString alloc] init];
for(int i = 0; i< [steps count]; i ++)
{
// NSLog(@steps i value%@,[[[steps objectAtIndex:i] objectForKey:@ 折线] objectForKey:@ sttpoints]);

[string appendString:[[[steps [objectAtIndex:i] objectForKey:@polyline] objectForKey:@points]];
}
NSLog(@final%@,string);

MKPolyline * polyline = [self polylineWithEncodedString:string];
[mapView addOverlay:polyline];
[self zoomToFitMapAnnotations:mapView];


$ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if(flag){
[recievedRoutes appendData:data];
}

}

/ /解码折线和回收协调我已经使用下面的方法

pre $ - (MKPolyline *)polylineWithEncodedString:(NSString *)encodedString {

const char * bytes = [encodedString UTF8String];
NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSUInteger idx = 0;

NSUInteger count = length / 4;
CLLocationCoordinate2D * coords = calloc(count,sizeof(CLLocationCoordinate2D));
NSUInteger coordIdx = 0;

float latitude = 0;
float longitude = 0;
while(idx< length){
char byte = 0;
int res = 0;
char shift = 0;

do {
byte = bytes [idx ++] - 63;
res | =(byte& 0x1F)<<转移;
shift + = 5;
} while(byte> = 0x20); (res& 1)?〜(res>> 1):(res>> 1));

float deltaLat =
latitude + = deltaLat;

shift = 0;
res = 0;

do {
byte = bytes [idx ++] - 0x3F;
res | =(byte& 0x1F)<<转移;
shift + = 5;
} while(byte> = 0x20); (res& 1)?〜(res>> 1):(res>> 1));

float deltaLon =
longitude + = deltaLon;

float finalLat =纬度* 1E-5;
float finalLon = longitude * 1E-5;

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat,finalLon);
coords [coordIdx ++] = coord;
NSLog(@in encoding%f%f,纬度,经度);
if(coordIdx == count){
NSUInteger newCount = count + 10;
coords = realloc(coords,newCount * sizeof(CLLocationCoordinate2D));
count = newCount;
}
}

MKPolyline * polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx];
free(coords);

返回折线;
}

//绘制实际路线

   - (MKOverlayView *)mapView:(MKMapView *)mapView 
viewForOverlay:(id< MKOverlay>)overlay {
MKPolylineView * overlayView = [[ MKPolylineView alloc] initWithOverlay:overlay];
overlayView.lineWidth = 2;
overlayView.strokeColor = [UIColor purpleColor];
overlayView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1f];
返回overlayView;

}



解决方案

p>

  NSArray * steps = [[aary objectAtIndex:0] valueForKey:@steps]; 

用这行代替行可能有用

  NSMutableArray * polyLinesArray = [[NSMutableArray alloc] init]; 

for(int i = 0; i< [steps count]; i ++)
{
NSString * encodedPoints = [[[steps [objectAtIndex:i] objectForKey:@折线] valueForKey:@points];
MKPolyline * route = [self polylineWithEncodedString:encodedPoints];
[polyLinesArray addObject:route];
}

[self.mapView addOverlays:polyLinesArray];;
[polyLinesArray release];


I am trying to draw route between two points using polyline . I got somewhat route but not correct and I think it will get confused at the turn . I am using google api for getting points of route .Following is code what i tried please check .

 - (IBAction)onclickDrawButton:(id)sender

{
    flag=TRUE;
    NSString *startpoint=[satrtTextfield text];    
    NSString *endpoint=[endTextfield text];

    NSMutableString *urlString=[NSMutableString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false",startpoint,endpoint];
    NSURL *url = [NSURL URLWithString:urlString];


    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request 
    delegate:self];
    if(connection)
    {
        NSLog(@"connectin done");
    }

}

 -(void)connection:(NSURLConnection*)connection didReceiveResponse: (NSURLResponse*)response
{    
 if(flag)
 {
    recievedRoutes=[[NSMutableData alloc]init];

 }

}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if(flag){
          NSString *jsonResult = [[NSString alloc] initWithData:recievedRoutes   encoding:NSUTF8StringEncoding];
    NSLog(@"json response %@",jsonResult);

    NSDictionary *partialJsonDict=[jsonResult JSONValue];
    NSArray *items=[partialJsonDict valueForKey:@"routes"];

    //NSLog(@"responsed valued %@",[[items objectAtIndex:0]valueForKey:@"legs"]);


    NSArray *aary=[[items objectAtIndex:0]valueForKey:@"legs"];
    NSLog(@"legs array wuth polyline %@",[[aary objectAtIndex:0]valueForKey:@"steps"]);
    NSArray *steps=[[aary objectAtIndex:0]valueForKey:@"steps"];
    NSLog(@"steps %@",[[steps objectAtIndex:1]objectForKey:@"polyline"]);
    NSMutableString *string=[[NSMutableString alloc]init];
    for(int i=0;i<[steps count];i++)
    {
        //NSLog(@"steps  i value %@",[[[steps objectAtIndex:i]objectForKey:@"polyline"]objectForKey:@"sttpoints"]);

        [string appendString:[[[steps objectAtIndex:i]objectForKey:@"polyline"]objectForKey:@"points"]];
    }
    NSLog(@"final %@",string);

    MKPolyline *polyline=[self polylineWithEncodedString:string];
    [mapView addOverlay:polyline];
    [self zoomToFitMapAnnotations:mapView];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
 {   
   if(flag){
    [recievedRoutes appendData:data];
   }

}

//to decode polyline and retrives coordintes i have used following method

-(MKPolyline *)polylineWithEncodedString:(NSString *)encodedString {

const char *bytes = [encodedString UTF8String];
NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSUInteger idx = 0;

NSUInteger count = length / 4;
CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D));
NSUInteger coordIdx = 0;

float latitude = 0;
float longitude = 0;
while (idx < length) {
    char byte = 0;
    int res = 0;
    char shift = 0;

    do {
        byte = bytes[idx++] - 63;
        res |= (byte & 0x1F) << shift;
        shift += 5;
    } while (byte >= 0x20);

    float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
    latitude += deltaLat;

    shift = 0;
    res = 0;

    do {
        byte = bytes[idx++] - 0x3F;
        res |= (byte & 0x1F) << shift;
        shift += 5;
    } while (byte >= 0x20);

    float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
    longitude += deltaLon;

    float finalLat = latitude * 1E-5;
    float finalLon = longitude * 1E-5;

    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon);
    coords[coordIdx++] = coord;
    NSLog(@"in encoding  %f %f ",latitude,longitude);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
    if (coordIdx == count) {
        NSUInteger newCount = count + 10;
        coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D));
        count = newCount;
    }
}

MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx];
free(coords);

 return polyline;
  }

// to draw actual route

 - (MKOverlayView *)mapView:(MKMapView *)mapView
        viewForOverlay:(id<MKOverlay>)overlay {
MKPolylineView *overlayView = [[MKPolylineView alloc] initWithOverlay:overlay];
overlayView.lineWidth = 2;
overlayView.strokeColor = [UIColor purpleColor];
overlayView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1f];
return overlayView;

}

解决方案

After your line

NSArray *steps=[[aary objectAtIndex:0]valueForKey:@"steps"];   

replace lines with this may work

NSMutableArray *polyLinesArray = [[NSMutableArray alloc] init];

for (int i = 0; i < [steps count]; i++)
{
    NSString* encodedPoints = [[[steps objectAtIndex:i] objectForKey:@"polyline"] valueForKey:@"points"];
    MKPolyline *route = [self polylineWithEncodedString:encodedPoints];
    [polyLinesArray addObject:route];
}

[self.mapView addOverlays:polyLinesArray];
[polyLinesArray release];

这篇关于在iPhone MapView中,绘制路线部分未获得所需的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆