系统地图和高德地图

系统地图:

#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>{
    MKMapView *_mapView;
    CLLocationManager *_manager;
    UIImageView* _imageView;
}
@end
@implementation ViewController
- (BOOL)prefersStatusBarHidden{
    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    //地图类型:MKMapTypeHybrid混合类型,MKMapTypeSatellite:卫星地图,MKMapTypeStandard:标准地图
    _mapView.mapType = MKMapTypeStandard;  
    //经纬度:39.96028(纬度),116.429672
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(39.91528,116.397672);
    //缩放比例
    MKCoordinateSpan span = MKCoordinateSpanMake(0.05, 0.05);
    //显示区域
    MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
    [_mapView setRegion:region animated:YES];
    _mapView.delegate = self;
    //显示用户位置
    _mapView.showsUserLocation = YES;
    [self.view addSubview:_mapView];
//    //大头针添加
//    MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];
//    //标题
//    anno.title = @"大头针";
//    //副标题
//    anno.subtitle = @"我叫大头针";
//    //显示位置
//    anno.coordinate = coordinate;
//    [_mapView addAnnotation:anno];
    //长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [_mapView addGestureRecognizer:longPress];
    //定位 卫星定位(A-gps),基站定位,Wi-Fi定位
    _manager = [[CLLocationManager alloc] init];
    //精确度
    _manager.desiredAccuracy = kCLLocationAccuracyBest;
    _manager.distanceFilter = 10;//距离过滤,超过10米时重新定位
    _manager.delegate = self;
    /*
     ios8中:
    首先要写这个方法:[_manager requestAlwaysAuthorization];
    其次要在Map文件夹中的文件夹Supporting Files--Map-Info.plist文件中添加:
     NSLocationAlwaysUsageDescription (string --地图应用要使用您的位置)
     */
    //开始定位
    [_manager startUpdatingLocation];
    //更新指向方向
    [_manager startUpdatingHeading];
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _imageView.image = [UIImage imageNamed:@"1.png"];
    [self.view addSubview:_imageView];
}
//更新方向
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
    NSLog(@"%f",newHeading.trueHeading);
    _imageView.transform = CGAffineTransformMakeRotation(-1 * newHeading.trueHeading * M_PI / 180);

}
//定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
   //取出最后一次定位的位置
    CLLocation *location = [locations lastObject];
    //停止定位
    [_manager stopUpdatingLocation];
    //显示定位的位置
    MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.1, 0.1));
    [_mapView setRegion:region animated:YES];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"定位失败");
}
//长按添加大头针
- (void)longPress:(UILongPressGestureRecognizer*)longPress{
   //判断状态
    if (longPress.state != UIGestureRecognizerStateBegan) {
        return;
    }
    //获取坐标
    CGPoint point = [longPress locationInView:_mapView];
    //转成经纬度
    CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];
    //创建大头针
    MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];
    //标题
    anno.title = @"大头针";
    //副标题
    anno.subtitle = @"还是大头针";
    //显示位置
    anno.coordinate = coordinate;
    [_mapView addAnnotation:anno];
}
//自定义大头针
- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //定位原点的样式,不写就是大头针样式
    if ([annotation isKindOfClass:[mapView.userLocation class]]) {
        return nil;
    }  
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"qqq"];
    if (pinView == nil) {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"qqq"];
    }
    //大头针详情页版
    pinView.canShowCallout = YES;
    //掉下的动画
    pinView.animatesDrop = YES;
    //可拖拽
    pinView.draggable = YES;
    //颜色
    pinView.pinColor = MKPinAnnotationColorPurple;
    //左视图
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    leftView.backgroundColor = [UIColor redColor];
    pinView.leftCalloutAccessoryView = leftView;  
    //右视图
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = button;
    return pinView;
}

高德地图:

#import "ViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>
@interface ViewController ()<AMapSearchDelegate,MAMapViewDelegate>{
    MAMapView *_mapView;
    AMapSearchAPI *_search;
}

@end

@implementation ViewController

- (BOOL)prefersStatusBarHidden{
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //注册key
    [MAMapServices sharedServices].apiKey = @"1acf6a53864215166147ef52511e69ba";
    
    _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    //是否显示交通
    _mapView.showTraffic = NO;
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    
    _search = [[AMapSearchAPI alloc] initWithSearchKey:@"1acf6a53864215166147ef52511e69ba" Delegate:self];
    
    //搜索兴趣点
    [self poi];
    
    //正向地理编码
    [self geocode];
    
    //行程路线规划
    [self navigation];
}

