Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Baidu Map location and check-in function by iOS

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "iOS how to achieve Baidu map location check-in function", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "iOS how to achieve Baidu map location check-in function" this article.

Write at the front:

The project needs to use this function, the main purpose is to achieve the teacher to set the location check-in range, students in a certain range of check-in function.

Brief introduction:

The following records the main implementation process, the implementation of the function is mainly based on the api documents provided by Baidu Map developer website, the combination of various functions. Baidu map SDK is now divided into map function and location function two different SDK,BaiduMapAPI this is the basic map function, BMKLocationKit this is the location function. The SDK used to implement the location check-in function in the project includes these two modules mentioned above, so when introducing framework with cocopods, you need to introduce: # Baidu Map pod 'BMKLocationKit' pod' BaiduMapKit'

Function realization

First, introduce into the APPdelegate.m file:

# import # import

Add the function code:

# pragma mark Baidu Map setup-(void) configBaiduMap {NSString * ak = @ "xxxx"; BMKMapManager * mapManager = [[BMKMapManager alloc] init]; self.mapManager = mapManager; BOOL ret = [mapManager start:ak generalDelegate:nil]; [[BMKLocationAuth sharedInstance] checkPermisionWithKey:ak authDelegate:self]; if (! ret) {NSLog (@ "manager start failed!");}}

Second, in the viewController that uses the map positioning function

# import # import / / introduce all the header files related to base # import / / introduce all the header files of the map function

Follow the protocol

Declare global variables

