vikrant tanwar
vikrant tanwar

Reputation: 319

how to implement road route navigation on google map in ios

My requirement to use turn by turn GPS navigation. Can anybody tell how to integrate google map for turn by turn gps navigation.

Suggest me any documentation regarding this issue. I don't want to use URL Scheme for navigation.

So please guide me how can I use map navigation in my app.

Upvotes: 1

Views: 2040

Answers (2)

stry-kai
stry-kai

Reputation: 493

For swift 4.

Although question is quite old.

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!


        }
    }
}

Usage:

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

Upvotes: 1

Jatiendar Kumar
Jatiendar Kumar

Reputation: 267

If you are using Google Maps SDK then use its method to show the path between two places:

-(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;
        }
    }];
}

Upvotes: 3

Related Questions