Use Cocospod to import Baidu map details see document
Use Baidu Map to get the key
2 Configuration Items
In info
<key>nsapptransportsecurity</key> <dict<key> Nsallowsarbitraryloads</key<true</dict>
Ensure network availability
<key>lsapplicationqueriesschemes</key> <array<string>baidumap </string</array>
Ensure Baidu map can be called
Import Feature file header
#import<baidumapapi_base/bmkbasecomponent.H>Introduce all the header files related to base #import<baidumapapi_map/bmkmapcomponent.H>Introduce all the header files of the map function #import<baidumapapi_search/bmksearchcomponent.H>Introduction of all header files for the retrieval function #import<baidumapapi_cloud/bmkcloudsearchcomponent. H>//Introduction to the Cloud Retrieval function All header files #import <baidumapapi_ Location/bmklocationcomponent. H>//introduction of positioning function All header files #import <baidumapapi_utils< Span class= "Sy0" >/bmkutilscomponent. h>//introduction of calculation tool All header files #import < Baidumapapi_radar/bmkradarcomponent. H>//introduction of peripheral radar function All header files #import < baidumapapi_map< Span class= "Sy0" >/bmkmapview. h>//introduces only the desired single-header file
Positioning
Initialize Baidu map and location
Mapview = [[Bmkmapview alloc]initwithframe:cgrectmake (0, +, [UIScreen mainscreen].bounds.size.width, [UIScreen MAINSCREEN].BOUNDS.SIZE.HEIGHT-50)];
[Self.view Addsubview:mapview];
Locservice = [[Bmklocationservice alloc]init];
Set up Proxy
Locservice.delegate = self;
Start Locationservice
[Locservice Startuserlocationservice];
The following _mapview are Bmkmapview objects
Agent
#pragma mark-locationinfomation (user location information)
/**
* This function is called after the user orientation is updated
* @param userlocation New User Location
*/
-(void) didupdateuserheading: (bmkuserlocation *) userlocation
{
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
if (userlocation) {
NSLog (@ "Heading is%@", userlocation.heading);
}
});
}
Processing location coordinates update to locate the current location
-(void) Didupdatebmkuserlocation: (bmkuserlocation *) userlocation
{
if (userlocation) {
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Current latitude and longitude
NSLog (@ "didupdateuserlocation lat%f,long%f", UserLocation.location.coordinate.latitude, UserLocation.location.coordinate.longitude);
_STR = [nsmutablestring stringwithformat:@ "%f,%f", UserLocation.location.coordinate.latitude, UserLocation.location.coordinate.longitude];
Self.latitude = UserLocation.location.coordinate.latitude;
Self.longitude = UserLocation.location.coordinate.longitude;
Update my location data
[Mapview updatelocationdata:userlocation];
Geo Search service (converting latitude and longitude to address, city, etc., known as reverse geocoding)
Self.searcher = [[Bmkgeocodesearch alloc]init];
Self.searcher.delegate = self;
Cllocationcoordinate2d point = (cllocationcoordinate2d) {self.latitude,self.longitude};
Bmkreversegeocodeoption *reversegeocodesearchoption = [[Bmkreversegeocodeoption alloc]init];
Reversegeocodesearchoption.reversegeopoint = point;
BOOL flag = [Self.searcher reversegeocode:reversegeocodesearchoption];
if (flag)
{
NSLog (@ "anti-geo search sent successfully");
}
Else
{
NSLog (@ "anti-geo retrieval send failed");
}
});
}
}
#pragma mark-ongetreversegeocoderesult (Reverse geocoding results)
Receive Reverse geocoding results
-(void) Ongetreversegeocoderesult: (Bmkgeocodesearch *) Searcher Result: (Bmkreversegeocoderesult *) result ErrorCode: ( Bmksearcherrorcode) Error
{
if (Error = = Bmk_search_no_error) {
The results of the reverse geocoding are printed here, including information such as city, address, etc.
NSLog (@ "Test results%@%@", result.addressdetail.city,result.address);
_placename.text = result.address;
}
else {
NSLog (@ "Sorry, no results found");
}
}
#pragma mark-configdelegate (set up agent and cancel agent)
Set up Proxy
-(void) Viewwillappear: (BOOL) animated
{
[Mapview Viewwillappear];
Mapview.delegate = self;
}
Remember to set nil when not in use, otherwise affect the release of memory
-(void) Viewwilldisappear: (BOOL) animated
{
[Mapview Viewwilldisappear];
Mapview.delegate = nil; When not in use, place nil
_searcher.delegate = nil;
}
#pragma mark-checknetwork (check network and authorization)
Check network status
-(void) Ongetnetworkstate: (int) Ierror
{
if (0 = = Ierror) {
NSLog (@ "Networking success");
}
else{
NSLog (@ "Ongetnetworkstate%d", ierror);
}
}
#pragma mark-bmkmapviewdelegate (map callout and route color)
/**
* Generate the corresponding view according to overlay
* @param mapview map view
* @param overlay specified by overlay
* @return-generated overlay view
*/
-(bmkoverlayview*) Mapview: (Bmkmapview *) map Viewforoverlay: (id<bmkoverlay>) Overlay
{
if ([Overlay Iskindofclass:[bmkpolyline class]]) {
bmkpolylineview* Polylineview = [[Bmkpolylineview alloc] initwithoverlay:overlay];
Polylineview.fillcolor = [[Uicolor alloc] initwithred:0 green:1 blue:1 alpha:1];
Polylineview.strokecolor = [[Uicolor alloc] initwithred:0 green:0 blue:1 alpha:0.7];
Polylineview.linewidth = 3.0;
return polylineview;
}
return nil;
}
Open follow
Mapview.usertrackingmode = Bmkusertrackingmodefollow;
Mapview.showsuserlocation = yes;//Show positioning layer
It's not very good to write a preservation role for yourself.
iOS Growth path Baidu map