iOS原生地圖與高德地圖的使用

原生地圖ios

一、什麼是LBSc++

LBS: 基於位置的服務 Location Based Servicegit

實際應用:大衆點評,陌陌,微信,美團等須要用到地圖或定位的Appapi

二、定位方式數組

1.GPS定位 2.基站定位 3.WIFI定位瀏覽器

三、框架微信

MapKit:地圖框架,顯示地圖框架

CoreLocation:定位框架,沒有地圖時也可使用定位.ide

四、如何使用原生地圖 和定位 動畫

MapKit:

1) 初始化MapView

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

[self.view addSubview:_mapView];

2) 設置代理

_mapView.delegate = self;

3) 設置地圖類型

_mapView.mapType = MKMapTypeStandard;

4) 容許顯示本身的位置

_mapView.showsUserLocation = YES;

5) 設置地圖中心座標點

CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(22.540396,113.951832);

 _mapView.centerCoordinate = centerCoordinate;

6) 設置地圖顯示區域

a) 設置縮放

MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);

b) 設置區域

MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);

c) 顯示區域

_mapView.region = region;

CoreLocation:

7) 初始化定位管理器

_manager = [[CLLocationManager alloc] init];

_manager.delegate = self;

8) iOS8定位

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

  2. 在代碼中加入

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

    [_manager requestAlwaysAuthorization];

    }

9) 開啓定位

[_manager startUpdatingLocation];

10) 定位成功代理

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

{

NSLog(@"定位成功");



  //獲取定位的座標

  CLLocation *location = [locations firstObject];



  //獲取座標

  CLLocationCoordinate2D coordinate = location.coordinate;

  NSLog(@"定位的座標:%f,%f", coordinate.longitude, coordinate.latitude);



  //中止定位

  //[_manager stopUpdatingLocation];

}

11) 定位失敗代理

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

{

NSLog(@"定位失敗」);

}

12) 屏幕座標轉經緯度座標

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

13) 反地理編碼

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

//獲取地標對象

CLPlacemark *mark = [placemarks firstObject];

 }];

大頭針(標註):

14) 添加大頭針

//建立大頭針

   MKPointAnnotation *pointAnn = [[MKPointAnnotation alloc] init];



   //設置座標

   pointAnn.coordinate = CLLocationCoordinate2DMake(23.181297, 113.346877);



   //設置標題

   pointAnn.title = @"個人第一個大頭針";



   //設置副標題

   pointAnn.subtitle = @"副標題";



   //顯示大頭針,把大頭針加入到地圖上

   [_mapView addAnnotation:pointAnn];

15) 大頭針的複用及定製

pragma mark - mapView 代理方法

//大頭針View

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

{

//若是是本身當前位置的大頭針,則不定製

if ( [annotation isKindOfClass:[MKUserLocation class]]) {

    return  nil;

}

if 1

// 一、自帶的大頭針視圖

MKPinAnnotationView *pinAnnView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];

if ( !pinAnnView ) {

    pinAnnView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"];

}



//設置大頭針的顏色

pinAnnView.pinColor = MKPinAnnotationColorPurple;



//設置掉落動畫

pinAnnView.animatesDrop = YES;



//是否彈出氣泡

pinAnnView.canShowCallout = YES;



//設置左視圖

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

leftView.backgroundColor = [UIColor blueColor];

pinAnnView.leftCalloutAccessoryView = leftView;



//設置右視圖

UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

pinAnnView.rightCalloutAccessoryView = rightBtn;





return  pinAnnView;

else

//二、自定義大頭針視圖

/*

   * 區別於MKPinAnnotationView

   * 一、能夠設置大頭針圖片

   * 二、不能夠設置大頭針顏色和掉落動畫

   */



MKAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID2"];

if ( !customView ) {

    customView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID2"];

}



//設置點擊大頭針能夠顯示氣泡

customView.canShowCallout = YES;



//設置大頭針圖片

customView.image = [UIImage imageNamed:@"marker"];



//設置左視圖

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

leftView.backgroundColor = [UIColor blueColor];

customView.leftCalloutAccessoryView = leftView;



//設置右視圖

UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

customView.rightCalloutAccessoryView = rightBtn;





return  customView;

endif

}

16) 移除大頭針

[_mapView removeAnnotations:_mapView.annotations];

17) 添加長按手勢,實如今地圖的長按點添加一個大頭針

//四、長按地圖顯示大頭針

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

[_mapView addGestureRecognizer:longPress];

pragma mark - 長按手勢

-(void)longPress:(UILongPressGestureRecognizer *)gesture

{

//避免屢次調用 只容許開始長按狀態才添加大頭針

if (gesture.state != UIGestureRecognizerStateBegan) {

    return;

}



//獲取長按地圖上的某個點

CGPoint point = [gesture locationInView:_mapView];



//把point轉換成在地圖上的座標經緯度

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



//添加長按的大頭針

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

annotation.coordinate = coordinate;

annotation.title = @"長按的大頭針";

annotation.subtitle = @"副標題";

[_mapView addAnnotation:annotation];

}

高德地圖

一、高德地圖申請Appkey流程:

a) 在瀏覽器中打開網址:http://lbs.amap.com/api/ios-sdk/guide/verify/

b) 訪問:http://lbs.amap.com/console/key/,使用高德開發者帳號登錄

c) 2.在「KEY管理」頁面點擊上方的「獲取key」按鈕,依次輸入應用名,選擇綁定的服務爲「iOS平臺SDK」,輸入Bundle Identifier(Bundle Identifier獲取方式爲Xcode->General->Identity)

<Bundle Identifier : com.qianfeng.gaodedemo >

<APIKEY : e848d391f9c4b98db0935052777f99d2 >

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

a)下載高德地圖iOS SDK

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

c)添加8個關聯庫QuartzCore, CoreLocation, SystemConfiguration, CoreTelephony, libz, OpenGLES, libstdc++6.09, Security(MAMapView)

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

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

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

三、單獨使用搜索服務包:

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

b)添加關聯庫SystemConfiguration, CoreTelephony, libz, libstdc++6.09

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

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

MAMapKit:

0) 配置高德地圖API

define APIKEY @"e848d391f9c4b98db0935052777f99d2"

[MAMapServices sharedServices].apiKey = APIKEY;

1) 初始化MAMapView

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

[self.view addSubview:_maMapView];

2) 設置代理

_maMapView.delegate = self;

3) 設置地圖類型

_maMapView.mapType = MAMapTypeStandard;

4) 容許顯示本身的位置(如使用定位功能,則必須設置爲YES)

_maMapView.showsUserLocation = YES;

5) 設置logo位置

_maMapView.logoCenter = CGPointMake(100, 100);

6) 顯示羅盤

_maMapView.showsCompass = YES;

7) 顯示交通

_maMapView.showTraffic = YES;

8) 是否支持旋轉

_maMapView.rotateEnabled = YES;

9) 是否支持拖動

_maMapView.scrollEnabled = YES;

10) 是否支持縮放

_maMapView.zoomEnabled = NO;

11) 設置地圖顯示區域

a) 設置座標

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.181297, 113.346877);

b) 設置縮放

MACoordinateSpan span = MACoordinateSpanMake(0.1, 0.1);

c) 設置區域

MACoordinateRegion region = MACoordinateRegionMake(coordinate, span);

d) 顯示區域

_maMapView.region = region;

12) 定位

#pragma mark - 定位 回調方法

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

{

NSLog(@"定位成功");



  CLLocation *location = userLocation.location;

  CLLocationCoordinate2D coordinate = location.coordinate;



  NSLog(@"個人座標位置:%f, %f", coordinate.longitude, coordinate.latitude);



  // 定位後,可設置中止定位

  // _maMapView.showsUserLocation = NO;

}

13) 添加標註(大頭針)

//添加標註

  MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];

  annotation.coordinate = coordinate; //設置標註的座標

  annotation.title = @"高德地圖標題"; //設置標題

  annotation.subtitle = @"副標題"; //設置副標題

  [_maMapView addAnnotation:annotation]; //將標註添加在地圖上

14) 標註的複用及定製

#pragma mark - 定製標註視圖(和原生地圖定製方式相似)

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

{

//不定製本身位置的標註視圖

  if ( [annotation isKindOfClass:[MAUserLocation class]]) {

      return nil;

  }

#if 1

// 一、自帶的標註視圖

  MAPinAnnotationView *pinAnnView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];

  if ( !pinAnnView ) {

      pinAnnView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"];

  }



  // 是否可彈出視圖

  pinAnnView.canShowCallout = YES;



  // 設置掉落動畫

  pinAnnView.animatesDrop = YES;



  // 設置標註顏色

  pinAnnView.pinColor = MAPinAnnotationColorGreen;



  // 設置左視圖

  UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

  leftView.backgroundColor = [UIColor blueColor];

  pinAnnView.leftCalloutAccessoryView = leftView;



  //設置右視圖

  UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

  pinAnnView.rightCalloutAccessoryView = rightBtn;



  return pinAnnView;

#else

//二、自定義標註視圖

  MAAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID2"];

  if ( !customView ) {

      customView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID2"];

  }



  //設置點擊大頭針能夠顯示氣泡

  customView.canShowCallout = YES;



  //設置大頭針圖片

  customView.image = [UIImage imageNamed:@"marker"];



  //設置左視圖

  UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

  leftView.backgroundColor = [UIColor blueColor];

  customView.leftCalloutAccessoryView = leftView;



  //設置右視圖

  UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

  customView.rightCalloutAccessoryView = rightBtn;



  return  customView;

#endif

}

15) 添加長按手勢

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

  [_maMapView addGestureRecognizer:longPress];

#pragma mark -- 長按手勢Action

-(void)longPress:(UILongPressGestureRecognizer *)longPress

{

if (longPress.state != UIGestureRecognizerStateBegan) {

      return;

  }



  //獲取點位置

  CGPoint point = [longPress locationInView:_maMapView];



  //將點位置轉換成經緯度座標

  CLLocationCoordinate2D coordinate = [_maMapView convertPoint:point toCoordinateFromView:_maMapView];



  //在該點添加一個大頭針(標註)

  MAPointAnnotation *pointAnn = [[MAPointAnnotation alloc] init];

  pointAnn.coordinate = coordinate;

  pointAnn.title = @"長按的大頭針";

  pointAnn.subtitle = @"副標題";

  [_maMapView addAnnotation:pointAnn];

}

AMapSearchKit:

1) 搜索周邊

0) 建立AMapSearchAPI對象,配置APPKEY,同時設置代理對象爲self

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

a) 建立搜索周邊請求類

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

b) 設置搜索類型(按關鍵字搜索)

searchRequest.searchType = AMapSearchType_PlaceKeyword;

c) 設置關鍵字

searchRequest.keywords = keywordsTextField.text;

d) 設置搜索城市

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

e) 開始搜索

[searchAPI AMapPlaceSearch:searchRequest];

2) 搜索周邊回調方法

a) 搜索失敗

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

{

NSLog(@"搜索失敗");

}

b) 搜索成功

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

{

//清空原來的標註(大頭針)

[_maMapView removeAnnotations:_maMapView.annotations];



//判斷是否爲空

if (response) {



    //取出搜索到的POI(POI:Point Of Interest)

    for (AMapPOI *poi in response.pois) {



        //poi的座標

        CLLocationCoordinate2D coordinate =

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

//地名

        NSString *name = poi.name;



        //地址

        NSString *address = poi.address;



        //用標註顯示

        MAPointAnnotation *pointAnn = [[MAPointAnnotation alloc] init];

        pointAnn.coordinate = coordinate;

        pointAnn.title = name;

        pointAnn.subtitle = address;

        [_maMapView addAnnotation:pointAnn];

    }

}

}

3) 添加折線

pragma mark - 畫折線

-(void)drawPolyLine

{

//初始化點

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];



// 建立數組

CLLocationCoordinate2D polyLineCoords[5];



for (int i=0; i<5; i++) {

    polyLineCoords[i].latitude = [latitudePoints[i] floatValue];

    polyLineCoords[i].longitude = [longitudePoints[i] floatValue];



}



// 建立折線對象

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;

}

相關文章
相關標籤/搜索