iOS大头针(自定义大头针,定位,画线)
2014-10-10 22:33
253 查看
//************************************自定义大头针 .h文件
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnn :
NSObject <MKAnnotation>
@property (nonatomic,assign)CLLocationCoordinate2D coordinate;
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *address;
-(id)initWithTitle:(NSString *)title withSubTitle:(NSString*)subTitle withCoordinate:(CLLocationCoordinate2D)coordinate;
@end
//************************************自定义大头针 .m文件
#import "MyAnn.h"
@implementation MyAnn
-(id)initWithTitle:(NSString *)title withSubTitle:(NSString *)subTitle withCoordinate:(CLLocationCoordinate2D)coordinate
{
self=[super
init];
if (self)
{
self.name=title;
self.address=subTitle;
self.coordinate=coordinate;
}
return
self;
}
-(NSString*)title
{
return
self.name;
}
-(NSString*)subTitle
{
return
self.address;
}
@end
//************************************MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController :
UIViewController<MKMapViewDelegate>
{
MKMapView *map_view;
}
@end
//************************************MapViewController.m
#import "MapViewController.h"
#import "MyAnn.h"
@implementation MapViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
//创建对象
map_view=[[MKMapView
alloc] initWithFrame:self.view.frame];
map_view.mapType=MKMapTypeStandard;
map_view.showsUserLocation=YES;
map_view.delegate=self;
map_view.userLocation.title=@"我的位置";
map_view.userLocation.subtitle=@"娃哈哈";
[self.view
addSubview:map_view];
//定位按钮
UIButton *button=[UIButton
buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0,
20, 60,
60);
button.backgroundColor=[UIColor
redColor];
[button setTitle:@"定位"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
//在地图上画图
//画多变形
曲线
圆
CLLocationCoordinate2D points[4];
points[0] =
CLLocationCoordinate2DMake(41.000512, -109.050116);
points[1] =
CLLocationCoordinate2DMake(41.002371, -102.052066);
points[2] =
CLLocationCoordinate2DMake(36.993076, -102.041981);
points[3] =
CLLocationCoordinate2DMake(36.99892, -109.045267);
// MKPolygon *gon=[MKPolygon polygonWithCoordinates:points count:4];
// [map_view addOverlay:gon];
MKCircle *circle=[MKCircle
circleWithCenterCoordinate:points[0]
radius:5000];
[map_view
addOverlay:circle];
MKPolyline *line=[MKPolyline
polylineWithCoordinates:points count:4];
[map_view
addOverlay:line];
}
-(void)btnClick:(UIButton *)button;
{
map_view.centerCoordinate=map_view.userLocation.coordinate;
//创建一个以用户为中心的区域
附近多少米
MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(map_view.userLocation.coordinate,
5000,
5000);
[map_view
setRegion:region animated:YES];
MyAnn *myAnn=[[MyAnn
alloc] initWithTitle:@"大头针"
withSubTitle:@"小毛驴儿" withCoordinate:map_view.userLocation.coordinate];
[map_view
addAnnotation:myAnn];
}
//将用户位置作为屏幕中心
此方法是代理方法 自动调用
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation
*)userLocation
{
map_view.centerCoordinate=userLocation.coordinate;
}
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *identifer=@"AnnotationView";
MKPinAnnotationView *annView=(MKPinAnnotationView*)[map_view
dequeueReusableAnnotationViewWithIdentifier:identifer];
if (annView==nil)
{
annView=[[MKPinAnnotationView
alloc] initWithAnnotation:annotation
reuseIdentifier:identifer];
}
//判断是否是用户标记点
if (mapView.userLocation==annotation)
{
return nil;
}
//设置打头针的颜色
annView.pinColor=MKPinAnnotationColorPurple;
//设置能够显示弹出气泡
annView.canShowCallout=YES;
//添加右边的试图
UIButton *button=[UIButton
buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView=button;
//视图
UIImageView *imageView=[[UIImageView
alloc] initWithFrame:CGRectMake(0,
0, 32,
32)];
imageView.image=[UIImage
imageNamed:@"b.jpg"];
annView.leftCalloutAccessoryView=imageView;
return annView;
}
//点击详情
调用的方法
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
//调用浏览器打开浏览器
[[UIApplication
sharedApplication] openURL:[NSURL
URLWithString:@"http://www.baidu.com"]];
}
//画 renderer
描绘 装饰品
-(MKOverlayRenderer*)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
//判断覆盖的类型
if ([overlay isKindOfClass:[MKPolygon
class]])
{
//创建多边形渲染
MKPolygonRenderer *aRenderer=[[MKPolygonRenderer
alloc]
initWithPolygon:(MKPolygon*)overlay];
//多边形的填充颜色
aRenderer.fillColor=[UIColor
redColor];
//边框的颜色
aRenderer.strokeColor=[UIColor
blueColor];
//线的宽度
aRenderer.lineWidth=4;
return aRenderer;
}
else if ([overlay
isKindOfClass:[MKPolyline
class]])
{
MKPolylineRenderer *lineRenderer=[[MKPolylineRenderer
alloc]
initWithPolyline:(MKPolyline*)overlay];
lineRenderer.strokeColor=[UIColor
redColor];
lineRenderer.lineWidth=3;
return lineRenderer;
}
else if([overlay
isKindOfClass:[MKCircle
class]])
{
MKCircleRenderer *circleRenderer=[[MKCircleRenderer
alloc] initWithOverlay:(MKCircle*)overlay];
circleRenderer.strokeColor=[UIColor
redColor];
circleRenderer.fillColor=[UIColor
purpleColor];
circleRenderer.lineWidth=3;
return circleRenderer;
}
return
nil;
}
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnn :
NSObject <MKAnnotation>
@property (nonatomic,assign)CLLocationCoordinate2D coordinate;
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *address;
-(id)initWithTitle:(NSString *)title withSubTitle:(NSString*)subTitle withCoordinate:(CLLocationCoordinate2D)coordinate;
@end
//************************************自定义大头针 .m文件
#import "MyAnn.h"
@implementation MyAnn
-(id)initWithTitle:(NSString *)title withSubTitle:(NSString *)subTitle withCoordinate:(CLLocationCoordinate2D)coordinate
{
self=[super
init];
if (self)
{
self.name=title;
self.address=subTitle;
self.coordinate=coordinate;
}
return
self;
}
-(NSString*)title
{
return
self.name;
}
-(NSString*)subTitle
{
return
self.address;
}
@end
//************************************MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController :
UIViewController<MKMapViewDelegate>
{
MKMapView *map_view;
}
@end
//************************************MapViewController.m
#import "MapViewController.h"
#import "MyAnn.h"
@implementation MapViewController
- (void)viewDidLoad
{
[super
viewDidLoad];
//创建对象
map_view=[[MKMapView
alloc] initWithFrame:self.view.frame];
map_view.mapType=MKMapTypeStandard;
map_view.showsUserLocation=YES;
map_view.delegate=self;
map_view.userLocation.title=@"我的位置";
map_view.userLocation.subtitle=@"娃哈哈";
[self.view
addSubview:map_view];
//定位按钮
UIButton *button=[UIButton
buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0,
20, 60,
60);
button.backgroundColor=[UIColor
redColor];
[button setTitle:@"定位"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:button];
//在地图上画图
//画多变形
曲线
圆
CLLocationCoordinate2D points[4];
points[0] =
CLLocationCoordinate2DMake(41.000512, -109.050116);
points[1] =
CLLocationCoordinate2DMake(41.002371, -102.052066);
points[2] =
CLLocationCoordinate2DMake(36.993076, -102.041981);
points[3] =
CLLocationCoordinate2DMake(36.99892, -109.045267);
// MKPolygon *gon=[MKPolygon polygonWithCoordinates:points count:4];
// [map_view addOverlay:gon];
MKCircle *circle=[MKCircle
circleWithCenterCoordinate:points[0]
radius:5000];
[map_view
addOverlay:circle];
MKPolyline *line=[MKPolyline
polylineWithCoordinates:points count:4];
[map_view
addOverlay:line];
}
-(void)btnClick:(UIButton *)button;
{
map_view.centerCoordinate=map_view.userLocation.coordinate;
//创建一个以用户为中心的区域
附近多少米
MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(map_view.userLocation.coordinate,
5000,
5000);
[map_view
setRegion:region animated:YES];
MyAnn *myAnn=[[MyAnn
alloc] initWithTitle:@"大头针"
withSubTitle:@"小毛驴儿" withCoordinate:map_view.userLocation.coordinate];
[map_view
addAnnotation:myAnn];
}
//将用户位置作为屏幕中心
此方法是代理方法 自动调用
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation
*)userLocation
{
map_view.centerCoordinate=userLocation.coordinate;
}
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *identifer=@"AnnotationView";
MKPinAnnotationView *annView=(MKPinAnnotationView*)[map_view
dequeueReusableAnnotationViewWithIdentifier:identifer];
if (annView==nil)
{
annView=[[MKPinAnnotationView
alloc] initWithAnnotation:annotation
reuseIdentifier:identifer];
}
//判断是否是用户标记点
if (mapView.userLocation==annotation)
{
return nil;
}
//设置打头针的颜色
annView.pinColor=MKPinAnnotationColorPurple;
//设置能够显示弹出气泡
annView.canShowCallout=YES;
//添加右边的试图
UIButton *button=[UIButton
buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView=button;
//视图
UIImageView *imageView=[[UIImageView
alloc] initWithFrame:CGRectMake(0,
0, 32,
32)];
imageView.image=[UIImage
imageNamed:@"b.jpg"];
annView.leftCalloutAccessoryView=imageView;
return annView;
}
//点击详情
调用的方法
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
//调用浏览器打开浏览器
[[UIApplication
sharedApplication] openURL:[NSURL
URLWithString:@"http://www.baidu.com"]];
}
//画 renderer
描绘 装饰品
-(MKOverlayRenderer*)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
//判断覆盖的类型
if ([overlay isKindOfClass:[MKPolygon
class]])
{
//创建多边形渲染
MKPolygonRenderer *aRenderer=[[MKPolygonRenderer
alloc]
initWithPolygon:(MKPolygon*)overlay];
//多边形的填充颜色
aRenderer.fillColor=[UIColor
redColor];
//边框的颜色
aRenderer.strokeColor=[UIColor
blueColor];
//线的宽度
aRenderer.lineWidth=4;
return aRenderer;
}
else if ([overlay
isKindOfClass:[MKPolyline
class]])
{
MKPolylineRenderer *lineRenderer=[[MKPolylineRenderer
alloc]
initWithPolyline:(MKPolyline*)overlay];
lineRenderer.strokeColor=[UIColor
redColor];
lineRenderer.lineWidth=3;
return lineRenderer;
}
else if([overlay
isKindOfClass:[MKCircle
class]])
{
MKCircleRenderer *circleRenderer=[[MKCircleRenderer
alloc] initWithOverlay:(MKCircle*)overlay];
circleRenderer.strokeColor=[UIColor
redColor];
circleRenderer.fillColor=[UIColor
purpleColor];
circleRenderer.lineWidth=3;
return circleRenderer;
}
return
nil;
}
相关文章推荐
- ios 实现地图的定位,查询,自定义大头针
- iOS地图和定位服务(自定义大头针)
- iOS上地图画线,画线区域内的经纬度坐标点添加大头针
- iOS -- 地图画线,画线区域内的经纬度坐标点添加大头针
- iOS开发之地图-添加多个自定义的大头针及自定义大头针动画
- 地图与定位(三)自定义大头针视图
- 百度地图的使用定位,逆地理编码,自定义不同类型的大头针
- iOS上地图画线,画线区域内的经纬度坐标点添加大头针
- 【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等
- iOS 大头针 MKAnnotationView 自定义
- iOS定位添加大头针
- iOS 自定义百度地图大头针点击事件
- ios 地图大头针自定义显示图片 MKAnnotationView
- 【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等
- iOS 百度地图自定义大头针。
- iOS 百度地图_自定义Annotation大头针_修改大头针图片
- iOS 定位、自定义MKAnnotation、
- IOS开发-自定义全局变量方法来定位界面元素的方法
- 定位 - MapKit-自定义大头针
- iOS开发之百度地图大头针的自定义解决方法