The use of IOS Baidu Map (general positioning, anti-geo-coding) _ios

Source: Internet
Author: User
Tags map class

iOS Location-normal location (no map)-Anti-geocoding (get specific location), the following code to explain to everyone, the code is as follows:

#import <CoreLocation/CoreLocation.h> used header file to introduce corelocation this package <CLLocationManagerDelegate> use of the proxy name// 1. Use the location service//Set the app has access to location services//In the use of the application/Always (app in the background)//info.plist file add the following two (or one):// Nslocationwheninuseusagedescription//nslocationalwaysusagedescription always//2.locationmanager object management-related positioning services during use of the application _
 Manager = [[Cllocationmanager alloc] init]; Manager Judge: Whether the mobile phone to open positioning/app has access to the right//[cllocationmanager locationservicesenabled]; Mobile phone is open positioning//[cllocationmanager authorizationstatus]; The status of the app access-positioned permissions if (![ Cllocationmanager Locationservicesenabled] | | [Cllocationmanager Authorizationstatus]!= kclauthorizationstatusauthorizedwheninuse) {[_manager Requestwheninuseauthorization];
 Permission to request access to the location service from the user} _manager.delegate = self;
 _manager.desiredaccuracy = Kcllocationaccuracybest;
 _manager.distancefilter = 1.0f;
[_manager startupdatinglocation]; Positioning agent latitude and longitude callback-(void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) newlocation Fromlocation:(cllocation *) oldlocation {[_manager stopupdatinglocation];
 Clgeocoder * Geocoder = [[Clgeocoder alloc] init]; [Geocoder reversegeocodelocation:newlocation completionhandler:^ (Nsarray *placemarks, NSError *error)
   {for (Clplacemark * Placemark in Placemarks) {nsdictionary *test = [Placemark addressdictionary];
  Country (country) state (city) sublocality (district) name NSLog (@ "%@", [Test objectforkey:@ "Name"]);
}
 }];

 }

The use of iOS Baidu map (general positioning, anti-geo code)

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
)];//add Mapview [Self.view Addsubview:_mapview];
 #pragma mark-Set Mapview Property-(void) Setmapviewproperty {_mapview.maptype = bmkusertrackingmodefollowwithheading; _mapview.showsuserlocation = YES; Whether to show the positioning layer (that is, the dot of my position) _mapview.zoomlevel = 16;//Map Display scale _mapview.rotateenabled = NO;
Sets whether [self passlocationvalue] can be rotated;
 #pragma mark-in-place coordinates//settings are positioned to the user's location, here is a simple application method (must be opened when the program has been acquired geographical coordinates, in order to solve map positioning always first show Tiananmen)-(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 anchor dot Properties-(void) Setuserimage {//User location class bmklocationviewdisplayparam* param = [[Bmklocationviewdisplaypara
 M alloc] init]; Param.locationviewoffseTY = 0;//Offset param.locationviewoffsetx = 0;
 Param.isaccuracycircleshow =no;//Set whether to display the positioning of the precision circle param.isrotateanglevalid = NO;
[_mapview Updatelocationviewwithparam:param];
 }

So the basic map interface is coming out.

If you need to make some requests on the map, you can implement Bmkmapviewdelegate, here are some protocol methods for Mapview

* * *
 @param mapview map View
 * @param animated whether animation/
-(void) Mapview: (Bmkmapview *) Mapview regionwillchangeanimated: (BOOL) animated
{
 //todo
}
 
/**
 * The map area will call this interface after the change is completed
 * @param mapview map View
 * @param animated animation/
-(void) Mapview: (Bmkmapview *) Mapview Regiondidchangeanimated: (BOOL) animated
{
 //todo
}
/**
 * This interface will be called when the map status changes are completed
 * @param Mapview map View
 *
/(void) mapstatusdidchanged: (Bmkmapview *) Mapview
{
 //todo
}

2. Map positioning

My side is to encapsulate an independent manager class to manage positioning and slide to location on the map, to separate the location function from the map Mapview, and to manage the change of geographic moving position.

#import <Foundation/Foundation.h> #import "BMapKit.h" @interface Userlocationmanager:nsobject <
 bmkmapviewdelegate,bmklocationservicedelegate> {cllocation *cllocation;
Bmkreversegeocodeoption *reversegeocodeoption;//Inverse geo-coding} @property (strong,nonatomic) Bmklocationservice *locService;
City name @property (strong,nonatomic) NSString *cityname;
User Latitude @property (nonatomic,assign) double userlatitude;
User Longitude @property (nonatomic,assign) double userlongitude;
User Location @property (strong,nonatomic) cllocation *clloction;
Initialization of single case + (Userlocationmanager *) sharedinstance;
Initialize Baidu Map user Position management class-(void) initbmkuserlocation;
Start positioning-(void) startlocation;
Stop positioning-(void) stoplocation;
 @end #import "UserLocationManager.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 initbmkuserlocation];
return self; #pragma initialize Baidu map user Location Management class/** * Initialize Baidu Map User Location Management class * *-(void) initbmkuserlocation {_locservice = [[Bmklocationservice alloc
 ]init];
 _locservice.delegate = self;
[Self startlocation];  #pragma open the location service/** * Open the Location service/-(void) startlocation {[_locservice startuserlocationservice];} #pragma turn off the positioning service/** * Close positioning service/-(void) stoplocation {[_locservice stopuserlocationservice];} #pragma bmklocationservicedelegate/** * User location is more When new, this function is called * @param userlocation New User location/-(void) Didupdateuserlocation: (bmkuserlocation *) userlocation {cllocation =
 Userlocation.location;
 _clloction = cllocation;
 _userlatitude = Cllocation.coordinate.latitude;
 _userlongitude = Cllocation.coordinate.longitude; [Self stoplocation];(if a real-time location is required without stopping the location service)}/** * calls this function after it stops positioning (void) Didstoplocatinguser {;}/** * When the location fails *@p Aram Error Error number/-(void) Didfailtolocateuserwitherror: (Nserror *) error {[Self stoplocation];}

The above code is the use of iOS Baidu map (general positioning, anti-geo code), I hope that the future work and learning for everyone to help.

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.