如何在地图视图上显示 .gpx?

Posted

技术标签:

【中文标题】如何在地图视图上显示 .gpx?【英文标题】:How to display a .gpx on a map View? 【发布时间】:2017-02-25 13:27:09 【问题描述】:

如何在带有Objective C 的地图视图上显示.gpx

我试过这样

    -(void)location_button:(id)sender


    _map.showsUserLocation = YES;

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [_map setUserTrackingMode:true];
    _map.zoomEnabled = true;


【问题讨论】:

raywenderlich.com/21365/… 您发布的代码与尝试在地图上绘制 GPX 文件有什么关系? 我修改了我的代码,GPX/GPX.h 找不到 -_- 8 你能帮我吗??!! 【参考方案1】:

你可以试试这段代码

GPXRoot *root = [GPXParser parseGPXWithString:@""];

NSMutableArray *annotations = [[NSMutableArray alloc] init];

for (GPXRoutePoint *point in [[[root routes] firstObject] routepoints]) 
    MKPointAnnotation *an = [[MKPointAnnotation alloc] init];
    an.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
    an.title = point.name;

    [annotations addObject:an];


[self.map addAnnotations:annotations];

我使用了这个框架:ios-GPX-Framework

对于 parseGPXWithString,您传递 GPX 字符串。我用这个:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" creator="mapstogpx.com" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
  <metadata>
    <link href="http://www.mapstogpx.com">
      <text>Sverrir Sigmundarson</text>
    </link>
    <time>2017-02-26T00:29:29Z</time>
  </metadata>
  <wpt lat="52.5200066" lon="13.404954">
    <name>Berlin</name>
    <desc>Berlin, Germany</desc>
  </wpt>
  <wpt lat="52.60359" lon="13.13132">
    <name>Fehrbelliner Str. 17</name>
    <desc>Fehrbelliner Str. 17, 14621 Schönwalde-Glien, Germany</desc>
  </wpt>
<rte>
  <name>Berlin to Fehrbelliner Str. 17</name>
  <rtept lat="52.5200119" lon="13.4049465">
    <name>RP001</name>
  </rtept>
  <rtept lat="52.5169512" lon="13.3857421">
    <name>RP002</name>
  </rtept>
  <rtept lat="52.5154578" lon="13.3860777">
    <name>RP003</name>
  </rtept>
  <rtept lat="52.5145908" lon="13.3775093">
    <name>RP004</name>
  </rtept>
  <rtept lat="52.5161642" lon="13.3769081">
    <name>RP005</name>
  </rtept>
  <rtept lat="52.514824" lon="13.3510166">
    <name>RP006</name>
  </rtept>
  <rtept lat="52.5151102" lon="13.3496192">
    <name>RP007</name>
  </rtept>
  <rtept lat="52.5147424" lon="13.3490666">
    <name>RP008</name>
  </rtept>
  <rtept lat="52.5128618" lon="13.3228339">
    <name>RP009</name>
  </rtept>
  <rtept lat="52.5131856" lon="13.32158">
    <name>RP010</name>
  </rtept>
  <rtept lat="52.519659" lon="13.2996583">
    <name>RP011</name>
  </rtept>
  <rtept lat="52.5213479" lon="13.2998705">
    <name>RP012</name>
  </rtept>
  <rtept lat="52.5225443" lon="13.3004309">
    <name>RP013</name>
  </rtept>
  <rtept lat="52.5341464" lon="13.2940108">
    <name>RP014</name>
  </rtept>
  <rtept lat="52.5366707" lon="13.2718733">
    <name>RP015</name>
  </rtept>
  <rtept lat="52.5393297" lon="13.232217">
    <name>RP016</name>
  </rtept>
  <rtept lat="52.5381524" lon="13.2296307">
    <name>RP017</name>
  </rtept>
  <rtept lat="52.540277" lon="13.2041199">
    <name>RP018</name>
  </rtept>
  <rtept lat="52.5454629" lon="13.1946981">
    <name>RP019</name>
  </rtept>
  <rtept lat="52.5539515" lon="13.1989368">
    <name>RP020</name>
  </rtept>
  <rtept lat="52.5553186" lon="13.1986193">
    <name>RP021</name>
  </rtept>
  <rtept lat="52.5957529" lon="13.1542102">
    <name>RP022</name>
  </rtept>
  <rtept lat="52.6003735" lon="13.1339152">
    <name>RP023</name>
  </rtept>
</rte>
</gpx>

此代码适用于您的 GPX

NSMutableArray *annotations = [[NSMutableArray alloc] init];

for (GPXRoutePoint *point in [root waypoints]) 
    MKPointAnnotation *an = [[MKPointAnnotation alloc] init];
    an.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
    an.title = point.name;

    [annotations addObject:an];


[self.mapView addAnnotations:annotations];

以上代码将适用于以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="topografix.com/GPX/1/1"; creator="JpRNavMaster" version="1.1" xmlns:xsi="w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="topografix.com/GPX/1/1 topografix.com/GPX/1/1/gpx.xsd">
    <wpt lat="50.13530" lon="1.85472">
        <name>ABB</name>
        <cmt>VOR/DME Abbeville</cmt>
    </wpt> <wpt lat="43.88810" lon="0.87278">
        <name>AGN</name> 
        <cmt>VOR/DME Agen Gaudonville</cmt>
    </wpt>
</gpx>

【讨论】:

把它拖到游览项目中 它不起作用。我已将框架拖到我项目的框架文件夹中。而且您的代码不起作用。 确保将其包含在目标依赖项中并使用库链接二进制文件 我不能-_- 很难 这里是下载工作项目的链接:sendspace.com/file/x6ku38【参考方案2】:

这是我的代码

    NSString* gpxPath = [[NSBundle mainBundle] pathForResource:@"lfn1603" ofType:@"gpx"];
NSString *gpxString = [NSString stringWithContentsOfFile:gpxPath encoding:NSUTF8StringEncoding error:nil];
GPXRoot *root = [GPXParser parseGPXWithString:gpxString];

NSMutableArray *annotations = [[NSMutableArray alloc] init];

for (GPXRoutePoint *point in [[[root routes] firstObject] routepoints]) 
    MKPointAnnotation *an = [[MKPointAnnotation alloc] init];
    an.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
    an.title = point.name;

    [annotations addObject:an];


[self.mapView addAnnotations:annotations];

【讨论】:

你能把它粘贴到文本中吗? 什么文字? GPX? 是的 GPX 文本 topografix.com/GPX/1/1" creator="JpRNavMaster" version="1.1" xmlns :xsi="w3.org/2001/XMLSchema-instance" xsi:schemaLocation="topografix.com/GPX/1/1 topografix.com/GPX/1/1/gpx.xsd"> ABBVOR/DME Abbeville AGNVOR/DME Agen Gaudonville跨度> 我修改了我的代码,但 GPX/GPX.h 找不到 -_- 8 你能帮帮我吗??!!

以上是关于如何在地图视图上显示 .gpx?的主要内容,如果未能解决你的问题,请参考以下文章

如何在谷歌地图中绘制gpx文件

如何从谷歌地图路线中提取路线 GPX?

【小程序】地图|绘制GPX轨迹

在适当的外部地图应用程序中打开 KML/GPX 文件

如何将谷歌地图 GeoJSON 转换为 GPX,保留位置名称

点线面如何标绘在地图上