//行程路线规划
- (void)navigation{
    //构造AMapNavigationSearchRequest对象,配置查询参数
    AMapNavigationSearchRequest *naviRequest= [[AMapNavigationSearchRequest alloc] init];
    naviRequest.searchType = AMapSearchType_NaviDrive;
    naviRequest.requireExtension = YES;
    naviRequest.origin = [AMapGeoPoint locationWithLatitude:39.91528 longitude:116.397672];
    naviRequest.destination = [AMapGeoPoint locationWithLatitude:20.0579337 longitude:110.329279];
    
    //发起路径搜索
    [_search AMapNavigationSearch: naviRequest];
}
//实现路径搜索的回调函数
- (void)onNavigationSearchDone:(AMapNavigationSearchRequest *)request response:(AMapNavigationSearchResponse *)response{
    //取出一个方案
    AMapPath *mapPath = response.route.paths[0];
    //保存坐标的数组
    NSMutableArray *pointArray = [NSMutableArray array];
    //遍历路段
    for (AMapStep *step in mapPath.steps) {
        //分割坐标点串
        NSArray *array = [step.polyline componentsSeparatedByString:@";"];
        for (NSString *pointString in array) {
            [pointArray addObject:pointString];
        }
        //NSLog(@"%@",pointArray);
    }
    CLLocationCoordinate2D coordinate[pointArray.count];
    //遍历坐标点
    for (int i = 0; i < pointArray.count; i++) {
        NSArray *array = [pointArray[i] componentsSeparatedByString:@","];
        coordinate[i].longitude = [array[0] floatValue];
        coordinate[i].latitude = [array[1] floatValue];
    }
    
    //折线
    MAPolyline *polyLine = [MAPolyline polylineWithCoordinates:coordinate count:pointArray.count];
    [_mapView addOverlay:polyLine];
}
//自定义覆盖物 折线,圆,多边形。。。
- (MAOverlayView*)mapView:(MAMapView *)mapView viewForOverlay:(id<MAOverlay>)overlay{
   //如果是画线
    if ([overlay isKindOfClass:[MAPolyline class]]) {
        MAPolylineView *polyLineView = [[MAPolylineView alloc]initWithOverlay:overlay];
        //线宽
        polyLineView.lineWidth = 5;
        //颜色
        polyLineView.strokeColor = [UIColor redColor];
        
        polyLineView.lineJoinType = kMALineJoinRound;//连接类型
        polyLineView.lineCapType = kMALineCapRound;//端点类型
        
        return polyLineView;
    }
    
    return nil;
}

//正向地理编码
- (void)geocode{
    
    //构造AMapGeocodeSearchRequest对象,address为必选项,city为可选项
    AMapGeocodeSearchRequest *geoRequest = [[AMapGeocodeSearchRequest alloc] init];
    geoRequest.searchType = AMapSearchType_Geocode;
    geoRequest.address = @"西单";
    geoRequest.city = @[@"beijing"];
    
    //发起正向地理编码
    [_search AMapGeocodeSearch: geoRequest];
}

//实现正向地理编码的回调函数
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response{
    for (AMapGeocode *geoCode in response.geocodes) {
        NSLog(@"%@",geoCode.formattedAddress);
    }
}

//搜索兴趣点
- (void)poi{
    //构造AMapPlaceSearchRequest对象,配置关键字搜索参数
    AMapPlaceSearchRequest *poiRequest = [[AMapPlaceSearchRequest alloc] init];
    poiRequest.searchType = AMapSearchType_PlaceKeyword;
    poiRequest.keywords = @"海南大学";
    poiRequest.city = @[@"haikou"];
    poiRequest.requireExtension = YES;
    
    //发起POI搜索
    [_search AMapPlaceSearch: poiRequest];
}
//实现POI搜索对应的回调函数
- (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response{
    //遍历兴趣点
    for (AMapPOI *poi in response.pois) {
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);
        //创建大头针
        MAPointAnnotation *anno = [[MAPointAnnotation alloc] init];
        anno.title = poi.name;
        anno.subtitle = poi.address;
        anno.coordinate = coordinate;
        [_mapView addAnnotation:anno];
    }
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    MAPinAnnotationView *pinView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"qqq"];
    if (pinView == nil) {
        pinView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"qqq"];
    }
    pinView.canShowCallout = YES;
    
    return pinView;
}

posted @ 2015-04-20 15:38  飞天至虹  阅读(299)  评论(0编辑  收藏  举报