High moral map SDK real-time rendering running path -Swift implementation

High moral map SDK real-time rendering running path -Swift implementation

One of the most basic functions of running software is to locate the moving users in real time and draw the path of motion. In this paper, the main content is to use high German SDK simple path rendering. Apple’s built-in map is also high German, of course, you can also use Baidu’s.

First, Xcode create a new project, select Single View Application, language select Swift

High moral map SDK real-time rendering running path -Swift implementation

After the project is completed, the next step is to import the high moral map framework. Can choose to use CocoaPods, manual installation is as follows:

Download High German map SDK, apply for API Key. The relevant page
(latest version of this writing high de SDK 3.3.0, need to add -Objc, or call the relevant interface will collapse, using the Demo version of 3.2.0, do not need to add markers between versions some interface and variables are different.)

Import SDK

The directory selected in the project name, the TARGETS-&gt Build Phases-> Link Binary; With Libaries, click the “+” button, click on the “Add Other” button in the pop-up window, select the downloaded MAMapKit.framework file added to the project.

High moral map SDK real-time rendering running path -Swift implementation

AMap.bundle resource file

AMap.bundle resource files stored in the location, the default pin mark view pictures, such as the use of these resources to develop pictures.

Select the name of the project in the left directory and select Add Files to in the right menu”… Select the AMap.bundle file from the MAMapKit.framework-> Resources folder, and select the “Copy items needed if” check box, click the “Add” button to add the resource file to the project.

MAMapKit.framework is best placed in the project file, otherwise it may lead to the compiler

Introducing dependency Libraries

The directory selected in the project name, the TARGETS-&gt Build Settings-> Link Binary; With Libaries, click the “+” button, find and select the required Library in the pop-up window (see below), click “Add” button, the library files added to the project.











After the introduction, as shown in the following figure:

High moral map SDK real-time rendering running path -Swift implementation

Info.plist add attributes

Add NSLocationAlwaysUsageDescription. If you don’t add this item, you will not be able to locate it

Add the Required background modes, App registers for and add to it location updates properties, making the application can continue to receive location information in the background, there is a set of attributes and the related MAMapView, later mentioned.

App Transport Settings add on Allow Loads Arbitrary, and set to YES, iOS9 after the default network request to HTTPS, will not modify the warning will be reported to the Security

The modified info.plist is as follows

High moral map SDK real-time rendering running path -Swift implementation

Next, the implementation of the start code

New bridge joint file

SDK is a third party OC project, which calls for the creation of a bridge header file in Swift. New header file, named MapPath-Bridging-Header.h, and then set the corresponding path, as follows:

High moral map SDK real-time rendering running path -Swift implementation

Ps. first set the trouble at this time can create a OC class, then Xcode will automatically pop up a bridge whether to create a header file prompt box, select the Create Bridging Header can automatically generate the corresponding and complete the project settings.

High moral map SDK real-time rendering running path -Swift implementation

The file code is as follows

Create-Bridging-Header.h #import / < MAMapKit/MAMapKit.h>

Set ApiKey

The use of maps need to set the ApiKey, otherwise it will be reported to ApiKey empty warning. Set up at the start of the project, so the didFinishLaunchingWithOptions is processed in AppDelegate

Func application / / AppDelegate.swift (application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]? ->); Bool Override point for customization {/ / after / / application launch. MAMapServices.sharedServices (.ApiKey) apiKey return true} = "e8be3a280e6fc3c2651ebef77e7a0fa5"

Create a new class RunningViewController, inherited from the UIViewController, comply with MAMapViewDelegate, which is used to show the running process of the map interface, while creating the RunningViewController.xib file

Class RunningViewController: UIViewController, MAMapViewDelegate

Declare properties

@IBOutlet weak var mapView: MAMapView var coordinateArray: [CLLocationCoordinate2D] = []!

Drag a UIView to the RunningViewController.xib view, set the spacing of the quad specific parent view to 0, and link the mapView. Too lazy to write code to add a view

The coordinateArray array is used to store the location update data each time it takes.

Map initialization settings in viewDidLoad

Override func (viewDidLoad) super.viewDidLoad (initMapView) {}) (func (initMapView) {mapView.delegate = self mapView.zoomLevel = 15.5 mapView.distanceFilter = 3 mapView.desiredAccuracy = kCLLocationAccuracyBestForNavigation}

The view controller is set to the mapView proxy, and when the location is updated, the callback is performed by the proxy

ZoomLevel scaling for current map

DistanceFilter for the location of the minimum update distance, when the moving distance exceeds the set value will be the location of the update callback

DesiredAccuracy for positioning accuracy, the default is the highest accuracy, generally with this

When the view is displayed, the positioning is started

Override func viewDidAppear (animated: Bool) {super.viewDidAppear (animated) startLocation (func)} (startLocation) {mapView.showsUserLocation = true mapView.userTrackingMode = MAUserTrackingMode.Follow mapView.pausesLocationUpdatesAutomatically = false mapView.allowsBackgroundLocationUpdates = true}

ShowsUserLocation is set to true after the start of the positioning, set to false positioning stop. Positioning itself will be more consumption point, the application does not need to locate the positioning function will be closed when you shut down, otherwise your phone will consume a lot of electricity quickly.

UserTrackingMode for the positioning of the way, to see the interface can be seen in three forms, follow the location of the user to move the location

AllowsBackgroundLocationUpdates is set to true to allow for background positioning, to ensure that the App registers location updates property has been set before, otherwise it will crash.

Agent implementation

Every time there is a location update will call mapView (mapView: MAMapView, didUpdateUserLocation userLocation: MAUserLocation, updatingLocation: Bool) function

MAMapViewDelegate func mapView / / MARK: (mapView: MAMapView, didUpdateUserLocation userLocation: MAUserLocation, updatingLocation: Bool) {/ / map each location update callbacks when if updatingLocation {/ / get the new positioning data let coordinate = userLocation.coordinate / / add to save point array self.coordinateArray.append (coordinate) (updatePath)}}

Through the updatePath function to draw the path

Func (updatePath) {/ / each time to get to the new location to draw a path / removed before removing overlay let overlays = self.mapView.overlays self.mapView.removeOverlays (overlays) let polyline = MAPolyline (coordinates: & self.coordinateArray, count: UInt (self.coordinateArray.count)) self.mapView.addOverlay (polyline) / / the latest location to the middle interface display let lastCoord = self.coordinateArray[self.coordinateArray.count - 1] self.mapView.setCenterCoordinate (lastCoord animated: true)}

Before the beginning of the implementation is to use the new point and a point of connection, but in this way will have a problem found in the map to enlarge the maximum zoom ratio will, through two points of connected line segments does not constitute a complete line, but a disjointed line. So now instead of taking all the dots to redraw the entire path. Remember to delete the existing path before drawing, otherwise the path of each draw will be stacked together, resulting in a rough line path.

RemoveOverlays is used to remove the previous path.

AddOverlay add a new path.

SetCenterCoordinate will specify the coordinates of the point in the middle of the map, to ensure that the user runs a long distance will not exceed the map display.

AddOverlay draw a path, there will be a callback function:

Func mapView (mapView: MAMapView, viewForOverlay overlay:! MAOverlay! ->); MAOverlayView if (MAPolyline) overlay.isKindOfClass! {{let polylineView = MAPolylineView (overlay: overlay) polylineView.lineWidth polylineView.strokeColor UIColor (red: = 6 = 4 / 255, 181 / 255 green: / 255, blue: 108, alpha: 1) return polylineView return nil}}

Determine whether the MAPolyline type, and then set the path width and color. In addition to the line, the image on the map, and some other operations are also processed in this callback

Generally realized as above. Then we can run Demo.

The program looks like the following, when opened, there is a button in the middle of the interface, click to enter the map interface

High moral map SDK real-time rendering running path -Swift implementation
High moral map SDK real-time rendering running path -Swift implementation

At this point, you may want to compile the prototype and then to the outside test results, actually provides a functional simulator to directly simulate the position change. Select the simulator of the Debug, point Location, there are several options, there are several ways to change the location, you can also set their own latitude and longitude positioning, which Freeway Drive faster movement of the ~

Draw the effect of the motion path as shown below:

High moral map SDK real-time rendering running path -Swift implementation

(map effect simulators does not display other information, the real machine will correspond with your actual location map content is displayed in full the information around)

See the source code

Finally there is a postscript

The location of the iOS system is used in hybrid positioning way, through GPS, Wifi, mobile phone base station positioning signal common way to improve the positioning accuracy, though, but the occasional location error of a point is still difficult to avoid, when there is a large deviation, will lead to a significant bulge point path or, the whole path burr phenomenon is serious, even for a long time does not move in a position, will also appear in the vicinity of positioning points derived fluttering situation. These problems can only be corrected by algorithmic analysis.

Commonly used in the processing algorithm for Calman filtering, the relevant content of the mathematical background of high demand, interested in self Google research.

Before no copy of the relevant source code implements found on GitHub, using Java, into iOS after directly used found that modify the results really play a certain help, can draw a path smoothing, filter out obvious burrs.

The following is the source of the data before and after the test results show

High moral map SDK real-time rendering running path -Swift implementation
High moral map SDK real-time rendering running path -Swift implementation

Interested to see their own source code to modify the use of the address below:

Java implementation of KalmanFilter

< end >;)