【问题标题】:how to implement road route navigation on google map in ios如何在ios的谷歌地图上实现道路路线导航
【发布时间】:2015-08-12 10:19:00
【问题描述】:

我要求使用轮流 GPS 导航。任何人都可以告诉如何集成谷歌地图的轮流gps导航。

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

所以请指导我如何在我的应用中使用地图导航。

【问题讨论】:

  • 您可以使用Google Directions API。您可以在 API 请求中简单地提供 origindestination。然后从 API 响应中,您可以构建您的折线。您可以查看此StackOverflow answer,了解如何为 Directions API 发出示例 URL 请求。
  • 我已经轻松准确地完成了这项任务。但现在我的主要问题是如何在驾驶模式下从一个位置导航到另一个位置。
  • 谁能帮我解决我的问题。我被困在这一点上。
  • @vikranttanwar 你有什么解决办法吗??
  • 是的,我得到了解决方案,感谢您的回复

标签: ios google-maps


【解决方案1】:

如果您使用的是 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;
        }
    }];
}

【讨论】:

  • 有人能用swift写这个吗?
【解决方案2】:

对于 swift 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)

【讨论】:

  • 您好,这已经完成了,我期待在生成路线后进行“START NAVIGATION”。我得到了一条折线的路线,但正在等待任何建议继续导航。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
相关资源
最近更新 更多