日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

iOS原生地图与高德地图的使用

發(fā)布時(shí)間:2025/4/5 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS原生地图与高德地图的使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原生地圖

1、什么是LBS

LBS: 基于位置的服務(wù) Location Based Service

實(shí)際應(yīng)用:大眾點(diǎn)評(píng),陌陌,微信,美團(tuán)等需要用到地圖或定位的App

2、定位方式

1.GPS定位 2.基站定位 3.WIFI定位

3、框架

MapKit:地圖框架,顯示地圖

CoreLocation:定位框架,沒(méi)有地圖時(shí)也可以使用定位.

4、如何使用原生地圖 和定位

MapKit:

1) 初始化MapView

_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];[self.view addSubview:_mapView];

2) 設(shè)置代理

_mapView.delegate = self;

3) 設(shè)置地圖類型

_mapView.mapType = MKMapTypeStandard;

4) 允許顯示自己的位置

_mapView.showsUserLocation = YES;

5) 設(shè)置地圖中心坐標(biāo)點(diǎn)

CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(22.540396,113.951832);_mapView.centerCoordinate = centerCoordinate;

6) 設(shè)置地圖顯示區(qū)域

a) 設(shè)置縮放

MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);

b) 設(shè)置區(qū)域

MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);

c) 顯示區(qū)域

_mapView.region = region;

CoreLocation:

7) 初始化定位管理器

_manager = [[CLLocationManager alloc] init];

_manager.delegate = self;

