2017-02-25 45 views
0

如何使用Objective C在地圖上顯示.gpx如何在地圖上顯示.gpx視圖?

我已經試過這樣

-(void)location_button:(id)sender{ 


    _map.showsUserLocation = YES; 

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

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

} 
+0

https://www.raywenderlich.com/21365/introduction-to-mapkit-in-ios-6-tutorial – Himanth

+0

您的發佈代碼與嘗試在地圖上繪製GPX文件有什麼關係? – rmaddy

+0

我修改我的代碼,並找不到GPX/GPX.h -_- 8請問您能幫助我嗎? – pilou

回答

0

您可以嘗試使用此代碼

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> 
+0

我不能使用框架... – pilou

+0

我該如何在我的xcode項目中做到這一點? – pilou

+0

只要在旅遊項目 – Karim

0

這是我的代碼

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]; 

enter image description here

+0

你可以將它粘貼到文本中嗎? – Karim

+0

什麼文字? GPX? – pilou

+0

是GPX文本 – Karim

相關問題