您的位置:首页 > 移动开发 > IOS开发

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息