How to draw an arc on Google Maps in iOS?

后端 未结 5 498
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 07:26

How to draw an arc between two coordinate points in Google Maps like in this image and same like facebook post in iOS ?

5条回答
  •  情书的邮戳
    2020-12-11 07:54

    Objective-C version @Rouny answer

    - (void)DrawCurvedPolylineOnMapFrom:(CLLocationCoordinate2D)startLocation To:(CLLocationCoordinate2D)endLocation
    {
        GMSMutablePath * path = [[GMSMutablePath alloc]init];
        [path addCoordinate:startLocation];
        [path addCoordinate:endLocation];
        // Curve Line
        double k = 0.2; //try between 0.5 to 0.2 for better results that suits you
        CLLocationDistance d = GMSGeometryDistance(startLocation, endLocation);
        float h = GMSGeometryHeading(startLocation , endLocation);
    
        //Midpoint position
        CLLocationCoordinate2D p = GMSGeometryOffset(startLocation, d * 0.5, h);
        //Apply some mathematics to calculate position of the circle center
        float x = (1-k*k)*d*0.5/(2*k);
        float r = (1+k*k)*d*0.5/(2*k);
        CLLocationCoordinate2D c = GMSGeometryOffset(p, x, h + -90.0);
    
        //Polyline options
        //Calculate heading between circle center and two points
        float h1 =  GMSGeometryHeading(c, startLocation);
        float h2 = GMSGeometryHeading(c, endLocation);
        //Calculate positions of points on circle border and add them to polyline options
        float numpoints = 100;
        float step = ((h2 - h1) / numpoints);
        for (int i = 0; i < numpoints; i++) {
            CLLocationCoordinate2D pi = GMSGeometryOffset(c, r, h1 + i * step);
            [path addCoordinate:pi];
        }
    
        //Draw polyline
        GMSPolyline * polyline = [GMSPolyline polylineWithPath:path];
        polyline.map = mapView;
        polyline.strokeWidth = 3.0;
        NSArray *styles = @[[GMSStrokeStyle solidColor:kBaseColor],
                            [GMSStrokeStyle solidColor:[UIColor clearColor]]];
    
        NSArray *lengths = @[@5, @5];
    
        polyline.spans = GMSStyleSpans(polyline.path, styles, lengths, kGMSLengthRhumb);
    
        GMSCoordinateBounds * bounds = [[GMSCoordinateBounds alloc]initWithCoordinate:startLocation coordinate:endLocation];
        UIEdgeInsets insets = UIEdgeInsetsMake(20, 20, 20, 20);
        GMSCameraPosition * camera = [mapView cameraForBounds:bounds insets:insets ];
        [mapView animateToCameraPosition:camera];
    
    }
    

提交回复
热议问题