谷歌地图在iOS中按照设备方向绘制折线

时间:2015-01-19 10:47:58

标签: ios iphone google-maps cllocationmanager google-polyline

我正在做一个使用Google地图方向API的导航应用程序,我要求Google Api使用“驾驶模式”在两个坐标之间绘制路径,然后我在地图上绘制它。我有一个问题,当我开车时,我重新计算从我当前位置到目的地位置的路径,但是我得到的路径没有跟随我的设备方向,当我朝双向行驶时它朝相反的方向运行路。

我没有找到任何解决办法。 (你可以在这个链接上看到我用iphone做的屏幕截图:https://imageshack.com/my/images

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

Thanks for your reply, i did this: 
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{

    if (newHeading.headingAccuracy < 0)
        return;

    // Use the true heading if it is valid.
    CLLocationDirection  theHeading = ((newHeading.trueHeading > 0) ?
                                       newHeading.trueHeading : newHeading.magneticHeading);

    [map.mapView animateToBearing:currenHeading];
    userLocation = manager.location;
    markerUserLocation.position = CLLocationCoordinate2DMake(userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    markerUserLocation.map=map.mapView;
    [map.mapView animateToViewingAngle:50];
    [map.mapView animateToZoom:19];

    currenHeading = theHeading;
    if(etapes.count > 0)
    {
    //Here i start navigation        
     [self NavigateToLeg:etapes];

    }

}

and the function NavigatToLeg: 

 -(void)NavigateToLeg:(NSMutableArray *)Steps {
polyline.map = nil;
        polyline = nil;

        NSDictionary *routes = [json objectForKey:@"routes"][0];
        NSDictionary *route = [routes objectForKey:@"overview_polyline"];
        NSString *overview_route = [route objectForKey:@"points"];

        /******************************** ON TRACE LE CHEMIN ************************************/
        GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
        polyline = [GMSPolyline polylineWithPath:path];
        polyline.strokeWidth = 10.f;
        polyline.strokeColor = [UIColor orangeColor];
        polyline.map = map.mapView;
        /******************************** FIN ************************************/

        /******************************** ON RECUPERE LES ETAPES ET ON LANCE LA NAVIGATION ************************************/

        // on récupère les indications avec leurs coordonnées :
        NSArray * legs = [routes objectForKey:@"legs"];
        etapes = [[legs objectAtIndex:0] objectForKey:@"steps"];
 NSLog(@"ETAPES COUNT = %d",etapes.count);
    NSLog(@"ETAPES NUMERO = %d",etapeCount);


    if(etapeCount < etapes.count){
    leg = [self getLegFromSteps:Steps forIndex:etapeCount];
    distanceLabel.text = [NSString stringWithFormat:@"%d m",[self roundUpDistance:(int)[userLocation distanceFromLocation:leg.endLoc]]];
    //[NSString stringWithFormat:@"%d m",[self roundUpDistance:leg.distance]];


    NSLog(@"Polyline Points: %@",leg.polyline.path);

    leg.polyline.strokeWidth = 10.f;
    leg.polyline.strokeColor = [UIColor blueColor];
    leg.polyline.map = map.mapView;
    //[map.mapView animateToLocation:userLocation.coordinate];
    [map.mapView animateToViewingAngle:50];
    [map.mapView animateToZoom:19];

}

在这样做的过程中,Path将遵循我的指示?

感谢。