Objective c 如何在google map sdk iOS中绘制从一个地方到另一个地方的路径?

Objective c 如何在google map sdk iOS中绘制从一个地方到另一个地方的路径?,objective-c,iphone,google-maps,google-maps-sdk-ios,Objective C,Iphone,Google Maps,Google Maps Sdk Ios,我需要在谷歌地图中找到从一个地方到另一个地方的路径。如何使用google map sdk iOS绘制方向图 你们中的任何一个都能给出源代码。然后解释如何实现这一点 下面我还附上了图片,我需要在iPhone应用程序中使用谷歌地图SDK iOS实现这一点。 谢谢,我希望这会对你有所帮助。这是来自谷歌api。绘制多段线,并在其中指定长度为lat的路径 -(无效)showRoute:(id)routeDict{ NSString*pointStr=[[[routeDict objectForKey:@

我需要在谷歌地图中找到从一个地方到另一个地方的路径。如何使用google map sdk iOS绘制方向图

你们中的任何一个都能给出源代码。然后解释如何实现这一点

下面我还附上了图片,我需要在iPhone应用程序中使用谷歌地图SDK iOS实现这一点。


谢谢,

我希望这会对你有所帮助。这是来自谷歌api。绘制多段线,并在其中指定长度为lat的路径

-(无效)showRoute:(id)routeDict{
NSString*pointStr=[[[routeDict objectForKey:@“routes”]objectAtIndex:0]objectForKey:@“overview_polyline”]objectForKey:@“points”];
浮动最大长度=0.0;
浮动最小长度=0.0;
浮动最大值=0.0;
浮动最小值=0.0;
NSMutableArray*routeArr=[自解码多段线:pointStr];
CLLocationCoordinate2D通勤点坐标[[routeArr count]];
CLLocation*loc;
如果([routeArr计数]){
loc=[routeArr对象索引:0];
最大长度=位置坐标经度;
min_long=位置坐标经度;
最大纬度=位置坐标纬度;
最小纬度=位置坐标纬度;
}
对于(int i=0;i max_lat){
最大纬度=位置坐标纬度;
}
如果(位置坐标纬度<最小纬度){
最小纬度=位置坐标纬度;
}
如果(位置坐标经度>最大长度){
最大长度=位置坐标经度;
}
if(位置坐标经度<最小长度){
min_long=位置坐标经度;
}
}
MKPolyline*overflowRoutePolygon=[MKPolyline polyline-polylineWithCoordinates:communierlotcoords count:[routeArr count]];
[mapView addOverlay:overflowRoutePolygon];
//NSLog(@“%f%f%f%f”、最小纬度、最大纬度、最小长度、最大长度);
如果(max|u lat==0.0 | | min|u lat==0.0 | | max|u long==0.0 | | min|u long==0.0){
}否则{
//计算地图中心
浮动中心长度=(最大长度+最小长度)/2;
浮动中心高度=(最大高度+最小高度)/2;
//计算三角洲
浮动增量=最大值-最小值+.00032;
浮动增量=最大长度-最小长度+0.00032;
//NSLog(@“%f%f%f”、中纬度、中长、德尔塔拉特、德尔塔隆);
//创建新区域并设置地图
ClLocationCoordinated2D堇酸盐;
cordinate.纬度=中心纬度;
cordinate.longitude=中心长度;
MKCoordinateSpan=MKCoordinateSpanMake(deltaLat,deltaLong);
MKCoordinateRegion={cordinate,span};
[地图视图设置区域:区域];
}
}
-(无效)展示路线
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
浮动源LAT=0.00;
浮动源长=0.00;
浮动目标=0.00;
浮动目标长=0.00;
id记录=[routeArray对象索引:selectedIndex];
sourceLat=[[[record objectForKey:@“To”]objectForKey:@“Latitude”]objectForKey:@“text”]floatValue];
sourceLong=[[[record objectForKey:@“To”]objectForKey:@“经度”]objectForKey:@“text”]floatValue];
destinationLat=[[[record objectForKey:@“From”]objectForKey:@“Latitude”]objectForKey:@“text”]floatValue];
destinationLong=[[[record objectForKey:@“From”]objectForKey:@“经度”]objectForKey:@“text”]floatValue];
如果(sourceLat==0.00 | | sourceLong==0.00 | | destinationLat==0.00 | | destinationLong==0.00){
UIAlertView*警报=[[UIAlertView alloc]initWithTitle:@“方向错误”
消息:@“您的一个目的地无效。”
代表:赛尔夫
取消按钮:@“确定”
其他按钮:无];
[警报显示];
[警报发布];
返回;
}
NSString*urlStr=[NSString stringWithFormat:@]http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&目标=%f,%f&传感器=false“,
sourceLat,sourceLong,
目的地,目的地长];
NSLog(@“urlStr:%@”,urlStr);
NSData*data=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:urlStr]];
//NSLog(@“方向响应:%@,[[NSString alloc]initWithData:数据编码:NSUTF8StringEncoding]);
JSONDecoder*JSONKITDEXCODER=[JSONDecoder];
NSMutableDictionary*RouteDigic=[[JSONKITDecommer objectWithData:data]copy];
如果(![[routeDic objectForKey:@“状态”]uppercaseString]IseQualtString:@“确定”]){
UIAlertView*警报=[[UIAlertView alloc]initWithTitle:@“方向错误”
消息:@“无法从默认位置找到方向。”
代表:赛尔夫
取消按钮:@“确定”
其他按钮:无];
[警报显示];
[警报发布];
[池释放];
返回;
}
[self-performSelectorOnMainThread:@selector(showRoute:)with object:routeDic waitUntilDone:YES];
[池释放];
返回;
}
-(NSMutableArray*)解码多段线:(NSString*)编码器
{
NSMutableString*encoded=[[NSMutableString alloc]initWithCapacity:[EncodedStrength]];
[encoded appendString:encodedStr];
[encoded replaceCurrencesofString:@“\\\\”和字符串:@“\\\”选项:NSLiteralSearch范围:NSMakerRange(0,[编码长度]);
NSInteger len=[编码长度];
NSInteger指数=0;
NSMutableArray*数组=[[[NSMutableArray alloc]init]autorelease];
NSInteger-lat=0;
NSInteger液化天然气=0;
while(指数-(void)showRoute:(id)routeDict {
    NSString *pointStr = [[[[routeDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"];

    float max_long = 0.0;
    float min_long = 0.0;

    float max_lat = 0.0;
    float min_lat = 0.0;

    NSMutableArray *routeArr = [self decodePolyLine:pointStr];
    CLLocationCoordinate2D commuterLotCoords[[routeArr count]];
    CLLocation *loc;
    if ([routeArr count]) {
        loc = [routeArr objectAtIndex:0];
        max_long = loc.coordinate.longitude;
        min_long = loc.coordinate.longitude;

        max_lat = loc.coordinate.latitude;
        min_lat = loc.coordinate.latitude;
    }

    for (int i=0; i<[routeArr count]; i++) {
        CLLocation *loc = [routeArr objectAtIndex:i];
        commuterLotCoords[i] = loc.coordinate;

        if (loc.coordinate.latitude > max_lat) {
            max_lat = loc.coordinate.latitude;
        }
        if (loc.coordinate.latitude < min_lat) {
            min_lat = loc.coordinate.latitude;
        }


        if (loc.coordinate.longitude > max_long) {
            max_long = loc.coordinate.longitude;
        }
        if (loc.coordinate.longitude < min_long) {
            min_long = loc.coordinate.longitude;
        }




    }

    MKPolyline *overflowRoutePolygon = [MKPolyline polylineWithCoordinates:commuterLotCoords count:[routeArr count]];

    [mapView addOverlay:overflowRoutePolygon];

    //NSLog(@"%f %f %f %f",min_lat,max_lat,min_long,max_long);
    if ( max_lat == 0.0 || min_lat == 0.0 || max_long == 0.0 || min_long == 0.0 ) {
    } else {
        //calculate center of map
        float center_long = (max_long + min_long) / 2;
        float center_lat = (max_lat + min_lat) / 2;

        //calculate deltas
        float deltaLat = max_lat - min_lat + .00032;
        float deltaLong = max_long - min_long + .00032;
        //NSLog(@"%f %f %f %f",center_lat,center_long,deltaLat,deltaLong);

        //create new region and set map
        CLLocationCoordinate2D cordinate;
        cordinate.latitude = center_lat;
        cordinate.longitude = center_long;
        MKCoordinateSpan span = MKCoordinateSpanMake(deltaLat, deltaLong);
        MKCoordinateRegion region = {cordinate, span};
        [mapView setRegion:region];
    }
}

-(void)showRoute 
{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    float sourceLat = 0.00;
    float sourceLong = 0.00;
    float destinationLat = 0.00;
    float destinationLong = 0.00;
    id record = [routeArray objectAtIndex:selectedIndex];
    sourceLat = [[[[record objectForKey:@"To"] objectForKey:@"Latitude"] objectForKey:@"text"] floatValue];
    sourceLong =  [[[[record objectForKey:@"To"] objectForKey:@"Longitude"] objectForKey:@"text"] floatValue];

    destinationLat = [[[[record objectForKey:@"From"] objectForKey:@"Latitude"] objectForKey:@"text"] floatValue];
    destinationLong =  [[[[record objectForKey:@"From"] objectForKey:@"Longitude"] objectForKey:@"text"] floatValue];

    if ( sourceLat == 0.00 || sourceLong == 0.00 || destinationLat == 0.00 || destinationLong == 0.00 ) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Direction Error"
                                                        message: @"One of your destination is not valid."
                                                       delegate: self
                                              cancelButtonTitle: @"OK"
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
        return;
    }

    NSString *urlStr = [NSString stringWithFormat: @"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false",
                        sourceLat, sourceLong,
                        destinationLat,destinationLong];
    NSLog(@"urlStr : %@",urlStr);
    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlStr]];
    //NSLog(@"direction response : %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

    JSONDecoder *jsonKitDecoder = [JSONDecoder decoder];
    NSMutableDictionary *routeDic = [[jsonKitDecoder objectWithData:data] copy];
    if ( ![[[routeDic objectForKey:@"status"] uppercaseString] isEqualToString:@"OK"] ) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Direction Error"
                                                        message: @"Not able to find direction from your default location."
                                                       delegate: self
                                              cancelButtonTitle: @"OK"
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
        [pool release];
        return;
    }

    [self performSelectorOnMainThread:@selector(showRoute:) withObject:routeDic waitUntilDone:YES];

    [pool release];
    return;
}

-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr

{

    NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
    [encoded appendString:encodedStr];
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
    NSInteger lat = 0;
    NSInteger lng = 0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitute = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
        NSNumber *longitute = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
        CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitute floatValue] longitude:[longitute floatValue]] autorelease];

        [array addObject:loc];
    }
    [encoded release];
    return array;
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation


