iOS中地图的添加(定位、划…

//  ViewController.h
//  xiaoguodongMapTest
//
//  Created by Dong on 13-10-4.
//  Copyright (c) 2013年 dong. All rights reserved.
//

#import
#import
#import
@interface ViewController : UIViewController
{
    MKMapView *map;
}
@end



//
//  ViewController.m
//  xiaoguodongMapTest
//
//  Created by Dong on 13-10-4.
//  Copyright (c) 2013年 dong. All rights reserved.
//

#import "ViewController.h"

#import "customAnnotation.h"
@interface ViewController ()

@end

@implementation ViewController


-(void)createRoute
{
    //初始化数组
    CLLocationCoordinate2D routeCoords[5];
    //初始化每个点的经纬度
    routeCoords[0] = CLLocationCoordinate2DMake(39.949227, 116.4083);
    routeCoords[1] = CLLocationCoordinate2DMake(39.948997, 116.395555);
    routeCoords[2] = CLLocationCoordinate2DMake(39.947892, 116.390645);
    routeCoords[3] = CLLocationCoordinate2DMake(39.972345, 116.402345);
    routeCoords[4] = CLLocationCoordinate2DMake(39.95937, 116.405724);
    
    MKPolygon *routeLine = [MKPolygon polygonWithCoordinates:routeCoords count:5];
    [map addOverlay:routeLine];
    
}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
{
    MKPolylineView *plView = [[MKPolylineView alloc]initWithOverlay:overlay];
    //划线的颜色
    plView.strokeColor = [UIColor purpleColor];
    //划线的粗细
    plView.lineWidth = 6.0;
    return plView;
}

-(void)createAnnotationWithCoords:(CLLocationCoordinate2D) coords {
//customAnnotation *annotation = [[customAnnotation alloc] init];
    customAnnotation *annotation = [[customAnnotation alloc] initWithCoordinate:coords];
    //customAnnotation
//点击大头针出现的标题和子标题
annotation.title = @"标题";
annotation.subtitle = @"子标题";
[map addAnnotation:annotation];
    

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    map = [[MKMapView alloc]initWithFrame:[self.view bounds]];
    map.showsUserLocation = YES;
    map.mapType = MKMapTypeSatellite;
    
    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.992616,116.389632);
    
    float zoomLevel = 0.002;
    
    MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
    [map setRegion:[map regionThatFits:region] animated:YES];
    
    map.delegate = self;
 
    [self createRoute];
    [self createAnnotationWithCoords:coords];
    [self.view addSubview:map];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



//  customAnnotation.h
//  xiaoguodongMapTest
//
//  Created by Dong on 13-10-5.
//  Copyright (c) 2013年 dong. All rights reserved.
//

#import
#import
@interface customAnnotation : NSObject
{
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}
@property (nonatomic , readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic , retain) NSString *title;
@property (nonatomic , retain) NSString *subtitle;
-(id) initWithCoordinate: (CLLocationCoordinate2D)coords;
@end



//
//  customAnnotation.m
//  xiaoguodongMapTest
//
//  Created by Dong on 13-10-5.
//  Copyright (c) 2013年 dong. All rights reserved.
//

#import "customAnnotation.h"

@implementation customAnnotation
@synthesize coordinate,subtitle,title;

-(id) initWithCoordinate: (CLLocationCoordinate2D)coords
{
    if (self = [super init]) {
        coordinate = coords;
    }
    return self;
    
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值