iOS 系统自带的地图定位功能实现:
环境配置
第一步ios8.0以上需要在plist中设置用户授权,和http白名单
第二步导入 MapKit(地图)+CoreLocation(定位)框架 遵守
CLLocationManagerDelegate,MKMapViewDelegate协议
代码部分
第一部分定位_locationM=[[CLLocationManager alloc]init];
_locationM.delegate=self;
_locationM.distanceFilter=1.0f;//设置过滤器的距离
_locationM.desiredAccuracy=kCLLocationAccuracyBest;//设置精确度
//[UIDevice currentDevice] 获取当前的机器 拿到机器的系统版本
if ([[[UIDevice currentDevice]systemVersion] floatValue]>=8.0) {
[_locationM requestWhenInUseAuthorization];
}
[_locationM startUpdatingLocation];//开始定位
定位有2个代理方法
1开始定位成功和位置发生改变时调用
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
//数组locations中最后一个元素就是最后更新的位置
CLLocation *location=[locations lastObject];
//根据经纬度反向地理编译出地址信息
//初始化
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark=placemarks[0];
NSLog(@"省:%@ 市:%@ 街道:%@",placeMark.administrativeArea,placeMark.locality,placeMark.thoroughfare);
}];
}
//定位失败的时候调用的方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"%@",error);
}
=============================================================================================
第二部分大头针
//这边是自己写的目标经纬度,实际开发中是服务器根据定位点传过来的数据
/这个样式是默认样式,/
MKPointAnnotation *annnotation1 = [[MKPointAnnotation alloc]init];
[annnotation1 setCoordinate:CLLocationCoordinate2DMake(31.232899, 121.433593)];
[annnotation1 setTitle:@"大胡子烧烤"];
[annnotation1 setSubtitle:@"康定路385号"];
[mpview addAnnotation:annnotation1];
大头阵样式2可以点击详情
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
//判断是不是用户定位
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
MKPinAnnotationView *customPinView = (MKPinAnnotationView*)[mapView
dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!customPinView) {
customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CustomPinAnnotationView"];
}
customPinView.pinTintColor = [UIColor blueColor];
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];
rightButton.backgroundColor = [UIColor grayColor];
[rightButton setTitle:@"查看详情" forState:UIControlStateNormal];
customPinView.rightCalloutAccessoryView = rightButton;
UIImageView *myCustomImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage"]];
customPinView.leftCalloutAccessoryView = myCustomImage;
return customPinView;
}
return nil;
//点击详情的时候调用
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
}
================================================================================================
第三部分导航
//目的地经纬度
CLLocationCoordinate2D coords2 = CLLocationCoordinate2DMake(31.232899,121.433593);
//目的地的位置
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coords2 addressDictionary:nil]];
toLocation.name = @"大胡子烧烤";
NSArray *items = [NSArray arrayWithObjects:toLocation, nil];
NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
//打开苹果自身地图应用,并呈现特定的item
[MKMapItem openMapsWithItems:items launchOptions:options];