initial
This commit is contained in:
+154
@@ -0,0 +1,154 @@
|
||||
//
|
||||
// DDMAMapView.h
|
||||
// DDMAMapKit_Private
|
||||
// Created by DDIsFriend on 2023/1/30.
|
||||
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <AMapNaviKit/MAMapKit.h>
|
||||
#import <DDMAMapKit_Private/MATraceReplayOverlay.h>
|
||||
#import <DDMAMapKit_Private/MATraceReplayOverlayRender.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol DDMAMapViewDelegate;
|
||||
@class DDAnnotationView;
|
||||
@class DDAnnotation;
|
||||
|
||||
@interface DDMAMapView : UIView
|
||||
@property (nonatomic, strong, nonnull)MAMapView *maMapView;
|
||||
|
||||
@property (nonatomic, weak)id<DDMAMapViewDelegate> delegate;
|
||||
|
||||
// 请在使用该类前先设置appkey
|
||||
+ (void)startWithAppKey:(nullable NSString *)appKey;
|
||||
|
||||
// 在创建该类实例前调用
|
||||
+ (void)agreePrivacy;
|
||||
|
||||
- (MAOverlayRenderer *)dd_rendererForOverlay:(id <MAOverlay>)overlay;
|
||||
@end
|
||||
|
||||
@interface DDMAMapView (Location)
|
||||
|
||||
/// @brief 用户当前位置的属性
|
||||
@property (nonatomic, readonly)MAUserLocation *maUserLocation;
|
||||
|
||||
/// @brief 调用此方法来自定义当前位置小蓝点
|
||||
- (void)updateLocationViewWithParam:(MAUserLocationRepresentation *)representation;
|
||||
|
||||
@end
|
||||
|
||||
@interface DDMAMapView (Trace)
|
||||
|
||||
@property (nonatomic, strong)MATraceReplayOverlay *traceReplayOverlay;
|
||||
|
||||
@property (nonatomic, strong)MATraceReplayOverlayRenderer *traceReplayOverlayRenderer;
|
||||
|
||||
@end
|
||||
|
||||
@protocol DDMAMapViewDelegate <NSObject>
|
||||
|
||||
@optional
|
||||
|
||||
/**
|
||||
* @brief 地图开始加载
|
||||
* @param mapView 地图View
|
||||
*/
|
||||
- (void)dd_mapViewWillStartLoadingMap:(MAMapView *)mapView;
|
||||
|
||||
/**
|
||||
* @brief 地图加载成功
|
||||
* @param mapView 地图View
|
||||
*/
|
||||
- (void)dd_mapViewDidFinishLoadingMap:(MAMapView *)mapView;
|
||||
|
||||
/**
|
||||
* @brief 地图加载失败
|
||||
* @param mapView 地图View
|
||||
* @param error 错误信息
|
||||
*/
|
||||
- (void)dd_mapViewDidFailLoadingMap:(MAMapView *)mapView withError:(NSError *)error;
|
||||
|
||||
/**
|
||||
* @brief 地形图加载失败
|
||||
* @param mapView 地图View
|
||||
* @param error 错误信息
|
||||
*/
|
||||
- (void)dd_mapView:(MAMapView *)mapView didFailLoadTerrainWithError:(NSError *)error;
|
||||
|
||||
/**
|
||||
* @brief 在地图View将要启动定位时,会调用此函数
|
||||
* @param mapView 地图View
|
||||
*/
|
||||
- (void)dd_mapViewWillStartLocatingUser:(MAMapView *)mapView;
|
||||
|
||||
/**
|
||||
* @brief 在地图View停止定位后,会调用此函数
|
||||
* @param mapView 地图View
|
||||
*/
|
||||
- (void)dd_mapViewDidStopLocatingUser:(MAMapView *)mapView;
|
||||
|
||||
/**
|
||||
* @brief 位置或者设备方向更新后,会调用此函数
|
||||
* @param mapView 地图View
|
||||
* @param userLocation 用户定位信息(包括位置与设备方向等数据)
|
||||
* @param updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
|
||||
*/
|
||||
- (void)dd_mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation;
|
||||
|
||||
/**
|
||||
* @brief 当plist配置NSLocationAlwaysUsageDescription或者NSLocationAlwaysAndWhenInUseUsageDescription,并且[CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined,会调用代理的此方法。
|
||||
此方法实现调用后台权限API即可( 该回调必须实现 [locationManager requestAlwaysAuthorization] ); since 6.8.0
|
||||
* @param locationManager 地图的CLLocationManager。
|
||||
*/
|
||||
- (void)dd_mapViewRequireLocationAuth:(CLLocationManager *)locationManager;
|
||||
|
||||
/**
|
||||
* @brief 定位失败后,会调用此函数
|
||||
* @param mapView 地图View
|
||||
* @param error 错误号,参考CLError.h中定义的错误号
|
||||
*/
|
||||
- (void)dd_mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error;
|
||||
|
||||
/**
|
||||
* @brief 根据anntation生成对应的View。
|
||||
|
||||
注意:
|
||||
1、5.1.0后由于定位蓝点增加了平滑移动功能,如果在开启定位的情况先添加annotation,需要在此回调方法中判断annotation是否为MAUserLocation,从而返回正确的View。
|
||||
if ([annotation isKindOfClass:[MAUserLocation class]]) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
2、请不要在此回调中对annotation进行select和deselect操作,此时annotationView还未添加到mapview。
|
||||
|
||||
* @param mapView 地图View
|
||||
* @param annotation 指定的标注
|
||||
* @return 生成的标注View
|
||||
*/
|
||||
- (MAAnnotationView *)dd_mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation;
|
||||
|
||||
/**
|
||||
* @brief 根据overlay生成对应的Renderer
|
||||
* @param mapView 地图View
|
||||
* @param overlay 指定的overlay
|
||||
* @return 生成的覆盖物Renderer
|
||||
*/
|
||||
- (MAOverlayRenderer *)dd_mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay;
|
||||
|
||||
/**
|
||||
* @brief 标注view被点击时,触发该回调。(since 5.7.0)
|
||||
* @param mapView 地图的view
|
||||
* @param view annotationView
|
||||
*/
|
||||
- (void)dd_mapView:(MAMapView *)mapView didAnnotationViewTapped:(MAAnnotationView *)view;
|
||||
|
||||
/**
|
||||
* @brief 当mapView新添加overlay renderers时,调用此接口
|
||||
* @param mapView 地图View
|
||||
* @param overlayRenderers 新添加的overlay renderers
|
||||
*/
|
||||
- (void)dd_mapView:(MAMapView *)mapView didAddOverlayRenderers:(NSArray *)overlayRenderers;
|
||||
|
||||
@end
|
||||
NS_ASSUME_NONNULL_END
|
||||
+245
@@ -0,0 +1,245 @@
|
||||
//
|
||||
// DDMAMapView.m
|
||||
// DDMAMapKit_Private
|
||||
// Created by DDIsFriend on 2023/1/30.
|
||||
|
||||
|
||||
#import "DDMAMapView.h"
|
||||
#import <objc/runtime.h>
|
||||
#import <DDCategoryKit_Private/UIImage+DDCategory.h>
|
||||
#import <DDMAMapKit_Private/MAPointAnnotation+DDCategory.h>
|
||||
#import <DDMAMapKit_Private/MABaseOverlay+DDCategory.h>
|
||||
#import <DDMAMapKit_Private/DriveRouteCustomAnnotationView.h>
|
||||
#import <DDMAMapKit_Private/DriveRouteCustomAnnotation.h>
|
||||
|
||||
@interface DDMAMapView () <MAMapViewDelegate>
|
||||
|
||||
@end
|
||||
|
||||
@implementation DDMAMapView
|
||||
// MARK: <Class Initialize>
|
||||
+ (void)startWithAppKey:(nullable NSString *)appKey{
|
||||
[[AMapServices sharedServices] setEnableHTTPS:YES];
|
||||
[AMapServices sharedServices].apiKey = appKey;
|
||||
}
|
||||
|
||||
+ (void)agreePrivacy{
|
||||
[MAMapView updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain];
|
||||
[MAMapView updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree];
|
||||
}
|
||||
|
||||
// MARK: <Init Function>
|
||||
- (instancetype)initWithFrame:(CGRect)frame{
|
||||
self = [super initWithFrame:frame];
|
||||
if (!self) return nil;
|
||||
|
||||
[self configMapView];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)configMapView{
|
||||
// MAMapView.metalEnabled = YES;
|
||||
self.maMapView = [[MAMapView alloc] init];
|
||||
self.maMapView.delegate = self;
|
||||
[self addSubview:self.maMapView];
|
||||
|
||||
self.maMapView.translatesAutoresizingMaskIntoConstraints = NO;
|
||||
[NSLayoutConstraint activateConstraints:@[
|
||||
[self.maMapView.leftAnchor constraintEqualToAnchor:self.leftAnchor],
|
||||
[self.maMapView.topAnchor constraintEqualToAnchor:self.topAnchor],
|
||||
[self.maMapView.rightAnchor constraintEqualToAnchor:self.rightAnchor],
|
||||
[self.maMapView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
|
||||
]];
|
||||
|
||||
self.maMapView.showsIndoorMap = NO;
|
||||
self.maMapView.zoomLevel = 16;
|
||||
self.maMapView.mapType = MAMapTypeStandard;
|
||||
|
||||
self.maMapView.showsCompass = NO;
|
||||
self.maMapView.showsScale = NO;
|
||||
}
|
||||
|
||||
- (MAOverlayRenderer *)dd_rendererForOverlay:(id <MAOverlay>)overlay{
|
||||
return [self.maMapView rendererForOverlay:overlay];
|
||||
}
|
||||
|
||||
// MARK: <MAMapViewDelegate>
|
||||
- (void)mapViewWillStartLoadingMap:(MAMapView *)mapView{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapViewWillStartLoadingMap:)]) {
|
||||
[self.delegate dd_mapViewWillStartLoadingMap:mapView];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mapViewDidFinishLoadingMap:(MAMapView *)mapView{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapViewDidFinishLoadingMap:)]) {
|
||||
[self.delegate dd_mapViewDidFinishLoadingMap:mapView];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mapViewDidFailLoadingMap:(MAMapView *)mapView withError:(NSError *)error{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapViewDidFailLoadingMap:withError:)]) {
|
||||
[self.delegate dd_mapViewDidFailLoadingMap:mapView withError:error];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mapView:(MAMapView *)mapView didFailLoadTerrainWithError:(NSError *)error{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapView:didFailLoadTerrainWithError:)]) {
|
||||
[self.delegate dd_mapView:mapView didFailLoadTerrainWithError:error];
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: <Locate>
|
||||
- (void)mapViewWillStartLocatingUser:(MAMapView *)mapView{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapViewWillStartLocatingUser:)]) {
|
||||
[self.delegate dd_mapViewWillStartLocatingUser:mapView];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mapViewDidStopLocatingUser:(MAMapView *)mapView{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapViewDidStopLocatingUser:)]) {
|
||||
[self.delegate dd_mapViewDidStopLocatingUser:mapView];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapView:didUpdateUserLocation:updatingLocation:)]) {
|
||||
[self.delegate dd_mapView:mapView didUpdateUserLocation:userLocation updatingLocation:updatingLocation];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mapViewRequireLocationAuth:(CLLocationManager *)locationManager {
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapViewRequireLocationAuth:)]) {
|
||||
[self.delegate dd_mapViewRequireLocationAuth:locationManager];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapView:didFailToLocateUserWithError:)]) {
|
||||
[self.delegate dd_mapView:mapView didFailToLocateUserWithError:error];
|
||||
}
|
||||
}
|
||||
|
||||
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapView:viewForAnnotation:)]) {
|
||||
return [self.delegate dd_mapView:mapView viewForAnnotation:annotation];
|
||||
}
|
||||
|
||||
if ([annotation isMemberOfClass:[MAPointAnnotation class]]){
|
||||
MAPointAnnotation *pointAnnotation = (MAPointAnnotation *)annotation;
|
||||
static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
|
||||
MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
|
||||
if (annotationView == nil)
|
||||
{
|
||||
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
|
||||
}
|
||||
annotationView.annotation = pointAnnotation;
|
||||
annotationView.image = pointAnnotation.dd_image;
|
||||
if (pointAnnotation.customCalloutView != nil) {
|
||||
annotationView.customCalloutView = [[MACustomCalloutView alloc] initWithCustomView:pointAnnotation.customCalloutView];
|
||||
annotationView.canShowCallout = YES;
|
||||
annotationView.selected = YES;
|
||||
}
|
||||
return annotationView;
|
||||
}
|
||||
|
||||
if ([annotation isMemberOfClass:[MAAnimatedAnnotation class]]) {
|
||||
MAAnimatedAnnotation *animatedAnnotation = (MAAnimatedAnnotation *)annotation;
|
||||
static NSString *pointReuseIndentifier = @"AnimatedAnnotationPointReuseIndentifier";
|
||||
MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
|
||||
if (annotationView == nil)
|
||||
{
|
||||
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
|
||||
}
|
||||
annotationView.annotation = animatedAnnotation;
|
||||
annotationView.image = animatedAnnotation.dd_image;
|
||||
return annotationView;
|
||||
}
|
||||
|
||||
if ([annotation isMemberOfClass:[DriveRouteCustomAnnotation class]]){
|
||||
DriveRouteCustomAnnotation *pointAnnotation = (DriveRouteCustomAnnotation *)annotation;
|
||||
static NSString *pointReuseIndentifier = @"DriveRouteCustomAnnotationPointReuseIndentifier";
|
||||
DriveRouteCustomAnnotationView *annotationView = (DriveRouteCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
|
||||
if (annotationView == nil)
|
||||
{
|
||||
annotationView = [[DriveRouteCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
|
||||
}
|
||||
annotationView.annotation = pointAnnotation;
|
||||
annotationView.image = pointAnnotation.dd_image;
|
||||
annotationView.customTextLabel.attributedText = pointAnnotation.customAttributedText;
|
||||
return annotationView;
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapView:rendererForOverlay:)]) {
|
||||
return [self.delegate dd_mapView:mapView rendererForOverlay:overlay];
|
||||
}
|
||||
|
||||
if ([overlay isMemberOfClass:[MAPolyline class]]){
|
||||
MAPolyline *polyline = (MAPolyline *)overlay;
|
||||
MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:polyline];
|
||||
polylineRenderer.lineWidth = polyline.dd_lineWidth;
|
||||
polylineRenderer.strokeColor = polyline.dd_strokeColor;
|
||||
polylineRenderer.lineJoinType = polyline.dd_lineJoinType;
|
||||
polylineRenderer.lineCapType = polyline.dd_lineCapType;
|
||||
return polylineRenderer;
|
||||
}
|
||||
|
||||
if ([overlay isMemberOfClass:[MATraceReplayOverlay class]]) {
|
||||
MATraceReplayOverlay *traceReplayOverlay = (MATraceReplayOverlay *)overlay;
|
||||
MATraceReplayOverlayRenderer *traceReplayOverlayRenderer = [[MATraceReplayOverlayRenderer alloc] initWithOverlay:traceReplayOverlay];
|
||||
traceReplayOverlayRenderer.lineWidth = traceReplayOverlay.dd_lineWidth;
|
||||
traceReplayOverlayRenderer.strokeColors = traceReplayOverlay.dd_strokeColors;
|
||||
traceReplayOverlayRenderer.lineJoinType = traceReplayOverlay.dd_lineJoinType;
|
||||
traceReplayOverlayRenderer.lineCapType = traceReplayOverlay.dd_lineCapType;
|
||||
self.traceReplayOverlay = traceReplayOverlay;
|
||||
self.traceReplayOverlayRenderer = traceReplayOverlayRenderer;
|
||||
return traceReplayOverlayRenderer;
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)mapView:(MAMapView *)mapView didAnnotationViewTapped:(MAAnnotationView *)view{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapView:didAnnotationViewTapped:)]) {
|
||||
[self.delegate dd_mapView:mapView didAnnotationViewTapped:view];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mapView:(MAMapView *)mapView didAddOverlayRenderers:(NSArray *)overlayRenderers{
|
||||
if ([self.delegate respondsToSelector:@selector(dd_mapView:didAddOverlayRenderers:)]) {
|
||||
[self.delegate dd_mapView:mapView didAddOverlayRenderers:overlayRenderers];
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation DDMAMapView (Location)
|
||||
- (MAUserLocation *)maUserLocation{
|
||||
return self.maMapView.userLocation;
|
||||
}
|
||||
|
||||
- (void)updateLocationViewWithParam:(MAUserLocationRepresentation *)representation{
|
||||
[self.maMapView updateUserLocationRepresentation:representation];
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation DDMAMapView (Trace)
|
||||
- (void)setTraceReplayOverlay:(MATraceReplayOverlay *)traceReplayOverlay{
|
||||
objc_setAssociatedObject(self, @selector(traceReplayOverlay), traceReplayOverlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
- (MATraceReplayOverlay *)traceReplayOverlay{
|
||||
return objc_getAssociatedObject(self, _cmd);
|
||||
}
|
||||
|
||||
- (void)setTraceReplayOverlayRenderer:(MATraceReplayOverlayRenderer *)traceReplayOverlayRenderer{
|
||||
objc_setAssociatedObject(self, @selector(traceReplayOverlayRenderer), traceReplayOverlayRenderer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
- (MATraceReplayOverlayRenderer *)traceReplayOverlayRenderer{
|
||||
return objc_getAssociatedObject(self, _cmd);
|
||||
}
|
||||
@end
|
||||
Reference in New Issue
Block a user