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 Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir