IOS-BMK annotation and ios Baidu map Annotation

Source: Internet
Author: User

IOS-BMK annotation and ios Baidu map Annotation

In iOS development, map is an important module. Our commonly used maps include AMAP, Baidu, and Google Maps. For China, Apple no longer uses Google Maps. The official uses AMAP. The following describes some small knowledge points in Baidu map development.

For how to configure the development environment of Baidu map, I will not talk about it here, the specific can refer to: http://developer.baidu.com/map/index.php? Title = iossdk/guide/buildproject

Baidu map iOS API: http://developer.baidu.com/map/index.php? Title = iossdk/sdkiosdev-download

For basic use of Baidu map, refer to Baidu map development document. Here we will summarize some important knowledge points and extensions in the development document. (MAP version ios sdk 2.9.0)

First, describe the problems that Baidu may encounter during map development:

How to add annotation (system annotation and custom annotation)

1 // Add Tag 2-(void) viewDidAppear :( BOOL) animated 3 {4/* 5 for (int I = 0; I <3; I ++) {6 BMKPointAnnotation * annotation = [[BMKPointAnnotation alloc] init]; 7 CLLocationCoordinate2D coor; 8 coor. latitude = 39.915 + I * 2; 9 coor. longpolling = 116.404 + I * 2; 10 annotation. coordinate = coor; 11 annotation. title = @ "Beijing"; 12 [myMapView addAnnotation: annotation]; 13} 14 */15 BMKPointAnnotation * annotation = [[BMKPointAnnotation alloc] init]; 16 CLLocationCoordinate2D coor; 17 coor. latitude = 39.915; 18 coor. longpolling = 116.404; 19 annotation. coordinate = coor; 20 annotation. title = @ "Beijing"; 21 annotation. subtitle = @ "1"; 22 // [myMapView addAnnotation: annotation]; 23 24 BMKPointAnnotation * annotation1 = [[BMKPointAnnotation alloc] init]; 25 bytes coor1; 26 coor1.latitude = 38.915; 27 coor1.longpolling = 113.404 + 2; 28 annotation1.coordinate = coor1; 29 annotation1.title = @ "Beijing"; 30 annotation1.subtitle = @ "2"; 31 // [myMapView addAnnotation: annotation1]; 32 33 BMKPointAnnotation * annotation2 = [[BMKPointAnnotation alloc] init]; 34 bytes coor2; 35 coor2.latitude = 38.915; 36 coor2.long1_= 119.404 + 2; 37 annotation2.coordinate = coor2; 38 annotation2.title = @ "here is also Beijing"; 39 annotation2.subtitle = @ "3"; 40 // [myMapView addAnnotation: annotation2]; 41 42 NSArray * arr = [NSArray arrayWithObjects: annotation, annotation1, annotation2, nil]; 43 [myMapView addAnnotations: arr]; 44} 45 46-(optional *) mapView :( BMKMapView *) mapView viewForAnnotation :( id <BMKAnnotation>) annotation47 {48 if ([annotation isKindOfClass: [BMKPointAnnotation class]) 49 {50 BMKPinAnnotationView * newAnnotationView = (BMKPinAnnotationView *) [mapView comment: @ "AnnotationView"]; 51 newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @ "AnnotationView"]; 52 newAnnotationView. pinColor = BMKPinAnnotationColorPurple; 53 newAnnotationView. animatesDrop = YES; // set the annotation animation to 54 return newAnnotationView; 55} 56 return nil; 57}System tag

 

 

 

How to customize a pin custom pin is to change the style of the pin. If you want to use your own picture to replace the style shown above, the Code implementation is basically the same as the above Code. Just add the following code to the Code implemented by the proxy above. 1 newAnnotationView. image = [UIImage imageNamed: @ "2, one title and one subtitle. Only the bubble style, such as the border size, Font, and other basic information, is modified here, and the overall layout is not changed. 1-(BMKAnnotationView *) mapView :( BMKMapView *) mapView viewForAnnotation :( id <BMKAnnotation>) annotation 2 {3 if ([annotation isKindOfClass: [BMKPointAnnotation class]) 4 {5 response * newAnnotationView = (comment *) [mapView comment: @ "AnnotationView"]; 6 newAnnotationView = [[comment alloc] initWithAnnotation: annotation reuseIdentifier: @ "AnnotationView"]; 7. newAnnotationView. pinColor = BMKPinAnnotationColorPurple; 8 newAnnotationView. animatesDrop = YES; // set the animation of this annotation point to display 9 10 UIView * view = [[UIView alloc] init]; 11 view. frame = CGRectMake (0, 0,100, 60); 12 view. backgroundColor = [UIColor greenColor]; 13 [view. layer setMasksToBounds: YES]; 14 [view. layer setCornerRadius: 3]; 15 view. alpha = 0.9; 16 17 UILabel * label1 = [[UILabel alloc] init]; 18 label1.frame = CGRectMake (0, 0,100, 30); 19 label1.text = annotation. title; 20 label1.numberOfLines = 0; 21 label1.font = [UIFont systemFontOfSize: 12]; 22 label1.textColor = [UIColor blackColor]; 23 [view addSubview: label1]; 24 25 UILabel * label2 = [[UILabel alloc] init]; 26 label2.frame = CGRectMake (0, 30,100, 30); 27 label2.text = annotation. subtitle; 28 label2.numberOfLines = 0; 29 label2.font = [UIFont systemFontOfSize: 10]; 30 label2.textColor = [UIColor lightGrayColor]; 31 [view addSubview: label2]; 32 33 BMKActionPaopaoView * pView = [[BMKActionPaopaoView alloc] initWithCustomView: view]; 34 pView. frame = CGRectMake (0, 0,100, 60); 35 (BMKPinAnnotationView *) newAnnotationView ). paopaoView = pView; 36 return newAnnotationView; 37} 38 return nil; 39}Simple definition of bubble styles

What should we do if we want to modify the bubble layout?

Because the system defaults to coordinates, titles, and subtitles. What do we need to do if we want to layout the bubbles in our own way?

First, the BMKPointAnnotation method provided by the system is not enough for our use. We need to inherit this class, such as the new class as myPoint and add some new attributes. For this class, we need to add three attributes.

NSString * imgName; NSString * placeName; NSString * idNum;

Next, we need to customize the bubble. The bubble itself is a UIView. We can create a subclass myPaopao In the inherited UIView. We need to add three controls in myPaopao, display the three attributes defined above.

1 # import <UIKit/UIKit. h> 2 3 @ interface myPaopao: UIView4 @ property (nonatomic, retain) UIImageView * imgView; 5 @ property (nonatomic, retain) UILabel * placeName; 6 @ property (nonatomic, retain) UILabel * idNum; 7 8 @ endMyPaopao. h 1 # import "myPaopao. h "2 3 @ implementation myPaopao 4 5-(instancetype) initWithFrame :( CGRect) frame 6 {7 self = [super initWithFrame: frame]; 8 if (self) 9 {10 self. frame = CGRectMake (0, 0,150, 60); 11 self. backgroundColor = [UIColor whiteColor]; 12 13 _ imgView = [[UIImageView alloc] init]; 14 _ imgView. frame = CGRectMake (0, 0, 60, 60); 15 [self addSubview: _ imgView]; 16 17 _ placeName = [[UILabel alloc] init]; 18 _ placeName. frame = CGRectMake (60, 0, 90, 30); 19 _ placeName. font = [UIFont systemFontOfSize: 12]; 20 [self addSubview: _ placeName]; 21 22 _ idNum = [[UILabel alloc] init]; 23 _ idNum. frame = CGRectMake (60, 30, 90, 30); 24 [self addSubview: _ idNum]; 25 26} 27 return self; 28} 29 @ endMyPaopao. m 1 // Add the flag 2-(void) viewDidAppear :( BOOL) animated 3 {4 myPoint * annotation = [[myPoint alloc] init]; 5 CLLocationCoordinate2D coor; 6 coor. latitude = 39.915; 7 coor. longpolling = 116.404; 8 annotation. coordinate = coor; 9 annotation. imgName = @ "1.jpg"; 10 annotation. placeName = @ "Beijing"; 11 annotation. idNum = @ "1"; 12 [myMapView addAnnotation: annotation]; 13 14 myPoint * annotation1 = [[myPoint alloc] init]; 15 CLLocationCoordinate2D coor1; 16 coor1.latitude = 38.915; 17 coor1.longpolling = 113.404 + 2; 18 annotation1.coordinate = coor1; 19 annotation1.imgName = @ "2.jpg"; 20 annotation1.placeName = @" Beijing "; 21 annotation1.idNum = @" 2 "; 22 [myMapView addAnnotation: annotation1]; 23 24} 25-(optional *) mapView :( BMKMapView *) mapView viewForAnnotation :( id <BMKAnnotation>) annotation26 {27 if ([annotation isKindOfClass: [myPoint class]) 28 {29 myPoint * myAnnotation = (myPoint *) annotation; 30 31 BMKPinAnnotationView * newAnnotationView = (BMKPinAnnotationView *) [mapView comment: @ "AnnotationView"]; 32 newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @ "AnnotationView"]; 33 newAnnotationView. pinColor = BMKPinAnnotationColorPurple; 34 newAnnotationView. animatesDrop = YES; // set the annotation animation to 35 myPaopao * paopapo = [[myPaopao alloc] init]; 36 37 paopapo. imgView. image = [UIImage imageNamed: myAnnotation. imgName]; 38 paopapo. placeName. text = myAnnotation. placeName; 39 paopapo. idNum. text = myAnnotation. idNum; 40 BMKActionPaopaoView * pView = [[BMKActionPaopaoView alloc] initWithCustomView: paopapo]; 41 (BMKPinAnnotationView *) newAnnotationView ). paopaoView = pView; 42 return newAnnotationView; 43} 44 return nil; 45}Implementation Code

If necessary, you can add a button to jump to the details page and add a button in the same way as the preceding method.

Point Aggregation

The point aggregation function is a new feature in v2.9.0. If there are a large number of points in a region, it will produce coverage. The point aggregation function can aggregate many points to one point, you can zoom in to display more points or aggregate points. When downloading the SDK, we will have a Demo. From the Demo, we can find the corresponding implementation code. The core code is provided in the Development document:

 

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.