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

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

2013-10-17 13:26 323 查看
原文地址: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: