简体   繁体   中英

how to draw an arrow between two points on the map? MapKit

How to draw an arrow between two points on the map?
I try calc latitude and longitude, but something goes wrong.

Best regards, Max

This is my code and result picture

        int currpoints1 = 2;
    NSLog(@"!!!!!!!!%d ",numPoints);
    while(currpoints1 < numPoints)
    {

        TrackPoint* current = nil;
        CLLocationCoordinate2D* coordsArrrow = malloc((3) * sizeof(CLLocationCoordinate2D));
        for (int i =currpoints1, j=0; i < numPoints; i=i+1, j++)
        {
            current = [cashpoints objectAtIndex:i];
            coordsArrrow[j] = current.coordinate;
            if (i % 2 !=0) {
                int Gug = 30;

                int ug;
                float bx, by, ex, ey;
                bx = coordsArrrow[0].latitude;by = coordsArrrow[0].longitude;
                ex = coordsArrrow[1].latitude;ey = coordsArrrow[1].longitude;
                float Lstr = sqrt((ex-ey)*(ex-ey)+(bx-by)*(bx-by));
                ug = [self RetGradW:(abs(coordsArrrow[1].latitude-coordsArrrow[0].latitude)) height:abs(coordsArrrow[1].longitude-coordsArrrow[0].longitude)];
                ug = ug - Gug;
                coordsArrrow[0].latitude  = ex;
                coordsArrrow[0].longitude = ey;
                coordsArrrow[1].latitude  = ex+Lstr*cos(ug*M_PI/180);
                coordsArrrow[1].longitude = ey+Lstr*sin(ug*M_PI/180);
                ug=ug+2*Gug;
                coordsArrrow[2].latitude  = ex+Lstr*cos(ug*M_PI/180);
                coordsArrrow[2].longitude = ey+Lstr*sin(ug*M_PI/180);

                MKPolyline *points = [MKPolyline polylineWithCoordinates:coordsArrrow count:3];
                points.subtitle = @"arrow";
                [map addOverlay:points];
                break;
            }
        }
        free(coordsArrrow);
        currpoints1 = currpoints1 +14;
    }

在此处输入图片说明

Oh guys. I change other way. Make annotation and rotate with direction between two point.

metods :

calc direction:

     int currpoints1 = 2;
    NSLog(@"!!!!!!!!%d ",numPoints);
    while(currpoints1 < numPoints)
    {

        TrackPoint* current = nil;
        CLLocationCoordinate2D* coordsArrrow = malloc((2) * sizeof(CLLocationCoordinate2D));
        for (int i =currpoints1, j=0; i < numPoints; i=i+1, j++)
        {
            current = [cashpoints objectAtIndex:i];
            coordsArrrow[j] = current.coordinate;
            if (i % 2 !=0) {
                DirectAnnotation *placemark=[[DirectAnnotation alloc] initWithCoordinate: coordsArrrow[0]];

                CLLocationCoordinate2D coord1 = coordsArrrow[0];
                CLLocationCoordinate2D coord2 = coordsArrrow[1];

                CLLocationDegrees deltaLong = coord2.longitude - coord1.longitude;
                CLLocationDegrees yComponent = sin(deltaLong) * cos(coord2.latitude);
                CLLocationDegrees xComponent = (cos(coord1.latitude) * sin(coord2.latitude)) - (sin(coord1.latitude) * cos(coord2.latitude) * cos(deltaLong));

                CLLocationDegrees radians = atan2(yComponent, xComponent);
                CLLocationDegrees degrees = radiansToDegrees(radians) + 360;

                self.dir =  fmod(degrees, 360);


                NSLog(@"%f,%f %f,%f",coordsArrrow[0].latitude,coordsArrrow[0].longitude,coordsArrrow[1].latitude,coordsArrrow[1].longitude);
                [self.map addAnnotation:placemark];
                break;
            }
        }
        free(coordsArrrow);
        currpoints1 = currpoints1 +14;
    }

and

annotation:

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation{


if ([annotation isKindOfClass:[DirectAnnotation class]]) {
    arrow=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"parkingloc"];
    arrow.image = [UIImage imageNamed:@"userLocationCompass.png"];


    CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadians(dir));
    arrow.transform = transform;

    return arrow;
}else {
    return nil;
}}

But i have some problem
before zoom: 在此处输入图片说明

after zoom : 在此处输入图片说明

direction is confused.

Your arrows change the angle because you don't reuse them in viewForAnnotation correctly.

Just make some unique id for each arrow like this in viewForAnnotation:

routeAnnotationView = (MKAnnotationView *)[lmapView dequeueReusableAnnotationViewWithIdentifier:[NSString stringWithFormat:@"%f%f",annotation.coordinate.latitude, annotation.coordinate.longitude]];

                if (!routeAnnotationView)
                {
                    routeAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[NSString stringWithFormat:@"%f%f",annotation.coordinate.latitude, annotation.coordinate.longitude]];
                    UIImage *image = [self rotateImage:[UIImage imageNamed:@"arrpix11.png"] onDegrees:RADIANS_TO_DEGREES(curAngle)+90];
                    routeAnnotationView.image = image;
                }
                else
                {
                    routeAnnotationView.annotation = annotation;
                }
                routeAnnotationView.canShowCallout = NO;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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