Integration of Baidu map in iOS development-map tagging & amp; POI retrieval and ios development map tagging

Source: Internet
Author: User

Integration of Baidu map in iOS development-map tagging & POI retrieval, and ios development map tagging

This article consists of two parts:

I. Map tagging

  Step 1: first create the BMKMapView

Step 2: set after the view is fully displayed and implement the proxy method

Step 3: run the program. The Pin effect can be displayed normally.

Ii. POI search

  Step 1: delayed object Loading

Step 2: implement the bmkcancearchdelegate proxy method

Step 3: Implement bmkcancearchdelegate to process callback results

Step 4: run the program, and you will be able to retrieve the notes related to snacks nearby.

I. Map tagging

To label BMKAnnotation, you must implement protocal <BMKMapViewDelegate>

Step 1: first create the BMKMapView

- (BMKMapView *)mapView {    if (!_mapView) {        _mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];        self.view = _mapView;    }    return _mapView;}

 Step 2: set after the view is fully displayed and implement the proxy method

-(Void) viewDidAppear :( BOOL) animated {// Add a PointAnnotation BMKPointAnnotation * annotation = [[BMKPointAnnotation alloc] init]; CLLocationCoordinate2D coor; coor. latitude = 39.915; coor. longpolling = 116.404; annotation. coordinate = coor; annotation. title = @ "Beijing"; annotation. subtitle = @ "I cannot breathe for you ~ "; [_ MapView addAnnotation: annotation];} // custom add pin method-(void) addAnnoWithPT :( CLLocationCoordinate2D) coor andTitle :( NSString *) title andAddress :( NSString *) address {// Add a PointAnnotation BMKPointAnnotation * annotation = [[BMKPointAnnotation alloc] init]; annotation. coordinate = coor; annotation. title = title; annotation. subtitle = address; [_ mapView addAnnotation: annotation] ;}# pragma mark # pragma mar The k-marker proxy method is used to add a pin (BMKAnnotationView *) mapView :( BMKMapView *) mapView viewForAnnotation :( id <BMKAnnotation>) annotation {static NSString * identifier = @ "myAnnotation "; if ([annotation isKindOfClass: [BMKPointAnnotation class]) {BMKPinAnnotationView * newAnnotationView = (BMKPinAnnotationView *) [mapView identifier: identifier]; if (! NewAnnotationView) {newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: identifier];} newAnnotationView. annotation = annotation; newAnnotationView. pinColor = BMKPinAnnotationColorPurple; newAnnotationView. animatesDrop = YES; // set the animation display for this annotation point. // Add a button to listen to the Click Event UIButton * btn = [UIButton buttonWithType: UIButtonTypeContactAdd]; newAnnotationView. rightCalloutAccessoryView = btn; [btn addTarget: self action: @ selector (click) forControlEvents: UIControlEventTouchUpInside]; return newAnnotationView;} return nil ;}

 

 Step 3: run the program. The Pin effect can be displayed normally.

 

Ii. POI search

Step 1: delayed object Loading

- (BMKPoiSearch *)poiSearch {    if (!_poiSearch) {        _poiSearch = [[BMKPoiSearch alloc] init];        _poiSearch.delegate = self;    }    return _poiSearch;}

Step 2: implement the bmkcancearchdelegate proxy method

// This method is called when a long-pressed map is used-(void) mapview :( BMKMapView *) mapView onLongClick :( CLLocationCoordinate2D) coordinate {// initiate retrieval BMKNearbySearchOption * option = [[BMKNearbySearchOption alloc] init]; option. pageIndex = 0; option. pageCapacity = 20; option. location = coor; option. keyword = @ "snacks"; BOOL flag = [self. poiSearch incluearchnearby: option]; if (flag) {NSLog (@ "nearby retrieval sent successfully");} else {NSLog (@ "nearby retrieval failed ");} // set the initialization region CLLocationCoordinate2D center = option. location; BMKCoordinateSpan span; span. latitudeDelta = 0.016263; span. longitudeDelta = 0.012334; BMKCoordinateRegion region; region. center = center; region. span = span; [self. mapView setRegion: region animated: YES];}

Step 3: Implement bmkcancearchdelegate to process callback results

-(Void) onGetPoiResult :( bmkincluearch *) searcher result :( BMKPoiResult *) poiResultList errorCode :( BMKSearchErrorCode) error {if (error = BMK_SEARCH_NO_ERROR) {// processing normal results here // NSLog (@ "success: % @", poiResultList. poiInfoList); [poiResultList. poiInfoList enumerateObjectsUsingBlock: ^ (BMKPoiInfo * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {// NSLog (@ "% @ ---- % @", obj. name, obj. address); // because 10 entries are specified for each page during retrieval, 10 related information is retrieved here [self addAnnoWithPT: obj.pt andTitle: obj. name andAddress: obj. address];} else if (error = BMK_SEARCH_AMBIGUOUS_KEYWORD) {// when no result is found in the Set city but in other cities, it is recommended to retrieve the city list for callback // result. cityList; NSLog (@ "ambiguous start point");} else {NSLog (@ "sorry, no result found, % zd", error );}}

 Step 4: run the program, and you will be able to retrieve the notes related to snacks nearby.

Note: header files to be introduced

# Import <baidumapi_map/BMKMapComponent. h> // introduce all header files of the map function # import <baidumapi_search/BMKSearchComponent. h> // introduce all header files of the search function # import <BaiduMapAPI_Location/BMKLocationComponent. h> // introduce all header files of the positioning function

 

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.