1

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

最好的问候, 马克斯

这是我的代码和结果图片

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

在此处输入图像描述

4

3 回答 3

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;
于 2015-02-26T23:43:15.430 回答
1

哦,伙计们。我换个方式。进行注释并在两点之间随方向旋转。

方法:

计算方向:

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


但是在缩放之前 我有一些问题:在此处输入图像描述

缩放后: 在此处输入图像描述

方向迷茫。

于 2013-07-24T12:31:58.360 回答
0

使用它来绘制地图中两个位置之间的路径。

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

于 2013-07-24T09:10:37.067 回答