iOS map coordinates array of structures constructed

Use Maps API developer back and forth there for three years now, the German fought from the use of high Tencent, the array of this structure is not always familiar with the map coordinates of each encountered on the investigation paste the copied data, delays, recorded here, convenient Find the future.

1. Tencent 3D map configuration scheme https://lbs.qq.com/ios_v1/guide-3d.html

Conventional processes,
add sdk,
add a bundle resource file (better to add on here, otherwise the first load various unexpected problems occur resource map)
-ObjC,
add various dependencies

2.0 Set the start and end position

    QPointAnnotation * startAnnotation;
    QPointAnnotation * endAnnotation;

    NSString * startLat = @"19.967205";
    NSString * startLon = @"110.410133";
    
    NSString * endLat = @"20.040722";
    NSString * endLon = @"110.231647";

    self.annotations = [NSMutableArray array];
    
    startAnnotation = [[QPointAnnotation alloc] init];
    startAnnotation.coordinate = CLLocationCoordinate2DMake([startLat doubleValue],[startLon doubleValue]);
    [self.annotations addObject:startAnnotation];
    
    endAnnotation = [[QPointAnnotation alloc] init];
    endAnnotation.coordinate = CLLocationCoordinate2DMake([endLat doubleValue],[endLon doubleValue]);
    [self.annotations addObject:endAnnotation];
    [self.mapView addAnnotations:self.annotations];

2.1 Set the start and end UI

A custom class CustomAnnotationView

@interface CustomAnnotationView : QAnnotationView

Custom substantially covering the subclass code to achieve

- (instancetype)initWithAnnotation:(id<QAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier])
    {
        
        self.bounds = CGRectMake(0, 0, 24, 38);
        [self setupLabelAndImage];
        self.centerOffset = CGPointMake(0, 0);
        self.backgroundColor = [UIColor clearColor];
    }
    
    return self;
}

-(void)setIconImageStr:(NSString *)iconImageStr
{
    _iconImageStr = iconImageStr;
    UIImage * image = [UIImage imageNamed:_iconImageStr];
    self.imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView.frame = CGRectMake(0, 0, 24, 38);
    [self addSubview:self.imageView];
}

2.2 Add the map covering agents

- (QAnnotationView *)mapView:(QMapView *)mapView viewForAnnotation:(id<QAnnotation>)annotation
{
    if ([annotation isKindOfClass:[QPointAnnotation class]])
    {
        static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
        }
        annotationView.canShowCallout   = NO;
        
        if (annotation == startAnnotation) {
            annotationView.iconImageStr = @"map_icon_origin";
        }else if(annotation == endAnnotation)
        {
            annotationView.iconImageStr = @"map_icon_destination";
        }else
        {
            annotationView.iconImageStr = @"order_icon_finish";
        }
        return annotationView;
    }
    return nil;
    
    
}

3. route planning service https://lbs.qq.com/webservice_v1/guide-road.html

Operator Interface Tencent road map webserviceAPI provided by getrequest comes in the start and end point, you can plan a path line. Planning the returned data, the coordinate point set polylinewill be compressed.

4.0 coordinate services decompression

https://lbs.qq.com/webservice_v1/guide-road.html#link-seven
Tencent illustrating the exemplary compression

var coors=[127.496637,50.243916,-345,-1828,19867,-26154];
for (var i = 2; i < coors.length ; I++)
{coors[i] = coors[i-2] + coors[i]/1000000}

iOS decompressed simple implementation

self.arr = @[@"127.496637",@"50.243916",@"-345",@"-1828",@"19867",@"-26154"];
    NSUInteger count = self.arr.count;
    NSMutableArray * arra = [NSMutableArray arrayWithArray:self.arr];

//    解压缩坐标实现
        for (int i = 2 ; i < arra.count; i ++) {
            double v0 = [arra[i - 2] doubleValue];
            double v1 = [arra[i] doubleValue] / 1000000;
            double vTotal = v0 + v1;
            NSNumber * number = [NSNumber numberWithDouble:vTotal];
            NSMutableArray *newArray = [arra mutableCopy];
            [newArray replaceObjectAtIndex:i withObject:number];
            arra = newArray;
        }
    NSLog(@"%@",arra);

https://lbs.qq.com/ios_v1/guide-3d.html

//    构造结构体数组实现并且赋值
    int vCount = (int)count / 2;
    CLLocationCoordinate2D coordinateArry[vCount];
        for (int i = 0; i < count ;i += 2) {
            double vLat = [arra[i] doubleValue];
            double vLon = [arra[i + 1] doubleValue];
            int v = i / 2;
            coordinateArry[v].latitude = vLat;
            coordinateArry[v].longitude = vLon;
        }
//    展示折线到地图上
        QPolyline *walkPolyline = [QPolyline polylineWithCoordinates:coordinateArry count:vCount];
        [self.mapView addOverlay:walkPolyline];

5.1 map covering agents

