Draw polyline using google map in iOS 7?

前端 未结 1 1985
说谎
说谎 2021-01-15 03:32

Using this link .

i tried to get route direction from one coordinates to another coordinates but didn\'t get route. Below mention used coding...

             


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-15 03:54

    You can use GoogleMapsDirection to get the path or DirectionResponse from the Google Directions API. The NSLogs are useful to see what you are working with.

    [[GMDirectionService sharedInstance] getDirectionsFrom:origin to:destination        succeeded:^(GMDirection *directionResponse) {   
        if ([directionResponse statusOK]){
            NSLog(@"Duration : %@", [directionResponse durationHumanized]);
            NSLog(@"Distance : %@", [directionResponse distanceHumanized]);
            NSArray *routes = [[directionResponse directionResponse] objectForKey:@"routes"];
            // NSLog(@"Route : %@", [[directionResponse directionResponse] objectForKey:@"routes"]);
    
        }
    } failed:^(NSError *error) {
            NSLog(@"Can't reach the server")
    }];
    

    Once you have the json you can get the path points (this shows the first route at index 0).

    GMSPath *path = [GMSPath pathFromEncodedPath:routes[0][@"overview_polyline"][@"points"]];
    

    Then you can convert the path to a polyline and draw it on your mapView and set the strokeColor and strokeWidth if desired.

    GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
    polyline.strokeColor = [UIColor redColor];
    polyline.strokeWidth = 5.f;
    

    Then set the polyline.map property to your mapView.

    polyline.map = mapView;
    

    Finally put it all together

    [[GMDirectionService sharedInstance] getDirectionsFrom:origin to:destination          succeeded:^(GMDirection *directionResponse) {   
        if ([directionResponse statusOK]){
            NSLog(@"Duration : %@", [directionResponse durationHumanized]);
            NSLog(@"Distance : %@", [directionResponse distanceHumanized]);
            NSArray *routes = [[directionResponse directionResponse] objectForKey:@"routes"];
            // NSLog(@"Route : %@", [[directionResponse directionResponse] objectForKey:@"routes"]);
    
            GMSPath *path = [GMSPath pathFromEncodedPath:routes[0][@"overview_polyline"]  [@"points"]];
            GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
            polyline.strokeColor = [UIColor redColor];
            polyline.strokeWidth = 5.f;
            polyline.map = mapView;
    
        }
    } failed:^(NSError *error) {
            NSLog(@"Can't reach the server")
    }];
    

    I find CocoaPods useful for installing the Google Maps SDK and GoogleMapsDirection.

    0 讨论(0)
提交回复
热议问题