8) iOS8定位

  • 在info.plist中添加 Privacy - Location Usage Description , NSLocationAlwaysUsageDescription

  • 在代碼中加入

    if ( [UIDevice currentDevice].systemVersion.floatValue >= 8.0 ) {

    [_manager requestAlwaysAuthorization];

    }

  • 9) 開(kāi)啟定位

    [_manager startUpdatingLocation];

    10) 定位成功代理

    • (void)locationManager:(CLLocationManager )manager didUpdateLocations:(NSArray )locations

    {

    NSLog(@"定位成功");//獲取定位的坐標(biāo)CLLocation *location = [locations firstObject];//獲取坐標(biāo)CLLocationCoordinate2D coordinate = location.coordinate;NSLog(@"定位的坐標(biāo):%f,%f", coordinate.longitude, coordinate.latitude);//停止定位//[_manager stopUpdatingLocation];

    }

    11) 定位失敗代理

    • (void)locationManager:(CLLocationManager )manager didFailWithError:(NSError )error

    {

    NSLog(@"定位失敗”);

    }

    12) 屏幕坐標(biāo)轉(zhuǎn)經(jīng)緯度坐標(biāo)

    CLLocationCoordinate2D cl2d = [_mapView convertPoint:point toCoordinateFromView:_mapView];

    13) 反地理編碼

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

    //獲取地標(biāo)對(duì)象

    CLPlacemark *mark = [placemarks firstObject];}];

    大頭針(標(biāo)注):

    14) 添加大頭針

    //創(chuàng)建大頭針MKPointAnnotation *pointAnn = [[MKPointAnnotation alloc] init];//設(shè)置坐標(biāo)pointAnn.coordinate = CLLocationCoordinate2DMake(23.181297, 113.346877);//設(shè)置標(biāo)題pointAnn.title = @"我的第一個(gè)大頭針";//設(shè)置副標(biāo)題pointAnn.subtitle = @"副標(biāo)題";//顯示大頭針,把大頭針加入到地圖上[_mapView addAnnotation:pointAnn];

    15) 大頭針的復(fù)用及定制

    pragma mark - mapView 代理方法

    //大頭針View

    -(MKAnnotationView )mapView:(MKMapView )mapView viewForAnnotation:(id)annotation

    {

    //如果是自己當(dāng)前位置的大頭針,則不定制if ( [annotation isKindOfClass:[MKUserLocation class]]) {return nil;}

    if 1

    // 1、自帶的大頭針視圖MKPinAnnotationView *pinAnnView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];if ( !pinAnnView ) {pinAnnView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"];}//設(shè)置大頭針的顏色pinAnnView.pinColor = MKPinAnnotationColorPurple;//設(shè)置掉落動(dòng)畫(huà)pinAnnView.animatesDrop = YES;//是否彈出氣泡pinAnnView.canShowCallout = YES;//設(shè)置左視圖UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];leftView.backgroundColor = [UIColor blueColor];pinAnnView.leftCalloutAccessoryView = leftView;//設(shè)置右視圖UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];pinAnnView.rightCalloutAccessoryView = rightBtn;return pinAnnView;

    else

    //2、自定義大頭針視圖/** 區(qū)別于MKPinAnnotationView* 1、可以設(shè)置大頭針圖片* 2、不可以設(shè)置大頭針顏色和掉落動(dòng)畫(huà)*/MKAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID2"];if ( !customView ) {customView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID2"];}//設(shè)置點(diǎn)擊大頭針可以顯示氣泡customView.canShowCallout = YES;//設(shè)置大頭針圖片customView.image = [UIImage imageNamed:@"marker"];//設(shè)置左視圖UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];leftView.backgroundColor = [UIColor blueColor];customView.leftCalloutAccessoryView = leftView;//設(shè)置右視圖UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];customView.rightCalloutAccessoryView = rightBtn;return customView;

    endif

    }

    16) 移除大頭針

    [_mapView removeAnnotations:_mapView.annotations];

    17) 添加長(zhǎng)按手勢(shì),實(shí)現(xiàn)在地圖的長(zhǎng)按點(diǎn)添加一個(gè)大頭針

    //4、長(zhǎng)按地圖顯示大頭針

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];[_mapView addGestureRecognizer:longPress];

    pragma mark - 長(zhǎng)按手勢(shì)

    -(void)longPress:(UILongPressGestureRecognizer *)gesture

    {

    //避免多次調(diào)用 只允許開(kāi)始長(zhǎng)按狀態(tài)才添加大頭針if (gesture.state != UIGestureRecognizerStateBegan) {return;}//獲取長(zhǎng)按地圖上的某個(gè)點(diǎn)CGPoint point = [gesture locationInView:_mapView];//把point轉(zhuǎn)換成在地圖上的坐標(biāo)經(jīng)緯度CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];//添加長(zhǎng)按的大頭針MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];annotation.coordinate = coordinate;annotation.title = @"長(zhǎng)按的大頭針";annotation.subtitle = @"副標(biāo)題";[_mapView addAnnotation:annotation];

    }

    高德地圖

    1、高德地圖申請(qǐng)Appkey流程:

    a) 在瀏覽器中打開(kāi)網(wǎng)址:http://lbs.amap.com/api/ios-sdk/guide/verify/

    b) 訪問(wèn):http://lbs.amap.com/console/key/,使用高德開(kāi)發(fā)者賬號(hào)登陸

    c) 2.在“KEY管理”頁(yè)面點(diǎn)擊上方的“獲取key”按鈕,依次輸入應(yīng)用名,選擇綁定的服務(wù)為“iOS平臺(tái)SDK”,輸入Bundle Identifier(Bundle Identifier獲取方式為Xcode->General->Identity)

    <Bundle Identifier : com.qianfeng.gaodedemo >

    <APIKEY : e848d391f9c4b98db0935052777f99d2 >

    2、高德地圖配置工程流程:

    a)下載高德地圖iOS SDK

    b)添加高德地圖的庫(kù)文件MAMapKit.framework

    c)添加8個(gè)關(guān)聯(lián)庫(kù)QuartzCore, CoreLocation, SystemConfiguration, CoreTelephony, libz, OpenGLES, libstdc++6.09, Security(MAMapView)

    d)添加AMap.bundle(MAMapKit.framework->Resources)

    e) TARGETS-Build Settings-Other Linker Flags 中添加內(nèi)容: -ObjC;

    f)在代碼中添加用戶Key: [MAMapServices sharedServices].apiKey =@"您的key";

    3、單獨(dú)使用搜索服務(wù)包:

    a)添加搜索庫(kù)文件AMapSearchKit.framework

    b)添加關(guān)聯(lián)庫(kù)SystemConfiguration, CoreTelephony, libz, libstdc++6.09

    c)在代碼中添加AMapSearchAPI *search = [[AMapSearchAPI alloc] initWithSearchKey: @"您的key" Delegate:self];

    4、如何使用高德地圖 和 搜索

    MAMapKit:

    0) 配置高德地圖API

    define APIKEY @"e848d391f9c4b98db0935052777f99d2"

    [MAMapServices sharedServices].apiKey = APIKEY;

    1) 初始化MAMapView

    _maMapView = [[MAMapView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:_maMapView];

    2) 設(shè)置代理

    _maMapView.delegate = self;

    3) 設(shè)置地圖類型

    _maMapView.mapType = MAMapTypeStandard;

    4) 允許顯示自己的位置(如使用定位功能,則必須設(shè)置為YES)

    _maMapView.showsUserLocation = YES;

    5) 設(shè)置logo位置

    _maMapView.logoCenter = CGPointMake(100, 100);

    6) 顯示羅盤(pán)

    _maMapView.showsCompass = YES;

    7) 顯示交通

    _maMapView.showTraffic = YES;

    8) 是否支持旋轉(zhuǎn)

    _maMapView.rotateEnabled = YES;

    9) 是否支持拖動(dòng)

    _maMapView.scrollEnabled = YES;

    10) 是否支持縮放

    _maMapView.zoomEnabled = NO;

    11) 設(shè)置地圖顯示區(qū)域

    a) 設(shè)置坐標(biāo)

    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.181297, 113.346877);

    b) 設(shè)置縮放

    MACoordinateSpan span = MACoordinateSpanMake(0.1, 0.1);

    c) 設(shè)置區(qū)域

    MACoordinateRegion region = MACoordinateRegionMake(coordinate, span);

    d) 顯示區(qū)域

    _maMapView.region = region;

    12) 定位

    #pragma mark - 定位 回調(diào)方法

    -(void)mapView:(MAMapView )mapView didUpdateUserLocation:(MAUserLocation )userLocation updatingLocation:(BOOL)updatingLocation

    {

    NSLog(@"定位成功");CLLocation *location = userLocation.location;CLLocationCoordinate2D coordinate = location.coordinate;NSLog(@"我的坐標(biāo)位置:%f, %f", coordinate.longitude, coordinate.latitude);// 定位后,可設(shè)置停止定位// _maMapView.showsUserLocation = NO;

    }

    13) 添加標(biāo)注(大頭針)

    //添加標(biāo)注MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];annotation.coordinate = coordinate; //設(shè)置標(biāo)注的坐標(biāo)annotation.title = @"高德地圖標(biāo)題"; //設(shè)置標(biāo)題annotation.subtitle = @"副標(biāo)題"; //設(shè)置副標(biāo)題[_maMapView addAnnotation:annotation]; //將標(biāo)注添加在地圖上

    14) 標(biāo)注的復(fù)用及定制

    #pragma mark - 定制標(biāo)注視圖(和原生地圖定制方式類似)

    • (MAAnnotationView )mapView:(MAMapView )mapView viewForAnnotation:(id)annotation

    {

    //不定制自己位置的標(biāo)注視圖if ( [annotation isKindOfClass:[MAUserLocation class]]) {return nil;}

    #if 1

    // 1、自帶的標(biāo)注視圖MAPinAnnotationView *pinAnnView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];if ( !pinAnnView ) {pinAnnView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"];}// 是否可彈出視圖pinAnnView.canShowCallout = YES;// 設(shè)置掉落動(dòng)畫(huà)pinAnnView.animatesDrop = YES;// 設(shè)置標(biāo)注顏色pinAnnView.pinColor = MAPinAnnotationColorGreen;// 設(shè)置左視圖UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];leftView.backgroundColor = [UIColor blueColor];pinAnnView.leftCalloutAccessoryView = leftView;//設(shè)置右視圖UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];pinAnnView.rightCalloutAccessoryView = rightBtn;return pinAnnView;

    #else

    //2、自定義標(biāo)注視圖MAAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID2"];if ( !customView ) {customView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID2"];}//設(shè)置點(diǎn)擊大頭針可以顯示氣泡customView.canShowCallout = YES;//設(shè)置大頭針圖片customView.image = [UIImage imageNamed:@"marker"];//設(shè)置左視圖UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];leftView.backgroundColor = [UIColor blueColor];customView.leftCalloutAccessoryView = leftView;//設(shè)置右視圖UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];customView.rightCalloutAccessoryView = rightBtn;return customView;

    #endif

    }

    15) 添加長(zhǎng)按手勢(shì)

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];[_maMapView addGestureRecognizer:longPress];

    #pragma mark -- 長(zhǎng)按手勢(shì)Action

    -(void)longPress:(UILongPressGestureRecognizer *)longPress

    {

    if (longPress.state != UIGestureRecognizerStateBegan) {return;}//獲取點(diǎn)位置CGPoint point = [longPress locationInView:_maMapView];//將點(diǎn)位置轉(zhuǎn)換成經(jīng)緯度坐標(biāo)CLLocationCoordinate2D coordinate = [_maMapView convertPoint:point toCoordinateFromView:_maMapView];//在該點(diǎn)添加一個(gè)大頭針(標(biāo)注)MAPointAnnotation *pointAnn = [[MAPointAnnotation alloc] init];pointAnn.coordinate = coordinate;pointAnn.title = @"長(zhǎng)按的大頭針";pointAnn.subtitle = @"副標(biāo)題";[_maMapView addAnnotation:pointAnn];

    }

    AMapSearchKit:

    1) 搜索周邊

    0) 創(chuàng)建AMapSearchAPI對(duì)象,配置APPKEY,同時(shí)設(shè)置代理對(duì)象為self

    searchAPI = [[AMapSearchAPI alloc] initWithSearchKey:APIKEY Delegate:self];

    a) 創(chuàng)建搜索周邊請(qǐng)求類

    AMapPlaceSearchRequest *searchRequest = [[AMapPlaceSearchRequest alloc] init];

    b) 設(shè)置搜索類型(按關(guān)鍵字搜索)

    searchRequest.searchType = AMapSearchType_PlaceKeyword;

    c) 設(shè)置關(guān)鍵字

    searchRequest.keywords = keywordsTextField.text;

    d) 設(shè)置搜索城市

    searchRequest.city = @[@"廣州"];

    e) 開(kāi)始搜索

    [searchAPI AMapPlaceSearch:searchRequest];

    2) 搜索周邊回調(diào)方法

    a) 搜索失敗

    • (void)searchRequest:(id)request didFailWithError:(NSError *)error

    {

    NSLog(@"搜索失敗");

    }

    b) 搜索成功

    -(void)onPlaceSearchDone:(AMapPlaceSearchRequest )request response:(AMapPlaceSearchResponse )response

    {

    //清空原來(lái)的標(biāo)注(大頭針)[_maMapView removeAnnotations:_maMapView.annotations];//判斷是否為空if (response) {//取出搜索到的POI(POI:Point Of Interest)for (AMapPOI *poi in response.pois) {//poi的坐標(biāo)CLLocationCoordinate2D coordinate =

    CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);

    //地名NSString *name = poi.name;//地址NSString *address = poi.address;//用標(biāo)注顯示MAPointAnnotation *pointAnn = [[MAPointAnnotation alloc] init];pointAnn.coordinate = coordinate;pointAnn.title = name;pointAnn.subtitle = address;[_maMapView addAnnotation:pointAnn];}}

    }

    3) 添加折線

    pragma mark - 畫(huà)折線

    -(void)drawPolyLine

    {

    //初始化點(diǎn)NSArray *latitudePoints =[NSArray arrayWithObjects:@"23.172223",@"23.163385",@"23.155411",@"23.148765",@"23.136935", nil];NSArray *longitudePoints = [NSArray arrayWithObjects:@"113.348665",@"113.366056",@"113.366128",@"113.362391",@"113.356785", nil];// 創(chuàng)建數(shù)組CLLocationCoordinate2D polyLineCoords[5];for (int i=0; i<5; i++) {polyLineCoords[i].latitude = [latitudePoints[i] floatValue];polyLineCoords[i].longitude = [longitudePoints[i] floatValue];}// 創(chuàng)建折線對(duì)象MAPolyline *polyLine = [MAPolyline polylineWithCoordinates:polyLineCoords count:5];// 在地圖上顯示折線[_maMapView addOverlay:polyLine];

    }

    pragma mark - 定制折線視圖

    -(MAOverlayView )mapView:(MAMapView )mapView viewForOverlay:(id)overlay

    {

    if ([overlay isKindOfClass:[MAPolyline class]]) {MAPolylineView *polyLineView = [[MAPolylineView alloc] initWithPolyline:overlay];polyLineView.lineWidth = 2; //折線寬度polyLineView.strokeColor = [UIColor blueColor]; //折線顏色polyLineView.lineJoinType = kMALineJoinRound; //折線連接類型return polyLineView;}return nil;

    }

    轉(zhuǎn)載于:https://www.cnblogs.com/YYSheng/p/5553976.html

    總結(jié)

    以上是生活随笔為你收集整理的iOS原生地图与高德地图的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。