The following content is reprinted from the article “Tencent Map SDK distance Measurement gadget” by Miaomu

Akik: Batter

Link: www.jianshu.com/p/6e507ebcd…

Source: Jane Book

Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.

preface

In order to get familiar with the QGeometry class in Tencent map SDK, as well as the coordination between points and lines, I wrote this small Demo which can dot on the map and obtain the straight line distance.

Usage scenarios

For some scenes that need to quickly know a certain route is not very long and need to plan the route by themselves, the route planning function of Tencent Map may not be what they want, and they need to be connected at all times. The purpose of this function is to plan the route on the map, get the distance of the route, and can save it as your own route.

However, because the linear distance is only calculated by latitude and longitude, there will be some errors in accuracy.

To prepare

  • Tencent MAP 3D SDK
  • Add custom gestures to the map
  • Poyline draw
  • Distance calculation

process

1. Add custom long-press gesture on MapView, convert gesture points on the screen into map coordinates, and add Marker:

- (void)setupLongPressGesture {
    self.addMarkerGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addMarker:)];
    [self.mapView addGestureRecognizer:self.addMarkerGesture];
}

- (void)addMarker:(UILongPressGestureRecognizer *)gesture {
    if(gesture. State = = UIGestureRecognizerStateBegan) {/ / take CLLocationCoordinate2D location = [self. The mapView convertPoint:[gesture locationInView:self.mapView] toCoordinateFromView:self.mapView]; QPointAnnotation *annotation = [[QPointAnnotation alloc] init]; annotation.coordinate = location; // Add to route [self.annotationArray addObject:annotation]; [self.mapView addAnnotation:annotation]; [self handlePoyline]; }}Copy the code
  • Tencent Map QMapView class, provides a convenient method to directly convert screen coordinates into map coordinates:- (CLLocationCoordinate2D)convertPoint: toCoordinateFromView:

2. Draw Polyline using the coordinate points of the added Marker:

- (void)handlePoyline { [self.mapView removeOverlays:self.mapView.overlays]; // Determine if there are more than two pointsif (self.annotationArray.count > 1) {
        NSInteger count = self.annotationArray.count;
        CLLocationCoordinate2D coords[count];
        for(int i = 0; i < count; i++) { QPointAnnotation *annotation = self.annotationArray[i]; coords[i] = annotation.coordinate; } QPolyline *polyline = [[QPolyline alloc] initWithCoordinates:coords count:count]; [self.mapView addOverlay:polyline]; } // Calculate the distance [self countDistance]; }Copy the code
  • The important thing to note here is that every time you add another Overlay, you need to remove the previous Overlay. At present, Tencent Map does not support continuous modification in the same Polyline.

3. Calculation distance: QGeometry is the class of geometric calculation provided by SDK, which provides many tools and methods, such as “coordinate transformation, judgment intersection, external rectangle” and other convenient functions

- (void)countDistance {
    _distance = 0;
    
    NSInteger count = self.annotationArray.count;
    
    for (int i = 0; i < count - 1; i++) {
        QPointAnnotation *annotation1 = self.annotationArray[i];
        QPointAnnotation *annotation2 = self.annotationArray[i + 1];
        _distance += QMetersBetweenCoordinates(annotation1.coordinate, annotation2.coordinate);
    }
    
    [self updateDistanceLabel];
}
Copy the code
  • QMetersBetweenCoordinates()The method receives two CLLocationCoordinate2D parameters and calculates the straight-line distance between the two coordinates

Example: Get the total distance of the route by connecting dots

link

Interested students can download the Demo in the code cloud to try it out.