地图定位

 2023-09-05 阅读 68 评论 0

摘要:2019独角兽企业重金招聘Python工程师标准>>> 地图定位 CoreLocation CLLocation *bjLoc Latitude 39.54 longitude:116.28 CLLocation *bjLoc Latitude 23.05 longitude:113.15 1、CoreLocation *CLLocationManager局部变量不能定位 *模拟器不支持定位,要手动

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

地图定位

CoreLocation

CLLocation *bjLoc  Latitude 39.54 longitude:116.28

CLLocation *bjLoc  Latitude 23.05 longitude:113.15

1、CoreLocation

*CLLocationManager局部变量不能定位

*模拟器不支持定位,要手动

代理

startUpdatingLocation

stopUpdatingLocation

locationManager:didUpdateLocations

a> *CLLocation  

b> *经纬度

    CLLocationCoordinate2D

c> *海拔

d> location.altitude

e> *速度

   location.speed

f> self.locManager.distanceFilter = 1000;

 //精确到1米 越精确精耗电

g> self.locManager.desiredAccuracy = 1;

*距离计算

CLLocation distanceFromLocation

广州 CLLocation *bjLoc  Latitude 39.54 longitude:116.28

北京 CLLocation *bjLoc  Latitude 23.05 longitude:113.15

*plist文件添加隐私描述

Privacy - Location Usage Description

计算两个经纬度间的直线距离

2、地理编码

CLGeocoder

>地理编码

*把地点名称解析成详细地理信息

self.geoCoder geocodeAddressString

placemarks放CLPlacemark(位置标记)

CLPlacemark里的属性成员

CLLocation *location = placeMark.location;

CLLocationCoordinate2D coordinate = location.coordinate;

注:longitude与latitude不要写错

>把地理位置解析成详细地理信息(116.35 40.16)

反地理编码

reverseGeocodeLocation

补充.

1.CLPlacemark的addressDictionary属性 遍历字典数据

addressDictionary enumerateKeysAndObjectsUsingBlock

2.block是在主线程调用,所以可以直接在block刷新UI

name                    :   地名

thoroughfare            :   街道

ubThoroughfare          :   街道相关信息,例如门牌等

locality                :   城市

subLocality             :   城市相关信息,例如标志性建筑

administrativeArea      :   直辖市

subAdministrativeArea   :   其他行政区域信息

postalCode              :   邮编

ISOcountryCode          :   国家编码

country;                :   国家

inlandWater             :   水源、湖泊

ocean;                  :   海洋

areasOfInterest         :   关联的或利益相关的地标

MapKit

1.MapKit使用的基本使用

*创建项目

添加框架

导入MapKit

运行项目

*设置mapType地理显示类型

*设置跟踪模式userTrackingMode

*设置代理

didUpdateUserLocation方法

大头针userLocation.title = @"北京";

位置信息userLocation.coordinate

显示居中mapView setCenterCoordinate

显示区域 mapView setRegion

regionDidChangeAnimated方法

*返回原点

2.大头针(随机添加)

*添加tap手势

*获取点位置转成mapView的地理位置

mapview的点转成经纬度

[self.mapView convertPoint:tapPoint toCoordinateFromView:tap.view]

*添加CZAnnotation实现MKAnnotation代理

*随机添加20个头针

在触摸点的位置随机添加20个位置arc4random_uniform(100)/ 1000.0

3.自定义大头针

传智地址 经度:116.3544839331055纬度:40.06214218062825

>

*自定义一个Tuangou大头针模型,先不要加图片属性

*touch的时候添加大头针

引出下面两个代理方法

mapView:viewForAnnotation(大头针模型)

返回nil时,系统会自己添加大头针

mapView:didAddAnnotationViews(大头什视图)

MKModernUserLocationView(用户无法用,苹果私有api)

MKPinAnnotationView

返回大头针

从缓存池里取(与tableView一样取法)

自己实现MKPinAnnotationView,要想显示标题与子标题,得设置pinView的canShowCallout canShowOffset

属性为YES

*实现从天而降效果animatesDrop

*pinColor别针颜色(枚举)

注:要写return pinView;

>直接让购View返回的是MKAnnotationView

然后在团购模型中添加图片

设置canShowCallout为YES

设置annoView.image改变大头针图片

设置calloutoffset = CGPointMake(0,-10)

*添加右边rightCalloutAccessoryView

*添加左边leftCalloutAccessoryView

4.自定义大头针描述

150 x 80

选添加团购大头针

再添加团购描述大头针

5.画线路

跟大头针一样 

//方向对象

addOverlay

MKPolyline

MKPolylineRenderer

//计算路线

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

//设置起点

MKPlacemark *fromPm = [[MKPlacemark alloc] initWithPlacemark:from];

request.source = [[MKMapItem alloc] initWithPlacemark:fromPm];

//设置终点

MKPlacemark *toPm = [[MKPlacemark alloc] initWithPlacemark:to];

request.destination = [[MKMapItem alloc] initWithPlacemark:toPm];

MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

  //计算路线

  [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {        

 

        NSLog(@"共有几条路线 %d",response.routes.count);

 

        for (MKRoute *route in response.routes) {

 

            [self.mapView addOverlay:route.polyline];

 

        }

 

    }];

 

 

 

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{

 

    MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithOverlay:overlay];

 

    render.strokeColor = [UIColor blueColor];

 

   render.lineWidth = 5

 

    return render;

 

}

 

 

 

 

 

6.使用系统的地图导航

 

[self.geocoder geocodeAddressString:@"广州" completionHandler:^(NSArray *placemarks, NSError *error) {

 

 MKMapItem *currentItem = [MKMapItem mapItemForCurrentLocation];

 

 CLPlacemark *gzCLPM = [placemarks lastObject];

 

 MKPlacemark *gzMKPM = [[MKPlacemark alloc] initWithPlacemark:gzCLPM];

 

 MKMapItem *destItem = [[MKMapItem alloc] initWithPlacemark:gzMKPM];

 

        

 

 NSMutableDictionary *options = [NSMutableDictionary dictionary];

 

options[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;//行车路线

 

options[MKLaunchOptionsShowsTrafficKey] = @YES ;

 

 options[MKLaunchOptionsMapTypeKey] = [NSNumber numberWithInt:MKMapTypeSatellite];

 

 [MKMapItem openMapsWithItems:@[currentItem,destItem] launchOptions:options];

 

    }];

 

转载于:https://my.oschina.net/itcastBlogs/blog/683080

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/1104.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息