Imitation of the United States can be pulled out of the map map – Baidu map

Preface

Here you apologize before the upload demo has a bug, thank you very much @MinJing_Lin friends help me out, but I because working reason has not been altered, today finally to fix it, and update the demo, first talk about bug, can also see comments, is to confirm the return when choosing and a data address in the list displayed on the interface and you don't choose the data, bug reason: because I made a deal, when choosing a data I also updated the map information, update the map information list below data are updated, the when the data has changed, resulting in the generation of bug, according to the business needs of the following solutions: 1 when the selection list data does not update the map directly back Back to the interface (do not need to determine the 2 button) when the select list data updating map information, confirm the return time, return the data list first data information, updated to select map data displayed in the first, even if the data you need. [email protected]_Lin commented that the method is also possible to change the model into a local variable. I am here demo is repaired in second ways, according to the needs of their own modifications.

Sometimes the need for access to precise position information of user in the process of development projects, but we all know, direct access to the address may have errors, this time you need to manually drag the map, and then obtain a precise location information, like the U.S. harvest address as…

First look at a renderings

Imitation of the United States can be pulled out of the map map - Baidu map
baiduDragMap.gif

Interface to do a little rough, but the basic data can be obtained!
I began to teach you how to achieve:

First step

(1) go to Baidu Developer Center to add your application to appkey.
(2) in accordance with the development of Baidu maps integrated SDK into their own projects, here I use the pod import, more convenient, but also facilitate the later update.
(3) in your AppDelegate.m file to add the initialization of the BMKMapManager, and fill in your application authorization Key, the example is as follows:

_mapManager = [[BMKMapManager alloc]init]; / / if you want to focus on the network and the authentication event, please set the generalDelegate parameters BOOL RET = [_mapManager start:@ "here is your application for appkey generalDelegate:self]; if (! RET) {NSLog (@ manager start failed!");}

Second step

Initializes BMKMapView map control and BMKGeoCodeSearch geo search service in the ZGYDrapMapVC.m file

_geocodesearch [[BMKGeoCodeSearch = alloc]init]; self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake / / initialize mapView (0, 64, [UIScreen mainScreen].bounds.size.width, 300); _mapView.userTrackingMode = BMKUserTrackingModeFollow; / / set the orientation state here need to be set to follow, because to be able to display the current location on the map of _mapView.showsUserLocation = YES; / / display orientation layer _mapView.zoomLevel = 19.5; / / scale level

Since 2.0.0, BMKMapView, viewWillAppear, viewWillDisappear new method to control the life cycle of BMKMapView, and in only one BMKMapView accept callback message, so the method calls the corresponding BMKMapView in the viewWillAppear and viewWillDisappear methods in using BMKMapView viewController, and delegate, the code is as follows:

The life cycle management of Baidu maps / * * * / - (void) viewWillAppear: (BOOL) animated{[_mapView viewWillAppear]; _mapView.delegate = self; _geocodesearch.delegate = self;} - (void) viewWillDisappear: (BOOL) animated{[_mapView viewWillDisappear]; _mapView.delegate = nil; / / to be set to nil when not in use, or affect the memory of the release of _geocodesearch.delegate = nil; / / are set to nil when not in use, or affect the release of memory}

Third step

Determine whether the location is available, if it can be used to initialize the location of the service, and open the positioning service to locate the service, if it is not available to prompt the user to open the positioning

