您可以嘗試使用此代碼
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>
https://www.raywenderlich.com/21365/introduction-to-mapkit-in-ios-6-tutorial – Himanth
您的發佈代碼與嘗試在地圖上繪製GPX文件有什麼關係? – rmaddy
我修改我的代碼,並找不到GPX/GPX.h -_- 8請問您能幫助我嗎? – pilou