美文网首页iOS开发.
兼容两步路的KML文件

兼容两步路的KML文件

作者: 侭情显現 | 来源:发表于2019-04-23 13:56 被阅读4次

    公司轨迹使用正规的KML文件格式.用谷歌的api-KML.http://code.google.com/apis/kml/documentation/kmlreference.html进行解析

    现需要兼容两步路的KML轨迹文件.使用以上api会解析失败.发现两步路使用的不是标准的KML文件.所以针对谷歌api_KML的源码做调整如下即可:

    KMLAbstractFeature.m文件中

       - (id)initWithXMLElement:(KMLXMLElement *)element parent:(KMLElement *)parent
        {
            self = [super initWithXMLElement:element parent:parent];
            if (self) {
                ......
                //解析gx:coord样式
                NSMutableArray * fq_gxCoordArr = [NSMutableArray array];
                KMLXMLElement *el = [KMLXML childAndNextElementNamed:@"gx:coord" parentElement:element];
                while (el != nil) {
                    KMLCoordinate * coordinate = [[KMLCoordinate alloc]initWithCustomText:[KMLXML textForElement:el]];
                    [fq_gxCoordArr addObject:coordinate];
                    el = [KMLXML nextSiblingNamed:@"gx:coord" searchFromElement:el];
                }
                _gxCoordArr = fq_gxCoordArr;
                
                _point = (KMLPoint *)[self childElementOfClass:[KMLPoint class] xmlElement:element];
            }
            return self;
        }
    

    KMLXML.m文件中添加一种全新解析方法

     + (KMLXMLElement*) childAndNextElementNamed:(NSString*)aName  parentElement:(KMLXMLElement*)aParentXMLElement{
            KMLXMLElement * xmlElement = aParentXMLElement;
            const char * name = [aName cStringUsingEncoding:NSUTF8StringEncoding];
            while (xmlElement) {
                xmlElement = xmlElement->firstChild;
                if(xmlElement){
                    
                    if (strlen(xmlElement->name) == strlen(name) && memcmp(xmlElement->name,name,strlen(name)) == 0) {
                        return xmlElement;
                    }
                    xmlElement = xmlElement->nextSibling;
                    if (strlen(xmlElement->name) == strlen(name) && memcmp(xmlElement->name,name,strlen(name)) == 0) {
                        return xmlElement;
                    }
                }
            }
            return nil;
        }
    

    随后在解析出轨迹的类:KKTraceFileServers.m中添加自己的代码

      + (void)asyncChangeToLineModelWithKMLRoot:(KMLRoot *)kmlRoot
                           complitedBlock:(void(^)(NSArray<KKMapLineModel *> *lineModels))complitedBlock
    {
                              .......
    
                          KMLLineString *line = (KMLLineString *)placemark.geometry;
                            
                            if (!line) {
                                if (placemark.gxCoordArr.count > 0) {
                                    line = [[KMLLineString alloc]init];
                                    [line setValue:placemark.gxCoordArr forKey:@"_coordinates"];
                                }
                            }
                            
                            if (![line isKindOfClass:[KMLLineString class]]) {
                                if (complitedBlock)
                                {
                                    complitedBlock(nil);
                                }                                
                                continue;
                            }
                        ..........
      }
    

    如此即可成功解析出轨迹点.思路来源于观察两步路的数据格式样式!

    相关文章

      网友评论

        本文标题:兼容两步路的KML文件

        本文链接:https://www.haomeiwen.com/subject/nvrvgqtx.html