1.导入地图的库:Mapkit.framework
2.在导入到工程.m中 导入对应的系统文件 如:
<MapKit/MapKit.h>
MKMapView *mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
mapView.delegate = self;
//地图样式
mapView.mapType = MKMapTypeHybrid;
// //缩放
// mapView.zoomEnabled = NO;
// //滑动
// mapView.scrollEnabled = NO;
//显示当前位置,位置发生变化时候会显示当前位置
mapView.showsUserLocation = YES;
[self.view addSubview:mapView];
//标注 (大头针)
自定义类
//添加的标注得自定义,同时实现<#(id<MKAnnotation>)#>协议
MyAnnotation *ann = [[MyAnnotation alloc]init];
ann.coordinate = CLLocationCoordinate2DMake(37.192279, -122.015586);
ann.title= @"精英";
ann.subtitle = @"河北四期";
[mapView addAnnotation:ann];
//覆盖物
这里要能显示效果需要调用渲染器-----在MKMapViewDelegate 的代理方法理写
CLLocationCoordinate2D points[4];//个数
points[0] = CLLocationCoordinate2DMake(37.192279, -122.015586);
points[1] = CLLocationCoordinate2DMake(30.192279, -122.015586);
points[2] = CLLocationCoordinate2DMake(30.192279, -112.015586);
points[3] = CLLocationCoordinate2DMake(37.192279, -112.015586);
// 闭合曲线
// MKPolygon *gon = [MKPolygon polygonWithCoordinates:points count:4];
// [mapView addOverlay:gon];
//折线
MKPolyline *line = [MKPolyline polylineWithCoordinates:points count:4];
[mapView addOverlay:line];
//圈圈
MKCircle *circle = [MKCircle circleWithCenterCoordinate:points[3] radius:100000];
[mapView addOverlay:circle];
//
}
//给覆盖物渲染颜色 ------------------代理方法
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay {
//判断是哪个类
if ([overlay isKindOfClass:[MKPolygon class]]) {
MKPolygonRenderer *render = [[MKPolygonRenderer alloc]initWithPolygon:overlay];
// 画笔颜色
render.strokeColor = [UIColor redColor];
//填充色
render.fillColor = [UIColor greenColor];
//线的宽度
render.lineWidth = 3;
return render;
}else if ([overlay isKindOfClass:[MKPolyline class]]){
MKPolylineRenderer *render = [[MKPolylineRenderer alloc]initWithPolyline:overlay];
render.strokeColor = [UIColor purpleColor];
render.lineWidth = 1;
return render;
}else{
MKCircleRenderer *render = [[MKCircleRenderer alloc]initWithOverlay:overlay];
render.strokeColor = [UIColor magentaColor];
render.fillColor = [UIColor cyanColor];
render.lineWidth = 2;
return render;
}
return nil;
}
// MyAnnotation.h
// MapTest
//
// Created by laoyu on 15/12/9.
// Copyright (c) 2015年 zhiyou. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
//自定义标注,必须实现协议的必选属性
//看到了属性马山想到 set get 方法
@interface MyAnnotation : NSObject<MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end