iOS 自帶地圖詳解


1.配置環境:

   1>iOS9為了增強數據訪問安全,將所有的http請求都改為了https,為了能夠在iOS9中正常使用地圖SDK,請在"Info.plist"中進行如下配置,否則影響SDK的使用。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

  2>在iOS9中為了能正常調起高德地圖App的功能,必須在"Info.plist"中將高德地圖App的URL scheme列為白名單,否則無法調起,配置如下:

   <key>

     LSApplicationQueriesSchemes

   </key>

<array>
    <string>iosamap</string>
</array>

2.認識地圖:

    MKMapView *mapView =[[MKMapView alloc]init];

    mapView.frame =self.view.bounds;

    // 1.設置地圖類型

    mapView.mapType = MKMapTypeStandard;

   // 2.設置跟蹤模式(MKUserTrackingModeFollow == 跟蹤)

    self.mapView.userTrackingMode = MKUserTrackingModeFollow;這里也是定位功能實現的重要一步

   // 3.設置代理(監控地圖的相關行為:比如顯示的區域發生了改變)

    self.mapView.delegate = self;

 

3. MKMapViewDelegate

/**

 *  更新到用戶的位置時就會調用(顯示的位置、顯示范圍改變) 這里也是定位功能實現的重要一步

 *  userLocation : 大頭針模型數據, 對大頭針位置的一個封裝(這里的userLocation描述的是用來顯示用戶位置的藍色大頭針)

 */

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

    CLLocationCoordinate2D center = userLocation.location.coordinate;

    MKCoordinateSpan span = MKCoordinateSpanMake(0.2509, 0.2256);

    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);

    [self.mapView setRegion:region animated:YES];//這里是設置地圖顯示的區域(經緯度,和跨度(經度跨度和緯度跨度))

一般在這里進行請求周邊的數據,添加大頭針

}

 //  地圖顯示的區域改變了就會調用(顯示的位置、顯示范圍改變)注意:這里一般在做地圖拖動的時候顯示周邊的團購或者東西的時候會用到這個方法

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

 // 地圖顯示的區域即將改變了就會調用

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated

當用戶想返回原來的位置的時候:

-(void)backToUserLocation {

    CLLocationCoordinate2D center =  self.mapView.userLocation.location.coordinate;

    [self.mapView setCenterCoordinate:center animated:YES];

}

4.地理編碼和返地理編碼 

@property (nonatomic, strong) CLGeocoder *geocoder;

- (CLGeocoder *)geocoder

{

  if (!_geocoder) {

        self.geocoder = [[CLGeocoder alloc] init];

    }

    return _geocoder;

}

   //  地理編碼:地名 -> 經緯度

 - (void)geocode

{

    // 1.獲得輸入的地址

    NSString *address = self.addressField.text;

    if (address.length == 0) return;

    // 2.開始編碼

    [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

        if (error || placemarks.count == 0)    return;

        // 編碼成功(找到了具體的位置信息)

            // 顯示最前面的地標信息

            CLPlacemark *firstPlacemark = [placemarks firstObject];//這里是地標(包括經度和緯度還有名字等等信息,這樣子就可以進行添加大頭針的操作了)

           CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;

            CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;

            self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", latitude];

            self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f", longitude];

    }]; 

}

         //  反地理編碼:經緯度 -> 地名

- (void)reverseGeocode

{

      // 開始反向編碼

    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longtitude];

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

        if (error || placemarks.count == 0) return;

                  // 顯示最前面的地標信息

            CLPlacemark *firstPlacemark = [placemarks firstObject];

            CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;

            CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;

            self.latitudeField.text = [NSString stringWithFormat:@"%.2f", latitude];

            self.longtitudeField.text = [NSString stringWithFormat:@"%.2f", longitude];

    }];

}

 5.添加大頭針(實現MKAnnotation)協議

    也可以自定義大頭針

     

 HYAnnotation *anno1 = [[HYAnnotation alloc] init];

 anno1.coordinate = CLLocationCoordinate2DMake(39, 119);

 anno1.title = @"北京";

 anno1.subtitle = @"中國牛逼的地方";

 // 添加一個大頭針模型(模型:描述大頭針的信息)

[self.mapView addAnnotation:anno1];

6.畫線(需要開始的位置和重點的位置,可以在進行地理編碼時得到)

- (void)drawLineWithSourceCLPm:(CLPlacemark *)sourceCLPm destinationCLPm:(CLPlacemark *)destinationCLPm

{

    if (sourceCLPm == nil || destinationCLPm == nil) return;

      // 1.初始化方向請求

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

    // 設置起點

    MKPlacemark *sourceMKPm = [[MKPlacemark alloc] initWithPlacemark:sourceCLPm];

    request.source = [[MKMapItem alloc] initWithPlacemark:sourceMKPm];

    self.sourceMKPm = sourceMKPm;

    // 設置終點

    MKPlacemark *destinationMKPm = [[MKPlacemark alloc] initWithPlacemark:destinationCLPm];

    request.destination = [[MKMapItem alloc] initWithPlacemark:destinationMKPm];

    self.destinationMKPm = destinationMKPm;

    // 2.根據請求創建方向

    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

    // 3.執行請求

    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

        if (error) return;

        for (MKRoute *route in response.routes) {

            // 添加路線遮蓋(傳遞路線的遮蓋模型數據)

            [self.mapView addOverlay:route.polyline];

        }

    }];

    // 遮蓋 overlay

}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay

{

    MKPolylineRenderer *redender = [[MKPolylineRenderer alloc] initWithOverlay:overlay];

    redender.lineWidth = 5;

    redender.strokeColor = [UIColor blueColor];

    return redender;

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM