Track motion tracks based on Baidu maps SDK

Tags: motion track, Baidu maps, SDK

Http://orchan.github.io/2016/02/22/ tracks motion tracks based on Baidu maps SDK


The final effect is shown in the following figure

  • Note 1: the simulator will set up the Location (if you choose None, you can’t locate it), and the first time you open the application, you’ll prompt for permission to use the location information. Click permission to do so.
Track motion tracks based on Baidu maps SDK
settings
Track motion tracks based on Baidu maps SDK
effect diagram 1
  • Note that 2: is checked by latitude and longitude on the map. This is a state of the United States, so there is no information available on Baidu maps.

First, the preparatory work for the 1 new Xcode 2 Bundle Identifier 3 for engineering application of key 4 into the framework configuration engineering 5 BMKMapManager two, initialize the combat record the motion track of 1 initial work began in 2 position 3 store the updated user location 4 track line drawing

First, preparatory work

First need to login Baidu open platform download iOS map SDK (embedded developer documentation framework and Demo sample), recommended download new Framework static library form, configuration is more simple and convenient, specific see below:

Track motion tracks based on Baidu maps SDK
Baidu open platform
Track motion tracks based on Baidu maps SDK
framework static library

1. new Xcode project

File-> New-> Project-> Single; View Application; fill in relevant information; complete project establishment.

Track motion tracks based on Baidu maps SDK
new project

2. get Bundle Identifier

Through project-> target-> general, you can see the application of Bundle Identifie, we need this string of string to Baidu development platform to apply for a Key for Baidu maps call.

Track motion tracks based on Baidu maps SDK
Bundle Identifie

3. apply for key

Baidu open platform login, click on the upper right corner of the API console application into the key interface, click create applications on the security code in your application’s Bundle Identifie, fill out the specific information please see below:

Track motion tracks based on Baidu maps SDK
apply for key information, fill in Figure 1
Track motion tracks based on Baidu maps SDK
apply for key information, fill in Figure 2

4. import framework configuration projects

The first step, the introduction of BaiduMapAPI.framework
Baidu map SDK provides two kinds of environment simulator and real machine used by framework, libs/Release-iphonesimulator and libs/Release-iphoneos were stored in the folder, developers can use the prototype or simulator package, if you need to use at the same time prototype and simulator package, you can use the lipo command to the device or emulator framwork package the BaiduMapAPI files into a generic file, as follows: lipo -create Release-iphoneos/BaiduMapAPI.framework/BaiduMapAPI Release-iphonesimulator/BaiduMapAPI.framework/BaiduMapAPI command -output Release-iphoneos/BaiduMapAPI.framework/BaiduMapAPI at the Release-iphoneos folder under the BaiduMapAPI.framework can be also used for prototype and simulator. Copy the required BaiduMapAPI.framework to the project folder. In TARGETS-&gt, Build, Phases-&gt, Link, Binary, With, Libaries, click the + button, click the “Add Other” button in the pop-up window, and select the BaiduMapAPI.framework file to add to the project. Note:
is realized by using ObjectC++ static library, so you need to ensure that there is at least one.Mm suffix in your project source files (you can use any.M file suffix renamed.Mm), or the specified compile mode in engineering properties, the upcoming Xcode Project – GT Edit Active Target; -> Build -> GCC4.2 – Language -&gt Compile Sources As is set to Objective-C++. The second step, the introduction of the required system library of
Baidu map SDK provides positioning function and animation, v2.0.0 version began to use OpenGL rendering, so you need to introduce CoreLocation.framework and QuartzCore.framework, OpenGLES.framework,
, SystemConfiguration.framework CoreGraphics.framework,
Security.framework in your Xcode project. Add mode: in the Xcode Project -> Active Target -> Build Phases -> Link Binary With Libraries, add these framework can be. The third step, the environment configuration
, adds -ObjC in TARGETS-&gt, Build Settings-&gt, and Other Linker Flags. The fourth step, the introduction of mapapi.bundle
resource file if you use basic map function, you need to add the resources, otherwise the map can not display the
mapapi.bundle stored in the default location, pin View and the key points of the line marked Photo resources, also stores the vector map drawing required resource file. If you do not need to use the built-in picture display feature, you can delete the image folder in the bundle file. You can also arbitrarily replace or delete the picture file of the image folder in the bundle according to specific requirements.
method: select the project name, and select Add Files to “project name” in the right-click menu”… Select the mapapi.bundle file from the BaiduMapAPI.framework||Resources file, and tick the “Copy items if needed” check box, click the Add button to add the resource file to the project. The fifth step, the introduction of
into the header file header file in the use of SDK class #import: < BaiduMapAPI/BMapKit.h> / / introduced all the header files of #import < BaiduMapAPI/BMKMapView.h> / / only the introduction of a single header file required – quote from Baidu open platform iOS SDK environment configuration


5. initialize BMKMapManager

  • Add the definition of BMKMapManager in AppDelegate.m:
@interface, AppDelegate () < BMKGeneralDelegate> @property (nonatomic, strong) BMKMapManager *mapManager; @end
  • Comply with the BMKGeneralDelegate to achieve its two agent method, the purpose is to learn the application of networking success, successfully authorized:
- (void) onGetNetworkState: (int iError) {if (0 = = iError) {NSLog (@ "networking success");} else{(NSLog @ onGetNetworkState%d ", iError);}} - (void) onGetPermissionState: (int iError) {if (0 = = iError) {NSLog (@" authorized successfully ");} else {NSLog (@ onGetPermissionState%d", iError);}}
Track motion tracks based on Baidu maps SDK
BMKGeneralDelegate.h
  • In the AppDelegate.m file, add the initialization of the BMKMapManager and fill in the application authorization Key, as follows:
- (BOOL) application: (UIApplication * application) didFinishLaunchingWithOptions: (NSDictionary * launchOptions) {/ / to use Baidu maps to instantiate BMKMapManager self.mapManager = [[BMKMapManager alloc]init]; / / if you want to focus on the network and the authentication event, please set the generalDelegate parameter BOOL RET [self.mapManager start:@ = "OjYbYha0YULmuLPaHT9wxxx" generalDelegate:self]; if (! RET) {NSLog (@ "manager start failed return YES");};}

Two. Record the movement track of actual combat

A complete trajectory is composed of a set of geographical coordinates of an array of anchor point connection, we need real-time monitoring of user location changes, will be in compliance with the new qualification position data stored in the data, draw a line method to call the SDK in the draw trajectory.


1. initialization

@interface ViewController (<); BMKMapViewDelegate, BMKLocationServiceDelegate> / * * recorded last position / @property (nonatomic, strong) CLLocation *preLocation; / * * * / @property array position (nonatomic, strong) NSMutableArray *locationArrayM; / * * * / @property line (nonatomic, strong) BMKPolyline *polyLine; / * * * / Baidu map View @property (nonatomic, strong) BMKMapView *mapView; / * * * / Baidu location map service @property (nonatomic, strong) BMKLocationService *bmkLocationService; @end @implementation ViewController (void) viewDidLoad {[super viewDidLoad]; / / initialize Baidu location-based services [self initBMLocationService]; / / self.mapView = [[BMKMapView alloc] to initialize the map window initWithFrame:self.view .bounds] [self setMapViewProperty]; / / some attributes set MapView; [self.view addSubview:self.mapView] @end;}
  • After initializing MapView, set some of its properties:
* / / * * * set some of the attributes of the Baidu MapView - setMapViewProperty (void) {/ / display the orientation layer self.mapView.showsUserLocation = YES; self.mapView.userTrackingMode = BMKUserTrackingModeNone / / set positioning mode; / / allow rotation map self.mapView.rotateEnabled = YES; / / display scale self.bmkMapView.showMapScaleBar = YES; self.bmkMapView.mapScaleBarPosition = CGPointMake (self.view.frame.size.width - 50, self.view.frame.size.height - 50); / / location layer custom style the parameters of BMKLocationViewDisplayParam *displayParam alloc]init] = [[BMKLocationViewDisplayParam; displayParam.isRotateAngleValid = NO; / / follow state rotation angle is in force displayParam.isAccuracyCircleShow = NO; / / accuracy That ring is displayParam.locationViewOffsetX = 0; / / offset location (longitude) displayParam.locationViewOffsetY = 0; / / offset location (latitude) @ displayParam.locationViewImgName = "walk"; [self.mapView updateLocationViewWithParam: displayParam];}
  • Parameter setting of Baidu location service:
* * / / * * initializes the Baidu location service (void) - initBMLocationService {/ / initialization Baidu self.bmkLocationService = [[BMKLocationService alloc] init] location service; / / set distance filter, said each mobile 10 update a position [BMKLocationService setLocationDistanceFilter:10]; / / set the positioning accuracy of [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyBest];}

2. start positioning

[self.bmkLocationService startUserLocationService] / / open location services; / / set the current map display area, display directly to the user's location BMKCoordinateRegion adjustRegion = [self.mapView regionThatFits:BMKCoordinateRegionMake (self.bmkLocationService.userLocation.location.coordinate, BMKCoordinateSpanMake (0.02f, 0.02f))]; [self.mapView setRegion:adjustRegion animated:YES];

You can learn the location updates as long as you follow the BMKLocationServiceDelegate protocol, and you need to implement the following proxy methods:

* / * * positioning failure will call this * * @param error * / - error message (void) didFailToLocateUserWithError: (NSError * error) {NSLog (@ did failed locate, error is, [error localizedDescription]% @ ");} / * * * user location update, will call this function * @param userLocation * / - new user location (void) didUpdateBMKUserLocation: (BMKUserLocation * userLocation) {/ / if the location update level of accuracy is more than 10 meters, the method can be used to directly return / simply judge the signal strength of if GPS (userLocation.location.horizontalAccuracy > kCLLocationAccuracyNearestTenMeters) {return}}; / * * * user direction after the update, will call this function * @param userLocation new the position of the user - * / (void) didUpdateUserHea Ding: (BMKUserLocation * userLocation) {/ / dynamic update location data to my [self.mapView updateLocationData:userLocation];}

3. store the updated user location

* / * * * * @param userLocation began recording track updating location information * / - (void) recordTrackingWithUserLocation: (BMKUserLocation *) userLocation if (self.preLocation) {{/ / calculate the location data and the distance between the last location data CGFloat distance = [userLocation.location distanceFromLocation:self.preLocation]; self.statusView.distanceWithPreLoc.text [NSString stringWithFormat:@ = "%.3f", "NSLog (@ distance]; with a point distance:%f, distance); / / (5 m limit storage array line drawing) if the distance is less than 5 meters, neglecting the data directly back to the method of if (distance < 5) {return};} / / 2 will meet the position stored in the array (1 Come here) [self.locationArrayM addObject:userLocation.location]; self.preLocation = userLocation.location; / / the 3 drawing [self drawWalkPolyline];}

4. draw the trace line

* * / / * * rendering route - (void drawWalkPolyline) {/ / track point array number NSUInteger count = self.locationArrayM.count; / / BMKMapPoint / / dynamic allocation of storage space is a structure of geographic coordinates, with geographical coordinates: X angle abscissa ordinate by BMKMapPoint Y: *tempPoints = new BMKMapPoint[count] / [self.locationArrayM enumerateObjectsUsingBlock:^ traversing arrays; (CLLocation *location, NSUInteger IDX, BOOL *stop) {BMKMapPoint locationPoint = BMKMapPointForCoordinate (location.coordinate); tempPoints[idx] = locationPoint;}}]; / / remove the original drawing, avoid in the original track redraw if (self.polyLine) {[self.mapView removeOverlay:self.polyLine];} Through the construction of BMKPolyline points / / self.polyLine = [BMKPolyline polylineWithPoints:tempPoints count:count]; / / add a line drawing, if (self.polyLine) {[self.mapView addOverlay:self.polyLine];} / / empty temporary tempPoints array delete []tempPoints; / / set based on polyline map range [self mapViewFitPolyLine:self.polyLine];}

Feedback and suggestion

– Github: https://github.com/orchan/BDTrackDemo/tree/alphaBranch

Thank you for reading this help file. If you find it useful, share it with your social circle. Please also indicate the source. Thank you!