如何在地图上显示.gpx?

时间:2017-02-25 13:27:09

标签: ios objective-c xcode cllocationmanager apple-maps

如何使用.gpx在地图视图中显示Objective C

我试过这样的

    -(void)location_button:(id)sender{


    _map.showsUserLocation = YES;

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

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

}

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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