CLLocationManager定位经纬度,MKReverseGeocoder地址解析, MKMapView 地图显示 iphone

本文主要是介绍CLLocationManager定位经纬度,MKReverseGeocoder地址解析, MKMapView 地图显示 iphone,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

讲的相当详细

from:http://hi.baidu.com/johnzhjfly/blog/item/ae8b7dd139dd7ac8572c84c4.html

CLLocationManager

CLLocationManager使用步骤
1.创建一个CLLocationManager实例
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
2.设置CLLocationManager实例委托和精度
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
3.设置距离筛选器distanceFilter,下面表示设备至少移动1000米,才通知delegate


locationManager.distanceFilter = 1000.0f;
或者没有筛选器的默认设置:


locationManager.distanceFilter = kCLDistanceFilterNone;
4.启动请求


[locationManager startUpdatingLocation];
5.停止请求
[locationManager stopUpdatingLocation];
罗盘功能

1.通过CLLocationManagerDelegate的- (void)locationManager:(CLLocationManager *)manager
       didUpdateHeading:(CLHeading *)newHeading 获取当前用户手机与地磁北极或者地理北极的夹角。
夹角范围[0, 360)


0-north 90-east 180-south


2.将罗盘上的指南针旋转一定角度


        CGFloat heading = (-1.0f*M_PI*newHeading.trueHeading)/180.f;
        self.circleView.transform = CGAffineTransformMakeRotation(heading);       
注意事项:


1.CLLocationManager用于获取当前位置,定位有相当大的误差。如果app不需要实时的获取位置信息且
使用了MKMapView, 则使用MKMapView获取当前位置,这个位置比较准确

2.desiredAccuracy适合需求就可以了,并不是都需要最佳的精度。
3.当不需要更新位置信息的时候,一定要停止。
MKReverseGeocoder查询地理位置信息,包括国家、城市、区域等等
1.实例MKReverseGeocoder
    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
    geocoder.delegate = self;
    [geocoder start];
2.在MKReverseGeocoderDelegate方法中获取placemark信息
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 成功查询出了地理位置信息
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error 获取失败
注意事项:


不需要的时候一定要调用cancel方法,停止查询


MKMapView MapKit地图控件


可以使用在map上显示圆. 点, 大头针,画路径等。


1.MKMapViewDelegate的常用回掉方法
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views;
// mapView:annotationView:calloutAccessoryControlTapped: is called when the user taps on left & right callout accessory UIControls.
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view;
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view;
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay;
// Called after the provided overlay views have been added and positioned in the map.
- (void)mapView:(MKMapView *)mapView didAddOverlayViews:(NSArray *)overlayViews __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);
2.mapview缩小的时候, 有时后不会回掉regionDidChangeAnimated这个方法,可以通过添加gesturerecognizer检测这个时间。


但必须实现UIGestureRecognizerDelegate的
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer方法
以添加大头针为例


1.创建一个自定义的MKPinAnnotation类,必须遵守MKAnnotation协议


2.在mapview上添加一个MKPinAnnotation是实例


    MKPintAnnotation *pinAnnotation = [[MKPintAnnotation alloc] initWithCoordinate:_currentCoordinate];
    pinAnnotation.title = @"PIN-TITLE";
    pinAnnotation.subtitle = @"PIN_SUBTITLE";
    [_mapView addAnnotation:pinAnnotation];
3.实现- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
            MKPinAnnotationView *pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier] autorelease];
            pinView.animatesDrop = TRUE;
            pinView.pinColor = MKPinAnnotationColorRed;//MKPinAnnotationColorGreen;
            pinView.canShowCallout = TRUE;
            UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];            
            button.frame = CGRectMake(0, 0, 25, 25);
            pinView.rightCalloutAccessoryView = button;
}
注意事项:


实际使用的时候在添加overlay和annotation的时候需要先移除mapview上已经有的overlays和annotations,避免叠加在一起了。


使用Google API在mapview上画路径


在mapview画指定两点之间的路径的原理和在地图上添加一个overlayview是相同的,关键是获取路径的数据.


一般路径数据是经纬度的数据,需要转化成mapview上的的mkmappoint数据类型的。


1.调用Google API获取两点之间的路径数据,详见http://code.google.com/apis/maps/documentation/directions/
2.解析路径数据(json格式),将经纬度的点转化为mkmapoint


3.在mapview上加个polygon line overlay
 MKPolyline *routeLine = [MKPolyline polylineWithPoints:points count:numberOfPoints;
    [_mapView addOverlay:routeLine];
    [_mapView setVisibleMapRect:routeVisableRegion animated:YES];  
4.实现


- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    MKPolygonView *overlayPolygonView = nil;
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        CGFloat lineWidth = 6;
        UIColor *fillColor = [UIColor redColor];
        UIColor *strokeColor = [UIColor greenColor];
        overlayPolygonView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
        overlayPolygonView.fillColor = fillColor;
        overlayPolygonView.strokeColor = strokeColor;
        overlayPolygonView.lineWidth = lineWidth;
    }
    return overlayPolygonView;
}

这篇关于CLLocationManager定位经纬度,MKReverseGeocoder地址解析, MKMapView 地图显示 iphone的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/415071

相关文章

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

全面解析Golang 中的 Gorilla CORS 中间件正确用法

《全面解析Golang中的GorillaCORS中间件正确用法》Golang中使用gorilla/mux路由器配合rs/cors中间件库可以优雅地解决这个问题,然而,很多人刚开始使用时会遇到配... 目录如何让 golang 中的 Gorilla CORS 中间件正确工作一、基础依赖二、错误用法(很多人一开

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结

Spring Boot 3.x 中 WebClient 示例详解析

《SpringBoot3.x中WebClient示例详解析》SpringBoot3.x中WebClient是响应式HTTP客户端,替代RestTemplate,支持异步非阻塞请求,涵盖GET... 目录Spring Boot 3.x 中 WebClient 全面详解及示例1. WebClient 简介2.

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

Spring Boot3.0新特性全面解析与应用实战

《SpringBoot3.0新特性全面解析与应用实战》SpringBoot3.0作为Spring生态系统的一个重要里程碑,带来了众多令人兴奋的新特性和改进,本文将深入解析SpringBoot3.0的... 目录核心变化概览Java版本要求提升迁移至Jakarta EE重要新特性详解1. Native Ima

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六