MKMapKit

苹果地图(MKmap)

2018-02-28  本文已影响41人  seventhboy

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

//成功回调

主线程调用

多次校准更精确

2)定位地理位置

CLLocationCoordinate2D location=CLLocationCoordinate2DMake(_latitude, _longitude);

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

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到某个宫格的数组中

给出每个宫格中点的平均位置
上一篇下一篇

猜你喜欢

热点阅读