【问题标题】:How to create PolyLine in MapView IOS如何在 MapView IOS 中创建折线
【发布时间】:2016-10-17 09:54:36
【问题描述】:

我已经创建了一个项目。我想通过谷歌方向 URL 在 MapView 中绘制一条折线。我尝试了许多教程和链接,但没有成功绘制折线。请建议任何教程,如何绘制折线。请帮忙。谢谢你

【问题讨论】:

    标签: ios objective-c google-maps


    【解决方案1】:
    GMSPolyline *poly = [GMSPolyline polylineWithPath:path];
    poly.strokeColor = [UIColor purpleColor];
    poly.tappable = TRUE;
    poly.map = self.googleMapView;
    

    对于带有谷歌地图的项目,请参见:

    https://developers.google.com/maps/documentation/ios-sdk/

    【讨论】:

    • 请建议如何调用url以及如何解密和画线后
    • @DivankKumawat 去谷歌网站我给你一个网址,有很多例子
    【解决方案2】:

    如果您使用的是 GOOGLE 地图,我更喜欢这种直截了当的方式。

    GMSPolyline *polyPath = [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:encodedPath]];
    

    以下是完整的代码sn-p。

    -(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"];
            NSDictionary *firstRoute    = [routes objectAtIndex:0];
            NSString *encodedPath       = [firstRoute[@"overview_polyline"] objectForKey:@"points"];
    
            GMSPolyline *polyPath       = [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:encodedPath]];
            polyPath.strokeColor        = [UIColor redColor];
            polyPath.strokeWidth        = 3.5f;
            polyPath.map                = _mapView;
        }
    }];}
    

    【讨论】:

    • 我建议用[[NSURLSession sharedSession] dataTaskWithRequest:...]替换已弃用的[NSURLConnection sendAsynchronousRequest:...]
    • @sandy 如何在源代码中添加纬度?
    • 我这样添加:NSString *baseUrl = [NSString stringWithFormat:@"maps.googleapis.com/maps/api/directions/…", 24.4764, 74.8624, 24.4748, 75.1404];我说得对吗?
    【解决方案3】:

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

    - (void) drawRoute:(NSArray *) path {
        NSInteger numberOfSteps = path.count;
    
        CLLocationCoordinate2D coordinates[numberOfSteps];
        for (NSInteger index = 0; index < numberOfSteps; index++) {
            CLLocation *location = [path objectAtIndex:index];
            CLLocationCoordinate2D coordinate = location.coordinate;
            coordinates[index] = coordinate;
        }
        MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
        [map addOverlay:polyLine];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      相关资源
      最近更新 更多