系统地图和高德地图

系统地图:

#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;
}

时间: 2024-08-02 10:18:15

系统地图和高德地图的相关文章

ios 一步一步学会自定义地图吹出框(CalloutView)--&gt;(百度地图,高德地图,google地图)

前言 在 ios上边使用地图库的同学肯定遇到过这样的问题:吹出框只能设置title和subtitle和左右的view,不管是百度地图还是高德地图还是自带的 google地图,只提供了这四个属性,如果想添加更多的view,只能自定义.可是,类库只能看到.h文件,.m都看不到,这让新手比较蛋疼,庞大的地 图类库一时半会摸不着头脑,从头再学还需要时间,本文就教大家快速制作一个属于自己的 CalloutView!等你一步一步调通后,再回过头来使用系统自带的方法设置callout,就会领悟这个过程. 正文

Android笔记:百度地图与高德地图坐标转换问题

Android笔记:百度地图与高德地图坐标转换问题 安卓项目使用了百度地图的定位SDK,web端使用的也是百度地图, 后来发现界面显示百度地图不如高德效果好,web改用高德地图,原本的百度地图坐标是可以直接使用的,由于高德和百度地图的坐标系不一致 要如何转换呢. 补充了下坐标系知识后发现高德使用的坐标系是“gcj02”也就是大家所说的“火星坐标”, 百度使用的是“BD09”因为是百度所用大家习惯称之为“百度坐标”  ,如何将bd09转为gcj02呢,突然想到在百度的定位sdk里有这样一段说明(来

arcgis api for js入门开发系列十七在线地图(天地图、百度地图、高德地图)

本篇主要讲述的是利用arcgis api加载互联网在线地图服务资源,简单封装一个底图切换控件js,在线地图包括:天地图.高德地图以及百度地图,效果图如下: 实现思路: 1.简单的底图切换控件map.LayerSwitcherToolbar.js文件,里面自定义加载天地图.高德地图以及百度地图类,其实都是继承TiledMapServiceLayer类: (1)高德地图: //高德地图图层扩展 GAODELayer = DObject({ id:null, esriLayer: null, esri

百度地图、高德地图的数据从哪里得到的?[声明我只是此文章的搬运工,从百度知道复制来的]

要说数据来源,首先得对地图数据做一个分类,因为不同分类的数据,其来源,采集方法都是有大不同的. 要明白地图的数据分类,必须先理解一个概念,就是地图图层的概念: 如上图,电子地图对我们实际空间的表达,事实上是通过不同的图层去描述,然后通过图层叠加显示来进行表达的过程. 对于我们地图应用目标的不同,叠加的图层也是不同的,用以展示我们针对目标所需要信息内容. 其次呢,我引入一下矢量模型和栅格模型的概念,GIS(电子地图)采用两种不同的数学模型来对现实世界进行模拟: 矢量模型:同多X,Y(或者X,Y,Z

百度地图、高德地图的数据从哪里得到的?

要说数据来源,首先得对地图数据做一个分类,因为不同分类的数据,其来源,采集方法都是有大不同的. 要明白地图的数据分类,必须先理解一个概念,就是地图图层的概念:        如上图,电子地图对我们实际空间的表达,事实上是通过不同的图层去描述,然后通过图层叠加显示来进行表达的过程. 对于我们地图应用目标的不同,叠加的图层也是不同的,用以展示我们针对目标所需要信息内容. 其次呢,我引入一下矢量模型和栅格模型的概念,GIS(电子地图)采用两种不同的数学模型来对现实世界进行模拟: 矢量模型:同多X,Y(

IOS原生地图与高德地图

原生地图 1.什么是LBS LBS: 基于位置的服务   Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位      2.基站定位      3.WIFI定位 3.框架 MapKit:地图框架,显示地图 CoreLocation:定位框架,没有地图时也可以使用定位. 4.如何使用原生地图<MapKit> 和定位<CoreLocation> MapKit: 1) 初始化MapView _map

iOS打开百度地图、高德地图导航

BOOL hasBaiduMap = NO; BOOL hasGaodeMap = NO; if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){ hasBaiduMap = YES; } if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap:

百度地图,高德地图,HTML5经纬度比较

对于一个地点的经纬度,是确定的?这个问题,我想很多人都会回答,肯定了,可实际上呢?我只能呵呵了. 在使用百度地图的过程中,发现一个很奇怪的现象,有时候调用百度地图js API时,后得到一个错的离谱的地方.然后我自己也就研究了一下jsAPI获取地理位置的相关信息,jsAPI其实都是利用了HTML5中的方法获取地理信息的.但很奇怪,使用HTML5获取到的经纬度和百度获取的竟然相差很大. HTML5获取到的经纬度 function getLocation() { if (navigator.geolo

iOS第三方地图-高德地图(导航sdk路径规划)

高德地图导航sdk的路径规划获取行程信息主要用到AMapNaviManager这个类 然后调下面的方法 /*! @brief 带起点的驾车路径计算 @param startPoints 起点坐标.支持多个起点,起点列表的尾点为实际导航起点,其他坐标点为辅助信息,带有方向性,可有效避免算路到马路的另一侧. @param endPoints 终点坐标.支持多个终点,终点列表的尾点为实际导航终点,其他坐标点为辅助信息,带有方向性,可有效避免算路到马路的另一侧. @param wayPoints 途经点