美文网首页
自定义大头针

自定义大头针

作者: nothing_c | 来源:发表于2016-10-31 19:19 被阅读96次

定位和地图可以分开使用

配置时需要条件编译

//地图的头文件

#import

//定位的头文件

#import

//自定义注释类(继承于MKAnnotation,去除readyonly后的coordinate,title,subtitle)

#import "CustomAnnotation.h"

//创建地图

MKMapView *map = [[MKMapView alloc] initWithFrame:self.view.bounds];

//显示样式(系统地图高德地图)

map.mapType = MKMapTypeStandard;

//经纬度

//CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(26.548277, 148.125381);

//设置地图中心点

//map.centerCoordinate = coor;

//初显示范围设置要显示的区域

//MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coor, 100000, 100000);

//[map setRegion:region animated:YES];

//map.scrollEnabled = NO;滑动

//map.zoomEnabled = NO;缩放

//左上角比例尺,多放大几次屏幕就显示

map.showsScale = YES;

//旋转时右上角的指南针(罗盘)默认YES显示

//map.showsCompass = NO;

//设置代理MKMapViewDelegate

map.delegate = self;

//第一次加载模拟器不显示位置,更新位置就显示,如果是真机,第一次就可以显示

//运行后自己再更新位置

map.showsUserLocation = YES;

[self.view addSubview:map];

//创建定位对象

manager = [[CLLocationManager alloc] init];

//授权定位

[manager requestAlwaysAuthorization];

//要去info.plist文件配置NSLocationAlwaysUsageDescription

//创建自定义注释对象

CustomAnnotation *ann = [[CustomAnnotation alloc] init];

ann.coordinate=CLLocationCoordinate2DMake(41.5427, 116.2617);

ann.title=@"北京";

ann.subtitle=@"五环";

//标注红色圆点

[map addAnnotation:ann];

//绘制路径

//MKCircle圆圈MKPolyline折线MKPolygon闭合

//闭合区域

//坐标点数组里面有四个点

CLLocationCoordinate2Dpoints [4];

points[0] = CLLocationCoordinate2DMake(39, 100);

points[1] = CLLocationCoordinate2DMake(39, 110);

points[2] = CLLocationCoordinate2DMake(29, 110);

points[3] = CLLocationCoordinate2DMake(29, 100);

//MKPolygon *gon = [MKPolygon polygonWithCoordinates:points count:4];

//[map addOverlay:gon];

//MKPolyline *line = [MKPolyline polylineWithCoordinates:points count:4];

//[map addOverlay:line];

MKCircle *circle = [MKCircle circleWithCenterCoordinate:points[3] radius:1000000.00];

[map addOverlay:circle];

}

#pragma mark -- MKMapViewDelegate

//对绘制区域的图层进行渲染图层才能显示

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay {

if([overlay isMemberOfClass:[MKPolygon class]]) {

//渲染对象

MKPolygonRenderer * renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];

//线宽

renderer.lineWidth= 5;

//线的颜色

renderer.strokeColor= [UIColor redColor];

//填充色

renderer.fillColor= [UIColor cyanColor];

return renderer;

} elseif ([overlay isKindOfClass:[MKPolyline class]]){

MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

render.lineWidth = 5;

render.strokeColor = [UIColor orangeColor];

render.fillColor = [UIColor redColor];

return render;

} else {

MKCircleRenderer *render = [[MKCircleRenderer alloc] initWithCircle:overlay];

render.lineWidth = 1;

render.strokeColor = [UIColor purpleColor];

render.fillColor = [UIColor yellowColor];

return render;

}

}

//更新用户位置的时候调用

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocationNS_AVAILABLE(10_9, 4_0) {

//初显示范围设置要显示的区域

MKCoordinateRegion regin =MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 100000, 100000);

//要设置才能触发

[mapView setRegion:regin animated:YES];

//更新位置点击图标显示

mapView.userLocation.title = @"福州";

mapView.userLocation.subtitle = @"hot";

}

//自定义弹出的气泡

- (nullableMKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {

//如果是自定义的大头针就处理泡泡显示的样式

if([annotation isKindOfClass:[CustomAnnotation class]]) {

//使用MKAnnotationView不能实现animatesDrop所有使用其子类MKPinAnnotationView

MKPinAnnotationView *ann = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:NSStringFromClass([MKPinAnnotationView class])];

if(!ann) {

ann = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:NSStringFromClass([MKPinAnnotationView class])];

//美化]

//大头针的背景色

ann.pinTintColor= [UIColor purpleColor];

//掉落的动画(从上往下的效果)

ann.animatesDrop =YES;

//可以响应(弹出气泡)

ann.canShowCallout =YES;

//弹出气泡的左侧的view

ann.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoLight];

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];

image.frame = CGRectMake(0, 0, 32, 32);

ann.rightCalloutAccessoryView = image;

}

return ann;

} else {//否则不处理返回系统自带样式

return nil;

}

}

//点击气泡上的按钮触发的方法

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

NSLog(@"tap ===");

}


//自定义注释类要导入这个头文件

#import

//MKAnnotation

@interface CustomAnnotation :NSObject

//去掉readyonly

//必选

@property (nonatomic,assign) CLLocationCoordinate2D coordinate;

//可选

@property (nonatomic,copy) NSString *title;

@property (nonatomic,copy) NSString *subtitle;

@implementation CustomAnnotation

//MRC  set方法

//- (void)setTitle:(NSString *)title {

//    //判断新旧值是否相等严谨

//    if (_title != title) {

//        //释放老对象的所有权

//        [_title release];

//        //为防止新对象释放掉要引用一次

//        [title retain];

//        //重新赋值(目的)

//        _title = title;

//    }

//}

@end

调试定位的经纬度 选择地区

相关文章

  • 大头针Annotation

    自定义大头针 使用大头针 点击添加大头针 自定义大头针颜色和动画 系统大头针的处理 自定义大头针图片 修改大头针模...

  • iOS 地图开发(MapKit)(二)

    相关类的介绍:MKAnnotation(大头针协议)大头针数据类(自定义的大头针需要遵守大头针协议)MKPoint...

  • iOS 系统自带API地图开发相关(二)

    地图-自定义大头针 关于大头针视图分两类: MKPinAnnotationView : apple自带的大头针,可...

  • 自定义大头针

    自定义大头针其实没什么东西,讲讲简单的自定义大头针吧! 1.需要定义大头针模型(里面至少有三个属性)#import...

  • 高德地图---iOS笔记摘录

    概念 使用 地图显示 交互 显示 计算 其他 1.1 大头针 自定义大头针: MAPointAnnotation ...

  • 百度地图大头针点击之后不调用mapView: viewForAn

    1.如何你自定义的大头针,看看大头针的UIImageView的userInteractionEnabled属性有没...

  • 地图高级 - 自定义大头针

    地图高级 - 自定义大头针 1. 理论支撑 2. 模拟实现系统大头针 实现当添加大头针数据模型时,地图回调的代理方...

  • iOS 自定义大头针

    一、自定义大头针 需求 需求:鼠标拖拽是在地图的哪个位置, 就在对应的位置加一个大头针,并反地理编码大头针所在城市...

  • 高德地图calloutView点击无响应,解决办法

    因为自定义的气泡是添加到大头针上的,而大头针的size只有下面很小一部分,所以calloutView是在大头针的外...

  • 自定义大头针Annotation

    (1)自定义大头针Annotation的样式,也就是定义view,主要的方法是如下,传递一个大头针annotati...

网友评论

      本文标题:自定义大头针

      本文链接:https://www.haomeiwen.com/subject/xxmputtx.html