@ property (nonatomic, strong) BMKUserLocation * userLocation; / / current location object @ property (nonatomic, strong) BMKLocationManager * locationManager;/** locationManager*/@property (nonatomic, strong) BMKMapView * mapView;/** map * / / @ property (nonatomic, strong) BMKPointAnnotation* annotation; / * tag * / @ property (nonatomic, strong) NSMutableArray * annotationArr;/** tag array * / @ property (nonatomic, strong) NSMutableArray * circleArr;/** circular array * /

This setting is recommended in the map SDK documentation in the following code in order to control memory

-(void) viewWillAppear: (BOOL) animated {[super viewWillAppear:animated]; [_ mapView viewWillAppear]; _ mapView.delegate = self;}-(void) viewWillDisappear: (BOOL) animated {[super viewWillDisappear:animated]; [_ mapView viewWillDisappear]; _ mapView.delegate = nil;}-(void) dealloc {if (_ mapView) {_ mapView = nil;}}

Initialize the array, which will be used next

-(NSMutableArray *) annotationArr {if (! _ annotationArr) {_ annotationArr = [NSMutableArray array];} return _ annotationArr;}-(NSMutableArray *) circleArr {if (! _ circleArr) {_ circleArr = [NSMutableArray array];} return _ circleArr;}

Add Map view

# pragma mark add map-(void) addSignMapBgView {if (! self.mapBgView) {UIView * mapBgView = [UIView new]; self.mapBgView = mapBgView; mapBgView.backgroundColor = [CommUtls colorWithHexString:APP_BgColor]; [self addSubview:mapBgView]; [mapBgView makeConstraints: ^ (MASConstraintMaker * make) {make.top.equalTo (self.tipView.bottom); make.left.right.bottom.equalTo (0);}]; _ mapView = [BMKMapView alloc] initWithFrame:CGRectZero]; / / _ mapView.delegate = self [_ mapView setZoomLevel:21]; / / accurate to 5m _ mapView.showsUserLocation = YES;// display positioning layer [mapBgView addSubview:_mapView]; [_ mapView makeConstraints: ^ (MASConstraintMaker * make) {make.edges.equalTo (0);}]; _ mapView.userTrackingMode = BMKUserTrackingModeNone;}}

Initialize the map positioning: here I use a positioning instead of choosing a continuous positioning.

# pragma mark initializes locationManager- (void) initUserLocationManager {/ / because mapView is created in a separate view, assign the mapView in signSetTypeView to the mapView of the current viewcontroller here Self.mapView = self.mainView.signSetTypeView.mapView; self.mapView.delegate = self;// self.annotation = [[BMKPointAnnotation alloc] init]; / / self.mapView is a BMKMapView object / / the precision circle setting BMKLocationViewDisplayParam * param = [[BMKLocationViewDisplayParam alloc] init]; / / sets the display precision circle, default YES param.isAccuracyCircleShow = YES; / / precision circle border color param.accuracyCircleStrokeColor = [UIColor colorWithRed:242/255.0 green:129/255.0 blue:126/255.0 alpha:1] / / fill color of precision circle param.accuracyCircleFillColor = [UIColor colorWithRed:242/255.0 green:129/255.0 blue:126/255.0 alpha:0.3]; [self.mapView updateLocationViewWithParam:param]; self.userLocation = [[BMKUserLocation alloc] init]; / / initialize instance _ locationManager = [[BMKLocationManager alloc] init]; / / set delegate _ locationManager.delegate = self; / / set the coordinate system type of return position _ locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL / / set distance filtering parameter _ locationManager.distanceFilter = kCLDistanceFilterNone; / / set expected precision parameter _ locationManager.desiredAccuracy = kCLLocationAccuracyBest; / / set application location type _ locationManager.activityType = CLActivityTypeAutomotiveNavigation; / / set whether to stop position update automatically _ locationManager.pausesLocationUpdatesAutomatically = NO; / / set whether background positioning / / _ locationManager.allowsBackgroundLocationUpdates = YES; / / set location to get timeout _ locationManager.locationTimeout = 15 / / set the timeout for getting address information _ locationManager.reGeocodeTimeout = 15; / / request a location [self requestLocation];}

Request location, get latitude and longitude

# pragma mark request location-(void) requestLocation {[_ locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock: ^ (BMKLocation * _ Nullable location, BMKLocationNetworkState state, NSError * _ Nullable error) {if (error) {NSLog (@ "locError: {% ld -% @};", (long) error.code, error.localizedDescription);} if (location) {/ / get location information, add annotation if (location.location) {NSLog (@ "LOC =% @", location.location) } if (location.rgcData) {NSLog (@ "rgc =% @", [location.rgcData description]);} if (! location) {return;} if (! self.userLocation) {self.userLocation = [[BMKUserLocation alloc] init];} self.userLocation.location = location.location; [self.mapView updateLocationData:self.userLocation]; CLLocationCoordinate2D mycoordinate = location.location.coordinate; self.mapView.centerCoordinate = mycoordinate / / assign initial values self.viewModel.lat = [NSString stringWithFormat:@ "% f", location.location.coordinate.latitude]; self.viewModel.lng = [NSString stringWithFormat:@ "% f", location.location.coordinate.longitude]; self.viewModel.radius = @ "50"; / / print latitude and longitude NSLog (@ "didUpdateUserLocation lat% f long% f", location.location.coordinate.latitude,location.location.coordinate.longitude);} NSLog (@ "netstate =% d", state);}];}

The map length can be selected by the point selection function:

/ / long press the map to select points-(void) mapview: (BMKMapView *) mapView onLongClick: (CLLocationCoordinate2D) coordinate {if (self.annotationArr.count > 0) {[mapView removeAnnotations:self.annotationArr]; [self.annotationArr removeAllObjects]; BMKPointAnnotation * annotation = [[BMKPointAnnotation alloc] init]; annotation.coordinate = coordinate; [self.annotationArr addObject:annotation]; [mapView addAnnotations:self.annotationArr];} else {BMKPointAnnotation * annotation = [BMKPointAnnotation alloc] init]; annotation.coordinate = coordinate; [self.annotationArr addObject:annotation] [mapView addAnnotations:self.annotationArr];} / / pop-up radius selection box [self showLocationSelectRadiusViewWithCoordinate:coordinate];}

Pop up the pop-up box to select the positioning range after selecting a point

# pragma mark pop-up box-(void) showLocationSelectRadiusViewWithCoordinate: (CLLocationCoordinate2D) coordinate {ExtraActLocationSignPopView * popView = [ExtraActLocationSignPopView new]; [popView show]; @ weakify (self); [popView.locatioonSureSignal subscribeNext: ^ (NSString * x) {@ strongify (self); self.viewModel.radius = x; CGFloat radius = [x floatValue]; [self circleWithCenterWithCoordinate2D:coordinate radius:radius];}];}

Draw the range circle after setting the anchor point and radius, and the declared circleArr is used to hold the added area circle at the beginning. When adding a new circle, the old one is removed to ensure that the range drawn each time is up-to-date. Similarly, annotationArr also has this function, because API provides-(void) addOverlays: (NSArray *) overlays. This method: / * add a set of Overlay to the map window. You need to implement the-mapView:viewForOverlay: function of BMKMapViewDelegate to generate the overlay array to be added to the View * @ param overlays corresponding to the annotation * /

# pragma mark add area circular coverage-(void) circleWithCenterWithCoordinate2D: (CLLocationCoordinate2D) coor radius: (CGFloat) radius {NSLog (@ "coordinate lat% f direction long% f", coor.latitude,coor.longitude); / / assign click selection value self.viewModel.lat = [NSString stringWithFormat:@ "% f", coor.latitude]; self.viewModel.lng = [NSString stringWithFormat:@ "% f", coor.longitude]; if (self.circleArr.count > 0) {[_ mapView removeOverlays:self.circleArr] [self.circleArr removeAllObjects]; BMKCircle * circle = [BMKCircle circleWithCenterCoordinate:coor radius:radius]; [self.circleArr addObject:circle]; [_ mapView addOverlays:self.circleArr];} else {BMKCircle * circle = [BMKCircle circleWithCenterCoordinate:coor radius:radius]; [self.circleArr addObject:circle]; [_ mapView addOverlays:self.circleArr] } # pragma mark redraw overlay- (BMKOverlayView *) mapView: (BMKMapView *) mapView viewForOverlay: (id) overlay {if ([overlay isKindOfClass: [BMKCircle class]]) {BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay]; circleView.fillColor = [UIColor colorWithRed:33/255.0 green:196/255.0 blue:206/255.0 alpha:0.3]; circleView.strokeColor = [UIColor colorWithRed:33/255.0 green:196/255.0 blue:206/255.0 alpha:1]; circleView.lineWidth = 1.0 Return circleView;} return nil;}

The above is all the contents of this article "how to achieve Baidu Map location check-in function by iOS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report