【发布时间】:2012-05-31 14:47:27
【问题描述】:
我正在尝试使用 polyline 在两点之间绘制路线。我得到了一些路线但不正确,我认为它会在转弯时感到困惑。我正在使用 google api 获取路线点。以下是我尝试过的代码,请检查。
- (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];
}
}
//解码折线并检索我使用以下方法的坐标
-(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;
}
//绘制实际路线
- (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;
}
【问题讨论】:
-
我建议你看看你从你的路线中得到的点,你可能得到了一个奇怪的点。您绘制的每条路线都会发生这种情况,还是仅此一种情况?
-
不是每条路线都会发生这种情况。
-
谢谢艾伦,正如你所说,我没有得到正确的路径点。实际上,我正在尝试对折线进行编码并将其附加到以前的轮询中,例如制作单个折线字符串并将此字符串传递给 polylineWithEncodedString 方法,这样就不会得到准确的点。现在为每条折线调用此编码方法,并为每个点添加叠加方法。这对我有用...:)
-
你好 Pavan 你在这里做了什么改变..?我有同样的问题请帮助我。
标签: iphone google-maps mkmapview google-polyline