Use of the iOS Baidu map

Source: Internet
Author: User
Tags map class

Here is not the specific configuration of the introduction of Baidu, configuration see HTTP://DEVELOPER.BAIDU.COM/MAP/INDEX.PHP?TITLE=IOSSDK

1. First accept the basic map function

Create a new map class, xib drag also line, my side is the code implementation.

 _mapview = [[bmkmapview alloc]initwithframe:cgrectmake (0, 0, Self.view.frame.size.width, self.view.frame.size.height)]; [self.view addsubview:_mapview];# pragma mark -  Setting Mapview Property-(void) Setmapviewproperty{    _mapview.maptype  = bmkusertrackingmodefollowwithheading;    _mapview.showsuserlocation =  yes;    _mapview.zoomlevel = 16;    _ mapview.rotateenabled = no;        [self   Passlocationvalue];} #pragma  mark -incoming location coordinates   (you must have obtained geolocation coordinates when you open the program, always show Tiananmen first in order to resolve map positioning)-(void) passlocationvalue{     bmkcoordinateregion viewregion = bmkcoordinateregionmake ([UserLocationManager  sharedinstance].clloction.coordinate, bmkcoordinatespanmake (0.02f,0.02f));     Bmkcoordinateregion adjustedregion = [_mapview regionthatfits:viewregion];    [_mapview setregion:adjustedregion  animated:yes];    } #pragma  mark -set the Anchor dot Property-(void) setuserimage{     bmklocationviewdisplayparam* param = [[bmklocationviewdisplayparam alloc] init] ;    param.locationviewoffsety = 0;     param.locationviewoffsetx = 0;    param.isaccuracycircleshow =no;     param.isRotateAngleValid = NO;    [_mapView  Updatelocationviewwithparam:param];}

To implement Bmkmapviewdelegate, here are some protocol methods for Mapview

* * This interface will be called when the map area is about to change * @param mapview map View * @param animated whether the animation */-(void) Mapview: (Bmkmapview *) Mapview Regionwillchange Animated: (BOOL) animated{//todo}/** * This interface will be called when the map area is changed * @param mapview map View * @param animated whether animation */-(void) Mapview: (BMK Mapview *) Mapview regiondidchangeanimated: (BOOL) animated{//todo}/** * This interface will be called after the map state change is complete * @param mapview map view */-(void) Mapstatusdidchanged: (Bmkmapview *) mapview{//todo}

So the basic map interface is out.


2. Map positioning

I'm on my side. Encapsulates a standalone manager class to manage positioning and slide-to-position on the map

#import   "UserLocationManager.h" #import   "BMKGeocodeSearch.h" @implementation  userlocationmanager+   (userlocationmanager *) sharedinstance{    static userlocationmanager  *_instance = nil;         @synchronized   (self)  {         if  (_instance == nil)  {             _instance = [[self alloc] init];         }    }         return _instance;} -(ID) init{    if  (self == [super init])     {         //[self getBMKUserLocation];         //[self initgeocodesearch];    }     return self;} #pragma   Initialize Baidu map user Location management class/** *   initialize Baidu Map user location management  */-  (void) initbmkuserlocation; {    _locservice = [[bmklocationservice alloc]init];     _locservice.delegate = self;    [self startlocation];} #pragma   Open Location Services/** *   Open Location Services  */-(void) startlocation{    [_ Locservice startuserlocationservice];} #pragma   Close Location Services/** *   turn off location services  */-(void) stoplocation{    [_ Locservice stopuserlocationservice];} This function is called when the  bmklocationservicedelegate/** * user location update is #pragma  * @param  userLocation  New User Location  */-  (void) Didupdateuserlocation: (bmkuserlocation *) userlocation{      cllocation = userlocation.location;    _clloction = cllocation;     _userlatitude = cllocation.coordinate.latitude;    _userlongitude = cllocation.coordinate.longitude;     [self stoplocation];    [self initgeocodesearch];} /** * This function is called when the location is stopped  */-  (void) didstoplocatinguser{    [_mapviewvc  Setmapviewproperty];} This function is called when the/** * location fails  * @param  error  error number  */-  (void) Didfailtolocateuserwitherror: ( nserror *) error{    [self stoplocation];} #pragma  bmkgeocodesearchdelegate-(void) initgeocodesearch{    _geocodesearch =  [[BMKGeoCodeSearch alloc]init];    _geoCodeSearch.delegate = self;     bmkreversegeocodeoption *reversegeocodeoption= [[bmkreversegeocodeoption  alloc] init];    reverseGeoCodeOption.reverseGeoPoint =  Cllocation.coordinate;    [_geocodesearch reversegeocode:Reversegeocodeoption];} /** * returns the anti-geocoding search results  * @param  searcher  Search Object  * @param  result  search Results  * @param  error  error number, @see  bmksearcherrorcode */-(void)  ongetreversegeocoderesult: ( bmkgeocodesearch *) Searcher result: (bmkreversegeocoderesult *) Result errorcode: ( Bmksearcherrorcode) error{    if  (error == 0)     {         _cityName = result.addressDetail.city;         nslog (@ "%@", _cityname);     }} @end

Follow up to update

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.