- (void)creatMapView
{
MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:map];
map.tag = 1000;
map.delegate = self;
self.mapView = map;
// 判断定位功能是否开启
if ([CLLocationManager locationServicesEnabled]) {
CLLocationManager *manager = [[CLLocationManager alloc] init];
manager.delegate = self;
self.manager = manager;
[manager startUpdatingLocation];
}else
{
NSLog(@"请开启定位功能!");
}
}
//- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
//{
// if (mapView.tag == 100) {
// NSLog(@"mapViewUserLoction:%f,%f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
// [mapView showsUserLocation];
// }
//}
// 获取通过CLLocationManager获取自己位置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
[self.manager stopUpdatingLocation];
[self showUserMapRegioInCenterLoction:location];
}
// 显示地图区域
- (void)showUserMapRegioInCenterLoction:(CLLocation *)loction
{
if (loction) {
// 拿到CLLocation里的经纬度
CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(loction.coordinate.latitude, loction.coordinate.longitude);
// 根据选择范围确定地图显示范围 1m = 0.00001纬度
MKCoordinateSpan span = MKCoordinateSpanMake(500*0.00001*2, 500*0.00001*2);
self.mapView.region = MKCoordinateRegionMake(coordinate2D, span);
self.mapView.showsUserLocation = YES; // 在地图上显示
self.loction = loction;
// 给地图加个圆圈 , radius单位为m
MKCircle *circle = [MKCircle circleWithCenterCoordinate:coordinate2D radius:500];
[self.mapView addOverlay:circle];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(zoomToAnnotation) userInfo:nil repeats:NO];
}
}
// 在地图上加入标记信息
- (void)zoomToAnnotation
{
NSLog(@"=====================%f,%f",self.loction.coordinate.latitude, self.loction.coordinate.longitude);
if (self.loction) {
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(self.loction.coordinate.latitude, self.loction.coordinate.longitude);
annotation.title = @"银都大厦";
annotation.subtitle = @"尼格科技有限责任公司滴答滴答滴答滴答滴答滴答的的";
[self.mapView addAnnotation:annotation];
[self.mapView selectAnnotation:annotation animated:YES];
// 如果是多个大头针可以用这个方法
// [self.mapView addAnnotations:[NSArray array]];
}
}
#pragma mark -- mapView delegate
// 定义标记视图
- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<<span style="color: #703daa">MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}else if ([annotation isMemberOfClass:[MKPointAnnotation class]])
{
static NSString *identifier = @"pin";
MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (pinAnnotation == nil) {
pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// 大头针上添加button
pinAnnotation.rightCalloutAccessoryView = rightButton;
// 大头针下落动画
pinAnnotation.animatesDrop = YES;
// 大头针下落后显示文字
pinAnnotation.canShowCallout = YES;
// 拖动大头针
pinAnnotation.draggable = YES;
// 设置大头针颜色
pinAnnotation.pinColor = MKPinAnnotationColorGreen;
return pinAnnotation;
}else
{
static NSString *identifier = @"imagePin";
MKAnnotationView *customAnnotation = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (customAnnotation == nil) {
customAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
// 自定义标记上视图
customAnnotation.rightCalloutAccessoryView = [[UIView alloc] init];
// 自定义地图标记图片
customAnnotation.image = [UIImage imageNamed:@""];
customAnnotation.canShowCallout = YES;
return customAnnotation;
}
}
// 点击大头针或者地图标记上的button按钮触发
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"=====datouzhen");
// 判断是否为大头针上的button
if([view isKindOfClass:[MKPinAnnotationView class]])
{
NSLog(@"===----==datouzhen");
}
}
// 给圆圈设置必要的属性
- (MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id<<span style="color: #703daa">MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKCircle class]]) { // 从mapview里找出MKCircle
// 创建MKCircleView
MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];
// 设置圆圈里的颜色
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.1];
// 设置圆边线的颜色
circleView.strokeColor = [[UIColor cyanColor] colorWithAlphaComponent:0.7];
// 边线宽度
circleView.lineWidth = 3.0;
return circleView;
}else
{
return nil;
}
}