IOS Baidu Map Simple use of detailed _ios

Source: Internet
Author: User

Baidu map iOS SDK is a set of iOS 5.0 and above version of the application interface, not only provide the basic interface of the map, but also provide POI search, path planning, map tagging, off-line map, positioning, surrounding radar, such as a wealth of LBS capabilities.

Today mainly introduces the following interface

    • Basic map
    • POI Search
    • Positioning

Configure the environment first

1. Automatic configuration. Framework Form development package (using Cocoapods) < recommendation >

2. Manual configuration. Framework Form Development Package

Special attention:
(The API has a lot of attention, you can specifically to see.) but I said that the latter two are less than one of them will fail, 1th, there is demand, must add)

1, if the use of iOS9 in the Baidu Map client function, must be in the "Info.plist" in the following configuration, otherwise can not adjust the Baidu map client.

  <key>LSApplicationQueriesSchemes</key>
  <array>
    <string>baidumap</string>
  </array>

2, since the iOS SDK v2.5.0, in order to iOS8 positioning ability to do compatible, need to add in Info.plist (the following two selected, two are added by default to use Nslocationwheninuseusagedescription):
Nslocationwheninuseusagedescription, allows you to obtain a GPS description when using the foreground
Nslocationalwaysusagedescription, allow permanent use of GPS description

3, in the use of XCODE6 SDK development process, you need to add in the Info.plist: Bundle display Name, and its value can not be empty (Xcode6 new project does not have this configuration, if it does not cause manager start fail

After configuration is complete

APPDELEGATE.M file to add initialization to Bmkmapmanager and fill in the requested authorization key

#import "AppDelegate.h"
#import <BaiduMapAPI_Base/BMKMapManager.h>
@interface appdelegate ()

@ End

@implementation appdelegate


-(BOOL) Application: (UIApplication *) application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions {

//Create and initialize an engine object
  bmkmapmanager *manager = [ Bmkmapmanager alloc] init];
Start map engine
  BOOL success = [Manager start:@ "Zbwlngrurtp9cvb5ez6gzpnebljmyylo" generaldelegate:nil];

  if (!success) {
    NSLog (@ "failed");
  }
  Override point for customization after application launch.
  Return YES
}

1. Basic map

#import "ViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
@interface Viewcontroller () < Bmkmapviewdelegate>

@property (nonatomic,strong) bmkmapview *mapview;//map view
@end

@implementation Viewcontroller

-(void) viewdidload {
  [super viewdidload];
   Initialize map
  Self.mapview = [[Bmkmapview alloc] initWithFrame:self.view.frame];
  Self.mapView.delegate =self;
  Set the display style of a map
  Self.mapView.mapType = bmkmaptypesatellite;//satellite map

  //Set map open Road layer
  self.mapView.trafficEnabled = YES;

  The bottom figure poi mark
  self.mapView.showMapPoi = NO;

  Currently available on the mobile phone level is 3-21
  self.mapView.zoomLevel =;

  Set map view can support rotating
  self.mapView.rotateEnabled = NO;

  Set map view can support users to move the map
  self.mapView.scrollEnabled = NO;

  Add to view
  [Self.view AddSubview:self.mapView];

  There are a number of properties to view the API based on requirements
}

Running effect into the next;

2. Positioning

#import "ViewController.h" #import <BaiduMapAPI_Map/BMKMapView.h> #import <baidumapapi_location/ Bmklocationservice.h> @interface Viewcontroller () <BMKLocationServiceDelegate,BMKMapViewDelegate> @ Property (Nonatomic,strong) Bmkmapview *mapview;//map View @property (nonatomic,strong) Bmklocationservice *service;//

   Location Service @end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload];
  Initialize map Self.mapview = [[Bmkmapview alloc] initWithFrame:self.view.frame];

  Self.mapView.delegate =self;

  Add to view [Self.view AddSubview:self.mapView];

  Initialize location self.service = [[Bmklocationservice alloc] init];

  Set Agent self.service.delegate = self;


  Open positioning [Self.service startuserlocationservice];
Do no additional setup after loading the view, typically from a nib. #pragma mark-------bmklocationservicedelegate/** * User location is updated to call this function * @param userlocation New User location/-(void) Didupdat Ebmkuserlocation: (bmkuserlocation *) userlocation {//ShowPositioning self.mapView.showsUserLocation = YES;

  Update location data [Self.mapview updatelocationdata:userlocation];

   Gets the user's coordinate self.mapView.centerCoordinate = userLocation.location.coordinate;

Self.mapView.zoomLevel = 18;

 }

Run results

POI Search

#import "ViewController.h" #import <BaiduMapAPI_Map/BMKMapView.h> #import <baidumapapi_location/ bmklocationservice.h> #import <BaiduMapAPI_Search/BMKPoiSearch.h> #import <baidumapapi_map/ bmkannotation.h> #import <BaiduMapAPI_Map/BMKPointAnnotation.h> #import <baidumapapi_map/ bmkpinannotationview.h> #define Kwidth [UIScreen mainscreen].bounds.size.width @interface Viewcontroller () < Bmklocationservicedelegate,bmkpoisearchdelegate,bmkmapviewdelegate> @property (nonatomic,strong) BMKMapView * mapview;//map View @property (nonatomic,strong) bmklocationservice *service;//positioning Service @property (Nonatomic,strong)
Bmkpoisearch *poisearch;//Search Service @property (nonatomic,strong) Nsmutablearray *dataarray; @end @implementation Viewcontroller-(Nsmutablearray *) DataArray {if (!_dataarray) {_dataarray = [Nsmutablearra

  Y array];

