如何在ios中的谷歌地图上实现道路路线导航

时间:2015-08-12 10:19:00

标签: ios google-maps

我要求使用转弯GPS导航。任何人都可以告诉如何整合谷歌地图转向gps导航。

建议我有关此问题的任何文档。我不想使用URL Scheme进行导航。

因此,请指导我如何在我的应用中使用地图导航。

2 个答案:

答案 0 :(得分:3)

如果您使用的是Google Maps SDK,请使用其方法显示两个地点之间的路径:

-(void)drawPathFrom{

    NSString *baseUrl = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=true", @"Safdarjung enclave South delhi", @"chandni chownk new delhi"];

    NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"Url: %@", url);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if(!connectionError){
            NSDictionary *result        = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSArray *routes             = [result objectForKey:@"routes"];
            NSDictionary *firstRoute    = [routes objectAtIndex:0];
            NSString *encodedPath       = [firstRoute[@"overview_polyline"] objectForKey:@"points"];

            GMSPolyline *polyPath       = [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:encodedPath]];
            polyPath.strokeColor        =aNumber;
            polyPath.strokeWidth        = 5.5f;
            polyPath.map                = mapView;
        }
    }];
}

答案 1 :(得分:0)

快速4。

尽管问题已经很久了。

func routingLines(origin: String,destination: String){
    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=\(googleapi)"


    Alamofire.request(url).responseJSON { response in

        let json = response.result.value as! NSDictionary
        let routes = json["routes"] as! NSArray

        for route in routes
        {
            let values = route as! NSDictionary

            let routeOverviewPolyline = values["overview_polyline"] as! NSDictionary
            let points = routeOverviewPolyline["points"] as! String
            let path = GMSPath.init(fromEncodedPath: points)

            let polyline = GMSPolyline(path: path)
            polyline.strokeColor = .black
            polyline.strokeWidth = 2.0
            polyline.map = self.mapView //where mapView is your @IBOutlet which is in GMSMapView!


        }
    }
}

用法:

let ori = "6.538729, 3.379302"
let dest = "6.444445, 3.402727"
routingLines(origin: ori,destination: dest)