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
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
相关文章推荐
- 【iOS】高德地图MAMapKit的使用:地图显示、添加大头针、导航、定位功能介绍
- IOS——地图的使用、地图定位到当前位置(包括ios8获取位置)、地图标注的添加、地图放大缩小监听
- iOS 关于iOS8地图定位问题
- iOS开发——高德地图调研之显示地图、定位与地理编码等
- iOS MKAnnotation协议为地图添加注解
- 【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等
- iOS开发系列--地图与定位
- iOS谷歌原生地图开发[用户定位获取不到除了并不有效的KOV外]独家解决方案
- iOS定位服务与地图开发(2)---地理信息反编码
- iOS定位服务与地图开发(5)---使用程序外地图之调用iOS 6苹果地图
- iOS 之 使用百度地图 (删除地图页面所有自己添加的标注)
- iOS学习笔记19-地图(一)定位CoreLocation
- iOS-高德地图点击地图获取点击点对应的地理位置,并添加自定义的大头针
- iOS第三方地图-百度地图定位的封装
- IOS开发笔记之六——ios 8地图定位服务问题
- iOS开发系列--地图与定位
- IOS之地图和定位应用开发
- iOS开发系列--地图与定位
- iOS定位与地图功能
- ios系统地图无法定位问题