return _dataarray;

  }-(void) viewdidload {[Super viewdidload]; Initialize map Self.mapview = [[Bmkmapview alloc] InitwithfraMe:self.view.frame];
Self.mapView.delegate =self; Set the display style of the map//Self.mapView.mapType = bmkmaptypesatellite;//satellite map//////Set the traffic//self.mapView.trafficEnabled = YE
S
//Bottom Figure poi Callout//Self.mapView.showMapPoi = NO;
//On the mobile phone currently available at level 3-21//self.mapView.zoomLevel = 21;
//rotate//self.mapView.rotateEnabled = NO;
//drag/self.mapView.scrollEnabled = NO;

  [Self.view AddSubview:self.mapView];

  Initialize location self.service = [[Bmklocationservice alloc] init];

  Set Agent self.service.delegate = self;


  Open positioning [Self.service startuserlocationservice];
Do no additional setup after loading the view, typically from a nib. #pragma mark-------bmklocationservicedelegate/** * User location is updated to call this function * @param userlocation New User location/-(void) DIDUPDA

  Tebmkuserlocation: (bmkuserlocation *) userlocation {//display positioning self.mapView.showsUserLocation = YES;

  Update location data [Self.mapview updatelocationdata:userlocation]; Gets the user's coordinates Self.mapView.centerCooRdinate = userLocation.location.coordinate;

 

  Self.mapView.zoomLevel = 18;


  Initialize search Self.poisearch =[[bmkpoisearch alloc] init];

 

  Self.poiSearch.delegate = self;

  Initializes a peripheral cloud retrieval object Bmknearbysearchoption *option = [[Bmknearbysearchoption alloc] init];

  Index defaults to 0 option.pageindex = 0;

  The number of pages defaults to ten option.pagecapacity = 50;

  Search radius Option.radius = 200;

  The central point of the search, by latitude option.location = userLocation.location.coordinate;

 

   Search the keyword Option.keyword = @ "Snack";
  Initiate a perimeter search based on center point, radius, and search terms BOOL flag = [Self.poisearch poisearchnearby:option];
    if (flag) {NSLog (@ "search succeeded");
  Close positioning [Self.service stopuserlocationservice];
  else {NSLog (@ "search failed"); #pragma mark-------bmkpoisearchdelegate/** * Returns POI search results * @param searcher Search Objects * @param poiresult search results list * @param ErrorCode error number, @see Bmksearcherrorcode */(void) Ongetpoiresult: (Bmkpoisearch *) Searcher Result: (Bmkpoiresult *) Poiresult errorcode: (bmksearcherrorcode) ErrorCode {//If the search succeeds if (erRorcode ==bmk_search_no_error) {//poi Information class//poi list for (Bmkpoiinfo *info in poiresult.poiinfolist) {[s

      Elf.dataarray Addobject:info];

      Comment that initializes a point//only three attributes bmkpointannotation *annotoation = [[Bmkpointannotation alloc] init];

      Coordinate annotoation.coordinate = info.pt;

      Title Annotoation.title = Info.name;

      Sub-headings annotoation.subtitle = info.address;
    Add annotations to the map [Self.mapview addannotation:annotoation]; /** * Returns POI details search results * @param searcher Search Object * @param poidetailresult details Search results * @param errorcode error number, @see Bmksearche Rrorcode * *-(void) Ongetpoidetailresult: (Bmkpoisearch *) Searcher Result: (Bmkpoidetailresult *) Poidetailresult

ErrorCode: (Bmksearcherrorcode) errorcode {NSLog (@ "%@", poidetailresult.name); #pragma mark-------------bmkmapviewdelegate/** * Generates a corresponding view @param mapview map view * @param annotation refers to Bmkannotationview * @return Generated Callout View * * * Mapview: (Bmkmapview *) maPView viewforannotation: (id<bmkannotation>) Annotation {//If it is an annotation point if ([annotation Iskindofclass:[bmkpointannota tion class]] {/////According to the annotation point, create and initialize the annotation point view bmkpinannotationview *newannotation = [[Bmkpinannotationview alloc] Initwitha

    Nnotation:annotation reuseidentifier:@ "an"];

    Set the color of the pin newannotation.pincolor = bmkpinannotationcolorred;

    Set animation Newannotation.animatesdrop = YES;

  return newannotation;
return nil; /** * When a annotation views are selected, call this interface * @param mapview map view * @param views Selected annotation views * * (void) Mapview: (Bmkma PView *) Mapview Didselectannotationview: (Bmkannotationview *) View {//poi Details Search information class bmkpoidetailsearchoption *option = [


  [Bmkpoidetailsearchoption alloc] init];

  Bmkpoiinfo *info = Self.dataArray.firstObject;

  The UID of the POI, which obtains option.poiuid = Info.uid from the BMKPOIRESULT structure returned by the POI retrieval; /** * POI details retrieved based on POI UID * Asynchronous function, returns results in bmkpoisearchdelegate ongetpoidetailresult notification * @param option POI details retrieve parameter class (bmkpoid EtailsearchoptioN) * @return successfully return Yes, otherwise return no/BOOL flag = [Self.poisearch poidetailsearch:option];
  if (flag) {NSLog (@ "retrieve success");
  else {NSLog (@ "Retrieval failed");

 }

 

}

Run results

Summarize

Baidu Map of the function is very powerful, there are many search, there is no write. Everyone is interested can delve into, after all, third party interface documents relatively clear. The above is the entire content of this article, I hope that the study of everyone to help, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.