如何在地图上的两点之间绘制箭头? MapKit

时间:2013-07-24 09:05:37

标签: ios objective-c mapkit

如何在地图上的两点之间绘制箭头? 我尝试计算纬度和经度,但出了点问题。

最好的问候,Max

这是我的代码和结果图片

        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;
    }

enter image description here

3 个答案:

答案 0 :(得分:1)

哦伙计们。我改变其他方式。进行注释并在两点之间按方向旋转。

metods:

计算方向:

     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;
    }

注释:

- (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;
}}

但我有一些问题 缩放前: enter image description here

放大后

enter image description here

方向很困惑。

答案 1 :(得分:1)

您的箭头会改变角度,因为您没有正确地在viewForAnnotation中重复使用它们。

在viewForAnnotation中为每个箭头创建一个唯一的id:

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;

答案 2 :(得分:0)

将此用于地图中两个位置之间的绘制路径。

http://code.google.com/p/ashiphone/downloads/detail?name=MapWithRoutes.zip&can=2&q=