{
    HAnnotation *ann = (HAnnotation *)annotation;
    static NSString *AnnotationViewID = @"annotationViewID";

    HAnnotationView *annotationView = (HAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil)
    {
        annotationView = [[[HAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID type:ann.type] autorelease];

    } 
    annotationView.type = ann.type;
    annotationView.annotation = annotation;
    [annotationView setNeedsDisplay];
    return annotationView;
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views

{


    MKAnnotationView *aV;
    for (aV in views) {
        MKAnnotationView* annotationView = aV;
        annotationView.canShowCallout = NO;
    }
}



- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay

{


    if( [overlay isKindOfClass:[MKPolyline class]] ) {
        MKPolylineView *view = [[MKPolylineView alloc] initWithOverlay:overlay];
        view.lineWidth=2;
        view.strokeColor=[UIColor blueColor];
        view.fillColor=[[UIColor blueColor] colorWithAlphaComponent:0.5];
        return [view autorelease];
    } else {
        MKPolygonView *view = [[MKPolygonView alloc] initWithOverlay:overlay];
        view.lineWidth=2;
        view.strokeColor=[UIColor yellowColor];
        view.fillColor=[[UIColor yellowColor] colorWithAlphaComponent:0.3];
        return [view autorelease];
    }
    return nil;
}