Created
September 6, 2016 21:59
-
-
Save nixta/c351e5771dc1358eedc941c1a764cc87 to your computer and use it in GitHub Desktop.
Simulating GPS with the ArcGIS Runtime SDK for iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "ViewController.h" | |
| #import <ArcGIS/ArcGIS.h> | |
| #define kSimulatedMetersPerSecond 2 | |
| @interface ViewController () <AGSRouteTaskDelegate> | |
| @property (nonatomic, strong) AGSRouteTaskParameters *defaultParams; | |
| @property (nonatomic, strong) AGSRouteTask *routeTask; | |
| @property (weak, nonatomic) IBOutlet AGSMapView *mapView; | |
| @property (nonatomic, strong) AGSRouteResult *currentRoute; | |
| @property (nonatomic, strong) AGSGraphicsLayer *routeResultsLayer; | |
| #pragma mark - Routing and Navigation | |
| -(void)routeTask:(AGSRouteTask *)routeTask operation:(NSOperation *)op didSolveWithResult:(AGSRouteTaskResult *)routeTaskResult { | |
| [self.mapView.locationDisplay stopDataSource]; | |
| self.currentRoute = [routeTaskResult.routeResults firstObject]; | |
| [self.routeResultsLayer removeAllGraphics]; | |
| [self.routeResultsLayer addGraphic:self.currentRoute.routeGraphic]; | |
| AGSPolyline *routeLine = (AGSPolyline *)self.currentRoute.routeGraphic.geometry; | |
| [self.mapView zoomToGeometry:routeLine withPadding:40 animated:YES]; | |
| [self setSimulatedGPSToPolyline:routeLine]; // Set up a simulation with a densified route result. | |
| [self.mapView.locationDisplay startDataSource]; // And start simulating. | |
| } | |
| #pragma mark - GPS Simulation | |
| -(void)setSimulatedGPSToPolyline:(AGSPolyline *)sourceLine { | |
| AGSPolyline *gpsLine = [self getSimulatedPolyineFromPolyline:sourceLine]; | |
| AGSSimulatedLocationDisplayDataSource *simDS = [[AGSSimulatedLocationDisplayDataSource alloc] init]; | |
| [simDS setLocationsFromPolyline:gpsLine]; | |
| // Set the simulator to follow the polyline. It hasn't started yet though! | |
| // Note, it emits a point a second (I think), so see getSimulatedPolyineFromPolyline to work out how to densify. | |
| // Still not perfect, but gets close. | |
| self.mapView.locationDisplay.dataSource = simDS; | |
| } | |
| -(AGSPolyline *)getSimulatedPolyineFromPolyline:(AGSPolyline *)source { | |
| // Conversion only necessary if you get the result back in some non-meters based SR. | |
| AGSPolyline *workingLine; | |
| workingLine = (AGSPolyline *)[[AGSGeometryEngine defaultGeometryEngine] projectGeometry:source | |
| toSpatialReference:[AGSSpatialReference webMercatorSpatialReference]]; | |
| workingLine = (AGSPolyline *)[[AGSGeometryEngine defaultGeometryEngine] densifyGeometry:workingLine | |
| withMaxSegmentLength:kSimulatedMetersPerSecond]; | |
| workingLine = (AGSPolyline *)[[AGSGeometryEngine defaultGeometryEngine] projectGeometry:workingLine | |
| toSpatialReference:[AGSSpatialReference wgs84SpatialReference]]; | |
| return workingLine; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, the above code doesn't cover creating and calling the route task...