高德开放平台
这里有两个地方可以参考,
https://lbs.amap.com/
百度开放平台
http://lbsyun.baidu.com/
0.选哪个
https://baijiahao.baidu.com/s?id=1616824316336369139&wfr=spider&for=pc
1.经纬度转换
https://blog.csdn.net/qq_35122556/article/details/80020370
2模拟定位
https://www.jianshu.com/p/f76fd7b19eac
这里有个问题:
定位之后手机要重启,不然你手机定位改不过来,
3。授权相关问题
https://www.jianshu.com/p/12e3f54fc0f7
4.带方向的定位小蓝点--高德地图ios
高德地图当前位置图标旋转功能实现-高德地图iOS
带箭头定位小蓝点-高德地图iOS
原理:手机的旋转角度-地图旋转角度,加个旋转动画
为什么贴代码呢,官方文档不是有么?因为官方文档上还有一些乱七八遭的东西,往往一个简单的功能非要让你阅读四五个乱七八糟的文件,特讨厌。
#import "ViewController.h"
#import
#import
static const NSUInteger kUserHearderAnnotaionViewTag = 10000;
@interface ViewController ()
@property (nonatomic, strong) MAMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self creatMapView];
}
- (void)creatMapView {
_mapView = [[MAMapView alloc]initWithFrame:self.view.bounds];
_mapView.delegate = self;
_mapView.showsCompass = NO;
_mapView.showsUserLocation = YES;
// _mapView.userTrackingMode = MAUserTrackingModeFollow;
//是否自定义用户位置精度圈
_mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
[self.view addSubview:_mapView];
}
#pragma mark MAMapViewDelegate
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
NSLog(@"%f,%f,%f",userLocation.heading.x,userLocation.heading.y,userLocation.heading.z);
//通过hearderAnnotationView的tag值拿到当前位置的annotationView
MAAnnotationView *hearderAnnotationView = [self.mapView viewWithTag:kUserHearderAnnotaionViewTag];
if (hearderAnnotationView)
{
// hearderAnnotationView.transform = CGAffineTransformIdentity;
CGFloat rotateDegree = userLocation.heading.trueHeading - _mapView.rotationDegree;
// CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI*userLocation.heading.magneticHeading/180.0);
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI*rotateDegree/180.0);
hearderAnnotationView.transform = transform;
}
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation {
//用户当前位置大头针
if ([annotation isKindOfClass:[MAUserLocation class]])
{
static NSString *kUserLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:kUserLocationStyleReuseIndetifier];
if (annotationView == nil)
{
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kUserLocationStyleReuseIndetifier];
}
annotationView.canShowCallout = NO;
annotationView.image = [UIImage imageNamed:@"当前位置"];
annotationView.bounds = CGRectMake(0, 0, 27, 34.5);
//annotationView.contentMode = UIViewContentModeScaleToFill;
annotationView.layer.masksToBounds = YES;
//设置当前位置大头针annotationView的tag值
annotationView.tag = kUserHearderAnnotaionViewTag;
return annotationView;
}
//其他大头针
else if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
}
return nil;
}
@end
https://www.jianshu.com/p/3979c3d3b12d这个链接就是上面代码的原版,有bug,都改好了。
1.上面的定位图标的大小
annotationView.bounds = CGRectMake(0, 0, 27, 34.5);
如果改成frame,回出先bug,大小回改变。不知到原因。不是
annotationView.contentMode = UIViewContentModeScaleToFill;
的原因。
是它的原因:
annotationView.frame = CGRectMake(0, 0, 27, 34.5);
2.旋转方向翻了
3.地图转了但是指针不转,
官方:
https://lbs.amap.com/dev/demo/location-rotation-effect#iOS
5.高德地图大头针自定义气泡
原理:自定义一个view,大头针addsubview 这个自定义的view;设置这个view的中心点,使它显示在大头针的上方。
5.1 地图显示大头针
百度
5.2代码
其他的不用管,只要在下面注释横线中间的地方添加代码
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
static NSString *reuseIndetifier = @"annotationReuseIndetifier";
CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
if (annotationView == nil)
{
annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
}
annotationView.image = [UIImage imageNamed:@"restaurant"];
// 设置中心点偏移,使得标注底部中间点成为经纬度对应点
annotationView.centerOffset = CGPointMake(0, -18);
//————————————这中间是要添加的大头针气泡————————————————————————//
// 设置为NO,用以调用自定义的calloutView
annotationView.canShowCallout = NO;
UIView * aa = [[UIView alloc]init];
aa.frame = CGRectMake(0, 0, 100, 50);
//改变气泡位置,官方文档上的不用管
aa.center = CGPointMake(CGRectGetWidth(annotationView.bounds) / 2.f + annotationView.calloutOffset.x,
-CGRectGetHeight(aa.bounds) / 2.f + annotationView.calloutOffset.y);
aa.backgroundColor = [UIColor redColor];
[annotationView addSubview:aa];
//------——————————————————————————————————————————————//
return annotationView;
}
return nil;
}
##5.3气泡的图样
找美工要
6 高德地图跳转高德,百度,系统地图APP导航
6.1添加白名单
@property (nonatomic,assign) CLLocationDegrees latitudeSelf;//纬度自身
@property (nonatomic,assign) CLLocationDegrees longitudeSelf;//经度自身
@property (nonatomic,assign) CLLocationDegrees latitudePurpose;//纬度目的地
@property (nonatomic,assign) CLLocationDegrees longitudePurpose;//经度目的地
#pragma mark --小蓝点
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
//自身经纬度
CLLocation *location = userLocation.location ;
_latitudeSelf = location.coordinate.latitude;
_longitudeSelf = location.coordinate.longitude;
}
#pragma mark --导航按钮
- (void)navigationBtn:(UIButton *)btn{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]) {//判断手机装原生地图app了么
NSString *urlString=[NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",_latitudeSelf,_longitudeSelf,_latitudePurpose,_longitudePurpose ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:nil
completionHandler:^(BOOL success) {
}];
}else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {//高德
NSString *urlString = [NSString stringWithFormat:@"iosamap://path?&sid=BGVIS1&slat=%f&slon=%f&did=BGVIS2&dlat=%f&dlon=%f&dev=0&t=0",_latitudeSelf,_longitudeSelf,_latitudePurpose ,_longitudePurpose] ;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:nil
completionHandler:^(BOOL success) {
}];
}else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {//百度
//高德地图坐标转百度地图坐标
double pi = 3.1415926535897932384626;
double x = _longitudePurpose ;
double y = _latitudePurpose ;
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * pi);
double theta = atan2(y, x) + 0.000003 * cos(x * pi);
double mgLon1 = z * cos(theta) + 0.0065;
double mgLat1 = z * sin(theta) + 0.006;
// //下标 1 为lat 下标 2 为lng
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%f,%f&mode=riding&src=车辆", mgLat1,mgLon1] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:nil completionHandler:^(BOOL success) {
}];
}else{//都没有
[[MBProgressHUDManager manger] showaAndHiddenWithText:@"请安装百度地图APP或高德地图APP或系统地图APP"];
}
}
7.高德地图路径规划
官方:
https://lbs.amap.com/api/ios-sdk/guide/route-plan/walk
8.模糊搜索,联想搜索
#import
#import
#import
@property (nonatomic, strong) AMapSearchAPI *search;//搜索
self.search = [[AMapSearchAPI alloc] init];
self.search.delegate = self;
#pragma mark -- __________________________————————————————————点击事件
#pragma mark -- 搜索
- (void)searchCarClick:(UIButton *)sender{
AMapInputTipsSearchRequest *tipsRequest = [[AMapInputTipsSearchRequest alloc] init];
tipsRequest.keywords = @"五环";
tipsRequest.city = @"北京";
tipsRequest.cityLimit = YES;
[_search AMapInputTipsSearch: tipsRequest];
}
#pragma mark --模糊搜索
-(void)onInputTipsSearchDone:(AMapInputTipsSearchRequest*)request response:(AMapInputTipsSearchResponse *)response
{
if(response.tips.count == 0) {
return;
}
NSMutableArray *tempArray = [NSMutableArray array];
for (AMapTip *tip in response.tips) {
// AdressModel *ads = [[AdressModel alloc]init];
// ads.uid = tip.uid;
// ads.name = tip.name;
// ads.district = tip.district;
// ads.address = tip.address;
// ads.adcode = tip.adcode;
// ads.latitude = tip.location.latitude;
// ads.longitude = tip.location.longitude;
// [tempArray addObject:ads];
}
// self.dataArray = [tempArray copy];
// [_tableView reloadData];
}
9 百度 高德 gps 之间坐标系统相互转换
https://www.jianshu.com/p/27dbe24e8771
高德地图地理围栏:官网
判断一个点是否在多边形内:
https://www.jianshu.com/p/fb1177cac1ec