呵呵!大家不要只看帖不回帖么,要不然我都沒有積極性了。
第一步:創建一個用來呈現mapview的viewcontroller,不廢話直接貼代碼
BasicMapViewController需要在該類中加入地圖的代理並實現相關事件
// 34.2778000000,108.9530980000 數據來源http://www.gpsspg.com/maps.htm
#define xian CLLocationCoordinate2DMake(34.2239390000,108.9807190000);
#import "BasicMapViewController.h"
#import “KHPaoPaoView.h”//氣泡窗口
#import “KHPointAnnotation.h”//point
@interfaceBasicMapViewController ()
{
BMKMapView *_mapView;
NSBundle *bundle;
NSArray *pointArr;
}
@end
@implementation BasicMapViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
CGFloat navH = 64;
if (__IPHONE_OS_VERSION < 7.0)
{
navH = 44;
}
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, navH+60, __SCREEN_SIZE.width, __SCREEN_SIZE.height-navH-60)];
// _mapView.showMapScaleBar = NO;
// _mapView.overlooking = 0;
bundle = [NSBundle mainBundle];
[self.view addSubview:_mapView];
KHPointAnnotation *point = [[KHPointAnnotationalloc] init];
point.title = @"西安市";
point.subtitle = @"我在這";
point.title1 = @"自定義泡泡測試";
point.title2 = @"自定義測試";
point.coordinate = xian;
BMKPointAnnotation *point1 = [[BMKPointAnnotationalloc] init];
point1.title = @"北京市";
point1.subtitle = @"0000000000000";
point1.coordinate = CLLocationCoordinate2DMake(39.905206, 116.390356);
pointArr = @[point,point1];
}
-(void)viewWillAppear:(BOOL)animated
{
[_mapViewviewWillAppear];
_mapView.delegate = self;// 此處記得不用的時候需要置nil,否則影響內存的釋放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapViewviewWillDisappear];
_mapView.delegate = nil;// 不用時,置nil
}
- (IBAction)switchAction:(UISwitch *)sender
{
if (sender.tag ==1)
{
if (sender.on)
{
[_mapViewsetMapType:BMKMapTypeStandard];
}
else
{
[_mapViewsetMapType:BMKMapTypeSatellite];
}
}
if (sender.tag == 2)
{
//第一參數地圖中心位置,第二參數一中心點顯示周圍的經緯度差值
if (sender.on)
{
[_mapViewsetRegion:BMKCoordinateRegionMake(CLLocationCoordinate2DMake(34.2778000000,108.9530980000), BMKCoordinateSpanMake(0.5,0.5)) animated:YES];
}
else
{
[_mapViewsetRegion:BMKCoordinateRegionMake(CLLocationCoordinate2DMake(39.905206, 116.390356), BMKCoordinateSpanMake(0.5,0.5)) animated:YES];
}
}
if (sender.tag == 3)
{
[_mapView setTrafficEnabled:sender.on];
}
}
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView
{
KHLog(@"地圖加載完成");//回調事件請自行參考使用
[_mapViewsetCompassPosition:CGPointMake(100,100)];//指南針位置可能看不見,這個和官方的交流吧!不清楚是什么原因
[_mapViewaddAnnotations:pointArr];
}
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
KHLog(@"地圖區域改變完成");
}
#pragma mark 設置大頭針
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
if([annotation isKindOfClass:[BMKPointAnnotation class]])
{
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.pinColor = BMKPinAnnotationColorRed;
newAnnotationView.animatesDrop = YES;// 設置該標注點動畫顯示
return newAnnotationView;
}
else
{
// 這里使用自定義大頭針
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.image = [UIImageimageWithContentsOfFile:[bundlepathForResource:@"local@2x"ofType:@"png"]];
newAnnotationView.animatesDrop = YES;// 設置該標注點動畫顯示
KHPaoPaoView *paopao = [[KHPaoPaoView alloc] init];
[paopao setAnnotationWith:((KHPointAnnotation *)annotation)];
newAnnotationView.paopaoView = [[BMKActionPaopaoView alloc] initWithCustomView:paopao];
return newAnnotationView;
}
}
第二步:創建大頭針相關的類、
KHPointAnnotation這個類只要是實現了BMKAnnotation
// 該類為一個抽象類,定義了基於BMKAnnotation的BMKShape類的基本屬性和行為,不能直接使用,必須子類化之后才能使用
@interface BMKShape : NSObject <BMKAnnotation> {
}
#import "BMKPointAnnotation.h"
@interface KHPointAnnotation : BMKShape{
@package
CLLocationCoordinate2D _coordinate;
}
///該點的坐標
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (strong) NSString *title1;
@property (strong) NSString *title2;
@end
//.m中沒什么東西就不貼了
KHPointAnnotation.h這個類繼承自UIView 主要就是大頭針被選中后展示內容
#import <UIKit/UIKit.h>
#import "KHPointAnnotation.h"
@interface KHPaoPaoView : UIView
{
UILabel *label1;
UILabel *label2;
UILabel *label3;
UILabel *label4;
}
-(instancetype)init;
/**
* 設置標注
*
* @param point 標注
*/
-(void)setAnnotationWith:(KHPointAnnotation *)point;
@end
#import "KHPaoPaoView.h"
@implementation KHPaoPaoView
-(instancetype)init
{
self = [super init];
if (self)
{
// 這里需要注意百度地圖中使用的是碎圖片拼起來的,這里的細化工作呵呵你們自己完成吧!
UIImage *image =[UIImageimageWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"medium@2x"ofType:@"png"]];
self.frame = CGRectMake(0, 0, image.size.width+10, image.size.height+10);
UIImageView *IMG = [[UIImageViewalloc] initWithFrame:self.frame];
IMG.image = image;
IMG.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:IMG];
label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, __SCREEN_SIZE.width-100, 15)];
[label1setFont:[UIFontsystemFontOfSize:14.0f]];
[self addSubview:label1];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, __SCREEN_SIZE.width-100, 15)];
[label2setFont:[UIFontsystemFontOfSize:14.0f]];
[self addSubview:label2];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, __SCREEN_SIZE.width-100, 15)];
[label3setFont:[UIFontsystemFontOfSize:14.0f]];
[self addSubview:label3];
label4 = [[UILabel alloc] initWithFrame:CGRectMake(10, 55, __SCREEN_SIZE.width-100, 15)];
[label4setFont:[UIFontsystemFontOfSize:14.0f]];
[self addSubview:label4];
}
returnself;
}
-(void)setAnnotationWith:(KHPointAnnotation *)point
{
label1.text = point.title;
label2.text = point.subtitle;
label3.text = point.title1;
label4.text = point.title2;
}