美文网首页MKMapKit
苹果地图(MKmap)

苹果地图(MKmap)

作者: seventhboy | 来源:发表于2018-02-28 18:15 被阅读41次

    1.基本调用

    导入mapkit framework

    import <mapkit/mapkit.h>

    new MKMapView *_mapview

    xib mapview -> mapview

    2.定位

    1. 获取地理位置

    if([CLLocationManager locationServicesEnabled]){
    //定位功能开启的情况下进行定位
    _manager = [[CLLocationManager alloc] init];
    _manager.distanceFilter = kCLDistanceFilterNone;//距离筛选器,单位米(移动多少米才回调更新)
    _manager.desiredAccuracy = kCLLocationAccuracyBest; //精确度
    [_manager setDelegate:self];
    [_manager startUpdatingLocation];
    }

    pragma mark – CLLocationManagerDelegate

    //成功回调

    • (void)locationManager:(CLLocationManager *)manager
      didUpdateToLocation:(CLLocation *)newLocation
      fromLocation:(CLLocation *)oldLocation
      //失败回调
    • (void)locationManager:(CLLocationManager *)manager
      didFailWithError:(NSError *)error
      注意:

    主线程调用

    多次校准更精确

    2)定位地理位置

    CLLocationCoordinate2D location=CLLocationCoordinate2DMake(_latitude, _longitude);

    MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(location, DEFAULTSPAN ,DEFAULTSPAN );
    MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:region];
    [_mapView setRegion:adjustedRegion animated:YES];
    3)获得附近的信息

    • (void)fetchNearbyInfo
      {
      CLLocationDegrees latitude=116.13554;
      CLLocationDegrees longitude=38.413546;
      CLLocationCoordinate2D location=CLLocationCoordinate2DMake(latitude, longitude);

      MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(location, DEFAULTSPAN ,DEFAULTSPAN );

      MKLocalSearchRequest *requst = [[MKLocalSearchRequest alloc] init];
      requst.region = region;
      requst.naturalLanguageQuery = @"place"; //想要的信息
      MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:requst];

      [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
      if (!error)
      {
      [_nearbyInfoArray addObjectsFromArray:response.mapItems];
      //
      }
      else
      {
      //
      }
      }];

      1. naturalLanguageQuery 大家可以看到

      requst.naturalLanguageQuery = @"place"; //想要的信息
      这一句很重要,因为他要是一个NSString类型的字符串,但是往往不知道应该赋值什么,其实我试过了,只要是你能想到的英语单词(地面)几乎都能显示出来,如:

      医院:hospital

      旅馆:hotel

      小区:village

      学校:school

      这些都可以获得你想要的信息,当然你如果想要全部类型的可以使用place(我试了很多,发现这个最合适)

      2.苹果提供的这个接口,一次只会返回固定的10个数组,如果你有需求要获得多个的话,建议使用不同naturalLanguageQuery获得多次请求后将其拼组在一起(有什么好的办法,也求大神们评论告诉我)

      3.除了苹果地图提供这个接口外还有其他的地图也能获得,可以使用http协议获得,如:

      google接口:

    https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=%f,%f&language=en&radius=1000&sensor=false&key=%@

    百度接口:
    

    http://api.map.baidu.com/place/v2/search?&query=%@&location=%f,%f&radius=2000&output=json&ak=%@

    具体使用方法,请参考官方文档,这里我就不介绍了。
    
    3.打点
    
    1.基本点
    
    void)addAnnotation:(id <MKAnnotation>)annotation;
    - (void)addAnnotations:(NSArray *)annotations;
    @protocol MKAnnotation <NSObject>
    
    // Center latitude and longitude of the annotion view.
    // The implementation of this property must be KVO compliant.
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    
    @optional
    
    // Title and subtitle for use by selection UI.
    @property (nonatomic, readonly, copy) NSString *title;
    @property (nonatomic, readonly, copy) NSString *subtitle;
    
    // Called as a result of dragging an annotation view.
    - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate NS_AVAILABLE(10_9, 4_0);
    
    @end
    
    头文件定义
    @interface CalloutMapAnnotation : NSObject <MKAnnotation>
    
    @property (nonatomic) CLLocationDegrees latitude;
    @property (nonatomic) CLLocationDegrees longitude;
    @property(nonatomic, copy) NSString *title;
    @property(nonatomic, copy) NSString *subtitle;
    
    @end
    2.冒泡
    
    (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
        if ([view.annotation isKindOfClass:[REVClusterPin class]]) {
            if (_calloutAnnotation && _calloutAnnotation.coordinate.latitude == view.annotation.coordinate.latitude&&
                _calloutAnnotation.coordinate.longitude == view.annotation.coordinate.longitude) {
                return;
            }
            if (_calloutAnnotation) {
                [mapView removeAnnotation:_calloutAnnotation];
                _calloutAnnotation = nil;
            }
            _calloutAnnotation = [[CalloutMapAnnotation alloc]
                                  initWithLatitude:view.annotation.coordinate.latitude
                                  andLongitude:view.annotation.coordinate.longitude];
    
            _calloutAnnotation.title = view.annotation.title;
            [mapView addAnnotation:_calloutAnnotation];
        }}
    
    - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
        if (_calloutAnnotation && ![view isKindOfClass:[CallOutAnnotationView class]] && !_isShowLevelMood) {
            if (_calloutAnnotation.coordinate.latitude == view.annotation.coordinate.latitude&&
                _calloutAnnotation.coordinate.longitude == view.annotation.coordinate.longitude) {
    
                CalloutMapAnnotation *oldAnnotation = _calloutAnnotation;
                _calloutAnnotation = nil;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [mapView removeAnnotation:oldAnnotation];
                });
            }
        }
    }
    
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    
        if ([annotation isKindOfClass:[CalloutMapAnnotation class]]) {
    
            CallOutAnnotationView *annotationView = (CallOutAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutView"];
            if (!annotationView) {
                annotationView = [[CallOutAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CalloutView"];
                JingDianMapCell  *cell = [[[NSBundle mainBundle] loadNibNamed:@"JingDianMapCell" owner:self options:nil] objectAtIndex:0];
                [annotationView.contentView addSubview:cell];
                
            }
        }
    }
    注:轮播冒泡只需要遍历点逐个select就行
    
    4.聚合
    
    聚合使用了第三方库REVClusterMap,原理:
    
    在- (void) mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated中,进行计算:
    
    排除屏幕外的点
    
    画出4*4的宫格块
    
    遍历所有点,将点add到某个宫格的数组中
    
    给出每个宫格中点的平均位置

    相关文章

      网友评论

        本文标题:苹果地图(MKmap)

        本文链接:https://www.haomeiwen.com/subject/csuvpttx.html