The availability and location judgment / * * * / initialization location information (void) - initLocation{(if [CLLocationManager locationServicesEnabled] & & ([CLLocationManager authorizationStatus] kCLAuthorizationStatusAuthorized [CLLocationManager authorizationStatus] || = = = = kCLAuthorizationStatusNotDetermined)) {/ / positioning function is available, [self setLocation] [self startLocation] began positioning;}else; if ([CLLocationManager = = kCLAuthorizationStatusDenied (authorizationStatus]) || ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusRestricted) [[ZGYAlertView alloc]showAlertViewMessage:nil) {Title:@ "please confirm open location services, and allows the system to obtain a business management The "cancleItem:@" to set the "andOtherItem:nil viewController:self onBlock:^ (AlertViewBtnIndex index) {if (index = = 0) {if ([CLLocationManager! LocationServicesEnabled]) {[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@" prefs:root=LOCATION_SERVICES "}else{[[UIApplication sharedApplication]"; openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];}}}}}]; #pragma mark (void) - set position parameter setLocation {/ / initialize BMKLocationService _locService [[BMKLocationService = alloc]init]; _locService.delegate = self; / / set positioning precision _locService.desiredAccuracy CLLocationDistance = kCLLocationAccuracyBest; distance = 10; _locService.distanceFilter = distance;} - (void) startLocation{[_locService startUserLocationService];}

Fourth step

After the positioning will be in the back of the method to get a latitude and longitude, access to latitude and longitude after the launch of an anti geocoding:

* / * * user location update, will call this function *@param userLocation new user location * / - (void) didUpdateBMKUserLocation: (BMKUserLocation * userLocation) {[self.view addSubview:self.mapView]; PoiResultListViewController *poiResultVC = (PoiResultListViewController * self.childViewControllers[0]); self.poiResultVC = poiResultVC; [self.contentView addSubview:poiResultVC.view]; [UIView animateWithDuration:0.3 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{_poiView.center = CGPointMake ([UIScreen mainScreen].bounds.size.width/2, 150 - 35)}; completion:^ (BOOL finished) {[UIView animateWithDuration:0.3 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{ _poiView.center = CGPointMake ([UIScreen mainScreen].bounds.size.width/2, 150 - 20)}; completion:nil];}]; CLLocationCoordinate2D = Pt (CLLocationCoordinate2D) {0, 0}; Pt = (CLLocationCoordinate2D) {userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude}; BMKReverseGeoCodeOption *reverseGeocodeSearchOption [[BMKReverseGeoCodeOption = alloc]init]; reverseGeocodeSearchOption.reverseGeoPoint = Pt; BOOL = flag [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption]; if (flag) {NSLog (@ "Geo search");} else successfully sent {NSLog (@ "anti geo retrieval successfully sent");} [_mapView updateLocationData:userLocation]; [_mapView setCenterCoordinate: UserLocation.location.coordinate animated:YES]; [_locService stopUserLocationService];} / * * @param error positioning positioning failure after failure of error information according to the cause of the error information to judge the failure of the * - (void) didFailToLocateUserWithError: (NSError * error{) / / unable to obtain location information / where alertView is its own package alertView / [[ZGYAlertView alloc]showAlertViewMessage:[NSString stringWithFormat:@ easy to use "error code:%ld, [error code]] Title:@" cannot get the position information of "cancleItem:@" Cancel "andOtherItem:nil viewController:self onBlock:^ (AlertViewBtnIndex index) {[_locService}}]; stopUserLocationService];

The anti geocoding search results in the result of anti geocoding in its proxy method

* / * * return search results *@param searcher encoding anti geographical search object *@param result search results *@param error error, @see BMKSearchErrorCode * / - (void) onGetReverseGeoCodeResult: (BMKGeoCodeSearch * searcher) result: (BMKReverseGeoCodeResult *) result errorCode: (BMKSearchErrorCode) error{if (error = = BMK_SEARCH_NO_ERROR) {NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; for (int I = 0 i; < result.poiList.count; i++) {BMKPoiInfo* POI = [result.poiList objectAtIndex:i]; self.model [[PoiModel = alloc]init]; self.model.name = poi.name; self.model.city = poi.city; self.model.address = poi.address; self.model.lat = poi.pt.latitude [array addObject:self.model]; self.model.lon = poi.pt.longitude; self.poiResultVC.resultListArray = [NSArray;}} (error arrayWithArray:array]; else if = = BMK_SEARCH_AMBIGUOUS_ROURE_ADDR) {NSLog (@ "the starting point is ambiguous");} else {/ / all judgment... }}

This method of result which contains the anti geocoding name,
City City, address, latitude and longitude, telephone number phone, mailbox postcode

I need to extract the data into the model into the array inside, easy to display on the tableView, display information as shown above.

Fifth step

After the completion of the initialization of the map is not disabled, you can drag the map area will be changed after the proxy method as follows:

* / * * map area changed after the completion of this interface will be called *@param MapView View *@param animated animation map - (void) mapView: * (BMKMapView *) mapView regionDidChangeAnimated: (BOOL animated) {NSLog (@%lf,%lf, mapView.centerCoordinate.latitude, mapView.centerCoordinate.longitude); [UIView animateWithDuration:0.3 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{_poiView.center = CGPointMake ([UIScreen mainScreen].bounds.size.width/2, 150 - 35);} completion:^ (BOOL finished) [UIView animateWithDuration:0.3 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{{_poiView.center = CGPointMake ([UIScreen mainScreen].bounds.size.width/2, 150 - 20);} completion }]; CLLocationCoordinate2D: nil]; Pt = (CLLocationCoordinate2D) {0, 0}; Pt = (CLLocationCoordinate2D) {mapView.centerCoordinate.latitude, mapView.centerCoordinate.longitude}; BMKReverseGeoCodeOption *reverseGeocodeSearchOption = BMKReverseGeoCodeOption + alloc]init]; reverseGeocodeSearchOption.reverseGeoPoint = Pt; BOOL = flag [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption]; if (flag) {NSLog (@ "anti geo retrieval successfully sent");} else {NSLog (@ "anti geo retrievalare successfully sent");}}
Methods I made UIview animation, looks a little better here in the middle of the map location picture is not in the map elements, just a imageView at the top of the screen in the middle position, when the map changes make the point of need also to display it on the line in the middle of the map, the use of methods is
Coordinate map center point set the center coordinates map / * * * *@param coordinate to set the *@param animated with the latitude and longitude said whether the animation * / - (void) setCenterCoordinate: (CLLocationCoordinate2D) coordinate animated: (BOOL) animated;

As long as this point to understand, and know that Baidu maps API provides these related methods this problem is very easy.

I also used a custom header

- (BMKAnnotationView *) mapView: (* BMKMapView) mapView viewForAnnotation: (id< BMKAnnotation> annotation{) static NSString @ *pinID = "pinID"; / / remove pin data view BMKAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:pinID] / / if removed from the cache pool; for nil, then manually create a pin view if (customView = = Nil) {customView = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];} / / customView.image = [UIImage 1 pin set picture imageNamed:@ "point"]; / / the 2 bomb box set customView.canShowCallout = YES; return customView;}

We have to remember that the pin is the same as the tableView cell has a reuse mechanism, the interface is not good to deal with a lot of pins can not disappear, please refer to the official documents related issues, more detailed.

The general process is these, but also need to do more than their own, the problem is not so complicated!

If you have any questions you would like to raise a lot of discussion, such as the wrong place in the article also want to point out that I will correct in time, thank you!

This article has been uploaded to the GitHub Demo, convenient for everyone to download and use, the code level is not high, see forgive, if there is any advice, I will correct! Thank you for your support! Thank you very much.