繁体   English   中英

在iOS中使用Google Maps上的路线

[英]Directions on Google Maps in IOS

我已经开始阅读有关Google地图的信息,自最近三天以来,我已经能够实现某些功能。 除此之外,我还必须阅读有关Web服务的基础知识,因此我已经阅读了有关使用AFNetworking库的信息。 现在,我已经能够实现的功能是:

  • 实施谷歌地图
  • 学习访问网络服务
  • 在地图上的两点之间绘制路径
  • 在地图上绘制路径,包括航点

这是此实现的代码:

//Showing Map         
{
    GMSCameraPosition *cameraPosition = [GMSCameraPosition 
        cameraWithLatitude:-33.86 longitude:151.20 zoom:6];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];
    mapView_.myLocationEnabled = YES;
    mapView_.mapType = kGMSTypeNormal;
    mapView_.settings.myLocationButton = YES;

    mapView_.delegate = self;
    self.view = mapView_;

    //Showing marker    
    GMSMarker *marker = [GMSMarker new];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.title = @"Sydney";
    marker.snippet = @"Australia";
    marker.map = mapView_;
    [self GETRequest];//method to implement google maps api
}

这是我的方法“ GETRequest”的实现:

-(void)GETRequest{

    NSURL  *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/directions/json?origin=-33.86,151.20&destination=-32.90,151.80&waypoints=-33.30,151.50"];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:urlRequest];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject){

        NSLog(@"success");
        NSLog(@"%@",responseObject);
        GMSMutablePath *path = [GMSMutablePath path];
        [path addCoordinate:CLLocationCoordinate2DMake(-33.86, 151.20)];
        [path addCoordinate:CLLocationCoordinate2DMake(-25.30, 140.50)];
        [path addCoordinate:CLLocationCoordinate2DMake(-32.90, 151.80)];

        GMSPolyline *line = [GMSPolyline polylineWithPath:path];
        line.strokeWidth = 2;
        line.map = mapView_;
        self.view = mapView_;
} failure

:^(AFHTTPRequestOperation *operation,id responseObject){


    NSLog(@"failure");

}];

[operation start];

这是我的输出的屏幕截图:

现在我的问题是:路线是否总是像这些直线一样绘制? 我想查看具体的路线,但是路线说明的google API没有提供与此相关的任何代码或说明。

屏幕截图中的路径实际上是由以下代码显式绘制的:

    [path addCoordinate:CLLocationCoordinate2DMake(-33.86, 151.20)];
    [path addCoordinate:CLLocationCoordinate2DMake(-25.30, 140.50)];
    [path addCoordinate:CLLocationCoordinate2DMake(-32.90, 151.80)];

您进行的呼叫会传回JSON字串。 您应该从那里解析所有坐标(在这种情况下,大约为110个),并使用解析后的坐标绘制路径。

您也可以在浏览器中打开Goog​​le网址以查看数据。 https://maps.googleapis.com/maps/api/directions/json?origin=-33.86,151.20&destination=-32.90,151.80&waypoints=-33.30,151.50

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM