【问题标题】:With MapKit should I use Directions or make my own Overlays of routes?使用 MapKit,我应该使用方向还是自己制作路线叠加?
【发布时间】:2015-12-03 10:48:30
【问题描述】:

我是任何编程的绝对初学者,但我正在尝试通过使用 MapKit 制作一个在地图上显示(我的!)路线的 iOS 应用程序来学习。沿途有带有特定信息的注释。

我有两个问题。

  1. 什么更好/更容易,尝试在注释之间使用 MapKit 方向,以便 MapKit 显示沿着道路的路线(我想要)(我必须在每个注释之间获取方向,否则路线不会总是我想展示的那个!)或以某种方式制作我的路线的叠加 png 图像并使用它?

  2. 如果使用 Overlay 效果更好,我该如何制作并确保它位于正确的位置并可以放大和缩小?

我找到了很多关于如何添加叠加层的教程,但似乎找不到太多关于如何制作可在应用中使用的叠加层的教程。

【问题讨论】:

    标签: iphone swift mapkit mkoverlay directions


    【解决方案1】:

    在地图上设置图像是实现此功能的不好方法。对于路径绘制,Apple 提供了 MKPolyline。这将解决您在缩放因子方面保持其位置的所有顾虑。

    请查看以下折线图

    iPhone: How to draw line between two points on MapKit?

    http://pinkstone.co.uk/how-to-draw-an-mkpolyline-on-a-map-view/

    最终,您的目标是通过 In-Built Direction API 或 Google Direction API 等第三方 API 以某种方式获得中间位置的 Lat-long。

    枚举这些位置并在地图上绘制折线。如果您将来尝试使用 Google 地图,也会出现相同的逻辑和情况。只是方法和类将被更改。

    - (void)drawLine {
    
        // remove polyline if one exists
        [self.mapView removeOverlay:self.polyline];
    
        // create an array of coordinates from allPins
        CLLocationCoordinate2D coordinates[self.allPins.count];
        int i = 0;
        for (Pin *currentPin in self.allPins) {
            coordinates[i] = currentPin.coordinate;
            i++;
        }
    
        // create a polyline with all cooridnates
        MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:self.allPins.count];
        [self.mapView addOverlay:polyline];
        self.polyline = polyline;
    
        // create an MKPolylineView and add it to the map view
        self.lineView = [[MKPolylineView alloc]initWithPolyline:self.polyline];
        self.lineView.strokeColor = [UIColor redColor];
        self.lineView.lineWidth = 5;
    
    }
    

    编辑

    如果您想使用 GOOGLE API 和 GOOGLE MAP 绘图

    -(void)drawPathFrom:(CLLocation*)source toDestination:(CLLocation*)destination{    
    
        NSString *baseUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true", source.coordinate.latitude,  source.coordinate.longitude, destination.coordinate.latitude,  destination.coordinate.longitude];
    
        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"];
                if([routes isKindOfClass:[NSArray class]] && routes.count>0){
                    NSDictionary *firstRoute    = [routes objectAtIndex:0];
                    NSString *encodedPath       = [firstRoute[@"overview_polyline"] objectForKey:@"points"];
    
                    GMSPolyline *polyPath       =  [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:encodedPath]];
                    polyPath.strokeColor        = [UIColor blueColor];
                    polyPath.strokeWidth        = 3.5f;
                    polyPath.map                = _mapView;
    
                }
            }
        }];
    
    }
    

    【讨论】:

    • 非常感谢 Mrug!我想我需要使用 Apple 的指示(或者如果我会使用 Goole Map),以便让折线跟随道路。没有方向,它只是两点之间的直线。或者你的意思是我应该有这么多的点,折线会跟随道路?
    • 是的,这完全取决于您从哪里获取路线点。 Apple 或 Google API。我给出的解决方案是画线连接坐标。
    • 结帐编辑..我也添加了谷歌代码。
    • 非常感谢!!会尝试的。非常感谢您的帮助!
    • 我知道,但我才开始在这个网站上,需要 15 分,我相信在我的选票之前...非常抱歉!我确实投票了,但它没有变得可见。
    猜你喜欢
    • 2014-07-15
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2011-06-02
    • 2011-03-23
    • 1970-01-01
    相关资源
    最近更新 更多