Realize the target of small and medium-sized vehicle market in logistics scenario

The following content is reprinted from the pasted article "realizing the target of car Marker in logistics scenario"

Author: batter

Link: https://www.jianshu.com/p/f794b02a81f5

Source: Jianshu

The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

Scenario requirements

In the APP related to express logistics, such as express delivery and meal delivery, the head of the express car Marker can always point to the destination at the passing point, as shown in the following figure:

Use technology: Tencent maps iOS SDKPoint marker and Draw lines

Core points:

1. Coordinates of operation qpointanotion

2. Get the coordinates of qpointanotion from mapView

3. Calculating the angle between the coordinates of the passing point and the end point by trigonometric function

4. Operate on the transform attribute of QAnnotationView

The code example is as follows:

1. The example shows that Fuzhou is sent to Beijing via Xi'an, Xining, Jinan, Taiyuan and Tianjin. First, the maker s of these points are added to the map:

// Fuzhou
locations[0] = CLLocationCoordinate2DMake(26.101797,119.415539);
// Xi'an
locations[1] = CLLocationCoordinate2DMake(34.475422,109.0005);
// Xining
locations[2] = CLLocationCoordinate2DMake(36.69099,101.749523);
// Ji'nan
locations[3] = CLLocationCoordinate2DMake(36.761434,117.174328);
// Taiyuan
locations[4] = CLLocationCoordinate2DMake(37.949064,112.56007);
// Tianjin
locations[5] = CLLocationCoordinate2DMake(39.117802,117.174328);
// Beijing
locations[6] = CLLocationCoordinate2DMake(39.897614,116.383312);

// Fuzhou
QPointAnnotation *nnAnnotation = [[QPointAnnotation alloc] init];
nnAnnotation.coordinate = locations[0];
[self.mapView addAnnotation:nnAnnotation];

....

2. Add a car marker, starting from Fuzhou:

_carAnnotation = [[QPointAnnotation alloc] init];
_carAnnotation.coordinate = locations[0];
// Specify userData custom data to determine marker type
_carAnnotation.userData = @"car";
[self.mapView addAnnotation:_carAnnotation];

3. Implement mapView proxy method to distinguish different marketers according to userData

- (QAnnotationView *)mapView:(QMapView *)mapView viewForAnnotation:(id<QAnnotation>)annotation {
    static NSString *reuse = @"annotation";
    static NSString *reuseCar = @"annotationCar";
    QPinAnnotationView *annotationView = (QPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuse];
    if (annotationView == nil) {
        if (annotation == _carAnnotation) {
            annotationView = [[QPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseCar];
            annotationView.image = [UIImage imageNamed:@"car"];
            // Save the car's AnnotationView as an attribute for steering operation
            _carAnnotationView = annotationView;
        } else {
            annotationView = [[QPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuse];
        }
    }
    
    return annotationView;
}

4. According to the trigonometric function, calculate the angle of the starting point and the ending point, and adjust the angle of the trolley Marker

- (void)annotationRotate {
    // Take out the coordinate position of the end point
    CLLocationCoordinate2D toCoord = _bjAnnotation.coordinate;
    
    double fromLat = _carAnnotation.coordinate.latitude;
    double fromlon = _carAnnotation.coordinate.longitude;
    double toLat = toCoord.latitude;
    double tolon = toCoord.longitude;

    double slope = ((toLat - fromLat) / (tolon - fromlon));
    
    double radio = atan(slope);
    double angle = 180 * (radio / M_PI);
    if (slope > 0) {
        if (tolon < fromlon) {
            angle = -90 - angle;
        } else {
            angle = 90 - angle;
        }
    } else if (slope == 0) {
        if (tolon < fromlon) {
            angle = -90;
        } else {
            angle = 90;
        }
    } else {
        if (toLat < fromLat) {
            angle = 90 - angle;
        } else {
            angle = -90 - angle;
        }
    }
    
    // Note here that the angle is calculated, and the rotation needs to be converted to radians first
    _carAnnotationView.transform = CGAffineTransformMakeRotation((M_PI * (angle) / 180.0));
}

On this basis, I added a function of switching the current route point in the navigationItem. Each time I click the button, the car will move to the next route point. The example code is as follows:

- (void)handleTestAction {
    _index++;
    
    if (_index == self.mapView.annotations.count - 2) {
        _index = 0;
    }
    
    QPointAnnotation *annotation = self.mapView.annotations[_index];
    
    _carAnnotation.coordinate = annotation.coordinate;
    
    [self annotationRotate];
}

An example of the effect is shown below:

Tags: Mobile Attribute iOS SDK

Posted on Fri, 29 May 2020 00:58:29 -0400 by rajavel