- (QOverlayView *)mapView:(QMapView *)mapView viewForOverlay:(id<QOverlay>)overlay
{
    QPolylineView *polylineView = [[QPolylineView alloc] initWithPolyline:overlay];
    polylineView.lineWidth = 8;
    polylineView.lineDashPattern = nil;
    polylineView.strokeColor = [UIColor greenColor];
    return polylineView;
}

6 illustrates the effect of:

2261629-4d1a0fbd1d7f2822.png
Simulator Screen Shot - iPhone 7 - 2019-05-31 at 15.59.09.png

7. code address https://github.com/xgkp/TencentMapDrawLine.git

Note: Due to Tencent map sdk greater than 100M, so I will have to upload the code when a file directory /Venderis added to file a gitignore gone, if you want to run the code, add your own. Directory icon:

2261629-20b683d1fccbbea4.png
zz.png

8. Reflections and hard turns

FIG:
icon:


2261629-70c09d653c29ca0f.png
zzz.png
2261629-f47689b1bdfd1728.png
zc.png

Tencent has a map

+ (QPolyline *)polylineWithCoordinates:
(CLLocationCoordinate2D *)coords count:(NSUInteger)count;

Method, the actual effect of this method is the set of all points on display.

Middle CLLocationCoordinate2D argument, which appears to be a picture from the last structure of a C, but the strange thing is that behind any connection with a count parameter, this is not the length of the array represents what, ah?

Since storage coords is an array type parameter, and it is a structure of this type CLLocationCoordinate2D contradictory.

This has caused my confusion, I spare a big detour.

I saw an array Yeah, easy to handle, write a for loop, using coordinates to traverse the array of coordinates, construction CLLocationCoordinate2D types of data into a final array, add objects of this type when CLLocationCoordinate2D to iOS in the array, error,
2261629-deab8f3148a75f9f.png
zcc.png

I saw, this looks like an array OC can not be stored in this type of structure, you need to turn into other types can keep it.
Here, it can be stored up to find a method of the structure OC:

 NSValue包装对象指针,CGRect结构体等
 一个NSValue对象是用来存储一个C或者Objective-C
数据的简单容器,它可以保存任意类型的数据.
比如int , float , char ,当然也可以是指pointers,structures, 
and object ids .
NSValue类的目标就是允许以上数据类型的数据结构能够被添加到集合里,
例如那些需要其元素是对象的数据结构,
如NSArray 或者NSSet的实例.
需要注意的是NSValue对象一直是不可枚举的

Directly on the code


    NSMutableArray * arraaa = [NSMutableArray arrayWithCapacity:0];
    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(19.000000, 110.000000);
//    [arraaa addObject:coor];
    NSValue *va = [NSValue valueWithBytes:&coor objCType: @encode(CLLocationCoordinate2D)];
    [arraaa addObject:va];

    }

So do not even write error, I guess, this should get it.

Then the next step emm


2261629-6f1975c7a12b7b6d.png
z2.png

Click Fix, Wow, is not actually being given, to see the dawn

   for (int i = 0; i < arra.count; i += 2) {
        double vLat = [arra[i] doubleValue];
        double vLng = [arra[i + 1] doubleValue];
        CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(vLat, vLng);
        NSValue *va = [NSValue valueWithBytes:&coor objCType: @encode(CLLocationCoordinate2D)];
          [arraaa addObject:va];
    }
    
    
    QPolyline *Polyline1 = [QPolyline polylineWithCoordinates:(__bridge CLLocationCoordinate2D *)(arraaa) count:vCount];
    [self.mapView addOverlay:Polyline1];

Running about, emm did not show blank. This should be felt with a strong bridge to OC array directly converted into an array of structures coordinate format is still a problem, until now did not understand.

Then convert ideas, as we have to a structure, bird from the outset to create the array structure looks like the job.

    CLLocationCoordinate2D coordinateArry[vCount];

This created, but there is no set method, no addObject method, jammed again! ! ! emmm, embarrassed not know where the impression, looks like a subscript to an assignment, try assignment.


2261629-5a284d609b0e3e2f.png
z22.png

Index value is not an array, pointer, vectors, do not like to take the structure of it, actually did struct I want. Well ~ fast Meizhe.
Home from work, do not want ~~

Recently arrived from work when he thunderstorm, but there is a day to see the rainbow ~ ~ ~

2261629-662b711cc4cece0d.jpeg
zzzz.jpeg
2261629-b4ecdf744620e34e.png
zzzzzz.png

Or not willing to go back, I opened Tencent map iOS3D map api introduction, suddenly thought of the demo looks like write polygons and polylines painting, look at how the data is structured, perhaps there is a solution.

Finally, I was surprised actually saw the scene ~ ~ ~


2261629-428d76dc5bba4d5c.png
z1.png

Really want to give yourself a mouth son, delayed for so long, there are actually ready! ! !
Always thinking with subscript to take, did not pay attention to the structure is to direct the same to bring up the property assignment ....

Ups and downs ~~~~~~~

Reproduced in: https: //www.jianshu.com/p/329f75f17432

Guess you like

Origin blog.csdn.net/weixin_34175509/article/details/91150358