Commit 8901206b authored by lmj_521aiau@163.com's avatar lmj_521aiau@163.com

applemap

parent 684ba8e8
......@@ -7,10 +7,12 @@
//
#import "ZJBaseMapViewController.h"
#import <MapKit/MapKit.h>
@interface ZJBaseMapViewController ()<MAMapViewDelegate, XMPopupListViewDelegate>
@interface ZJBaseMapViewController ()<MAMapViewDelegate, XMPopupListViewDelegate, MKMapViewDelegate>
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) MKMapView *mapView;
//@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) UIButton *gpsButton;
@property (nonatomic, strong) ZJMapViewPersonCardView *cardView;
......@@ -40,41 +42,32 @@
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self getFriendList];
});
if ([ZJUserInfoManager CNLanguage]) {
self.mapView.mapLanguage = @0;
[self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(0)];
}else{
self.mapView.mapLanguage = @1;
[self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(1)];
}
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:true animated:true];
if ([ZJUserInfoManager CNLanguage]) {
self.mapView.mapLanguage = @0;
[self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(0)];
}else{
self.mapView.mapLanguage = @1;
[self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(1)];
}
}
- (void)setupUI{
///初始化地图
self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
self.mapView.mapType = 0;
_mapView.showsWorldMap = @YES;
[self.mapView performSelector:@selector(setShowsWorldMap:) withObject:@(YES)];
// _mapView.showsWorldMap = @YES;
// [self.mapView performSelector:@selector(setShowsWorldMap:) withObject:@(YES)];
if ([ZJUserInfoManager CNLanguage]) {
// self.mapView.mapLanguage = @0;
// [self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(0)];
}else{
// self.mapView.mapLanguage = @1;
// [self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(1)];
}
_mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
///把地图添加至view
[self.view addSubview: _mapView];
_mapView.delegate = self;
_mapView.zoomLevel = 14;
// _mapView.zoomLevel = 14;
_mapView.showsScale = false;
_mapView.showsCompass = false;
......@@ -126,29 +119,29 @@
};
}
- (void)mapView:(UIView *)mapView regionDidChangeAnimated:(BOOL)animated {
// if(mapView.isHidden) {
// return;
//- (void)mapView:(UIView *)mapView regionDidChangeAnimated:(BOOL)animated {
//// if(mapView.isHidden) {
//// return;
//// }
////
//// if(self.isSwitching) {
//// self.isSwitching = NO;
//// return;
//// }
//
//// [self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(1)];
//// [self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(0)];
//
// if([mapView isKindOfClass:[MAMapView class]]) {
// if(!AMapDataAvailableForCoordinate(self.mapView.centerCoordinate)) {
//
// }
// } else {
// if(AMapDataAvailableForCoordinate(self.mapView.centerCoordinate)) {
//
// if(self.isSwitching) {
// self.isSwitching = NO;
// return;
// }
// [self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(1)];
// [self.mapView performSelector:NSSelectorFromString(@"setMapLanguage:") withObject:@(0)];
if([mapView isKindOfClass:[MAMapView class]]) {
if(!AMapDataAvailableForCoordinate(self.mapView.centerCoordinate)) {
}
} else {
if(AMapDataAvailableForCoordinate(self.mapView.centerCoordinate)) {
}
}
}
// }
//}
#pragma mark 好友列表
- (void)getFriendList{
......@@ -251,7 +244,7 @@
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
pointAnnotation.coordinate = CLLocationCoordinate2DMake(lat, lon);
pointAnnotation.title = @"latestLocation";
[_self.mapView addAnnotation:pointAnnotation];
[_self.mapView addAnnotation:(id<MKAnnotation>)pointAnnotation];
[_self.mapView setCenterCoordinate:pointAnnotation.coordinate animated:false];
}else{
......@@ -279,6 +272,19 @@
annotationView.enabled = NO;
return annotationView;
}
else if([annotation isKindOfClass:[MKUserLocation class]]){
NSString *pointReuseIndetifier = @"pointReuseIndetifier3";
MKAnnotationView *annotationView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:pointReuseIndetifier];
annotationView.canShowCallout = YES;
}
annotationView.image = [UIImage imageNamed:@"userPosition"];
annotationView.enabled = NO;
return (MAAnnotationView *)annotationView;
}
return nil;
}
......
......@@ -18,10 +18,12 @@
#import "AlertControllerTool.h"
#import <CoreLocation/CoreLocation.h>
#import "BaseWebViewController.h"
#import <MapKit/MapKit.h>
@interface ZJFriendTrackMapViewController () <MAMapViewDelegate>
@interface ZJFriendTrackMapViewController () <MAMapViewDelegate, MKMapViewDelegate, CAAnimationDelegate>
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) MKMapView *mapView;
//@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) UIButton *gpsButton;
@property (nonatomic, strong) ZJUserTrackTimeView *trackTimeView;
@property (nonatomic, strong) NSMutableArray<ZJLocationModel *> *locations;
......@@ -30,12 +32,18 @@
@property (strong, nonatomic) NSDate *endDate;
///全轨迹overlay
@property (strong, nonatomic) MAPolyline *commonPolyline;
@property (strong, nonatomic) MKPolyline *commonPolyline;
///车头方向跟随转动
@property (nonatomic, strong) MAAnimatedAnnotation *track;
@property (nonatomic, strong) NSArray *distanceArray;
@property (nonatomic, assign) double sumDistance;
@property (nonatomic, strong) ZJAlertView *alertView;
@property (nonatomic, strong) CAShapeLayer * shapeLayer;
@end
@implementation ZJFriendTrackMapViewController
......@@ -86,7 +94,6 @@
self.alertView.alertSubtitle.text = kLocalizedString(@"track_friend_authority_alert");
}
[self.alertView.leftButton addTapBlock:^(UITapGestureRecognizer *tap) {
NSLog(@"%@", @"leftButton");
......@@ -119,7 +126,8 @@
- (void)setupUI {
///初始化地图
self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
// self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
///把地图添加至view
[self.view addSubview: _mapView];
......@@ -128,7 +136,7 @@
_mapView.showsUserLocation = false;
_mapView.delegate = self;
_mapView.zoomLevel = 14;
// _mapView.zoomLevel = 14;
_mapView.showsScale = false;
_mapView.showsCompass = false;
......@@ -161,7 +169,9 @@
self.track = [[MAAnimatedAnnotation alloc] init];
self.track.title = @"pointReuseIndetifier1";
[self.mapView addAnnotation:self.track];
// self.track = [[MKPointAnnotation alloc] init];
// self.track.title = @"pointReuseIndetifier1";
[self.mapView addAnnotation:(id<MKAnnotation>)self.track];
}
- (void) drawLineAction {
......@@ -171,27 +181,27 @@
[self.mapView removeOverlay:self.commonPolyline];
// need get user location info
if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {
// do nothing
} else if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
//定位不能用
[AlertControllerTool alertControllerWithTitle:@"打开定位开关" message:kLocalizedString(@"alert_location_service") cancelTitle:kLocalizedString(@"alert_cancel") cancelBlock:^{
} confirm:kLocalizedString(@"alert_setting") confirmBlock:^(id s) {
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url]) {
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}else{
[[UIApplication sharedApplication] openURL:url];
}
}
} finishBlock:nil];
return;
}
// if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {
// // do nothing
// } else if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
// //定位不能用
// [AlertControllerTool alertControllerWithTitle:@"打开定位开关" message:kLocalizedString(@"alert_location_service") cancelTitle:kLocalizedString(@"alert_cancel") cancelBlock:^{
// } confirm:kLocalizedString(@"alert_setting") confirmBlock:^(id s) {
// NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
// if([[UIApplication sharedApplication] canOpenURL:url]) {
// if (@available(iOS 10.0, *)) {
// [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
// }else{
// [[UIApplication sharedApplication] openURL:url];
// }
// }
// } finishBlock:nil];
// return;
// }
MAPointAnnotation *pointAnnotation1 = [[MAPointAnnotation alloc] init];
pointAnnotation1.coordinate = CLLocationCoordinate2DMake(self.locations.firstObject.latitude, self.locations.firstObject.longitude);
pointAnnotation1.title = @"start";
[self.mapView addAnnotation:pointAnnotation1];
[self.mapView addAnnotation:(id<MKAnnotation>)pointAnnotation1];
//构造折线数据对象
CLLocationCoordinate2D commonPolylineCoords[self.locations.count];
......@@ -204,12 +214,14 @@
MAPointAnnotation *pointAnnotation2 = [[MAPointAnnotation alloc] init];
pointAnnotation2.coordinate = CLLocationCoordinate2DMake(self.locations.lastObject.latitude, self.locations.lastObject.longitude);
pointAnnotation2.title = @"end";
[self.mapView addAnnotation:pointAnnotation2];
[self.mapView addAnnotation:(id<MKAnnotation>)pointAnnotation2];
//构造折线对象
MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:self.locations.count];
// MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:self.locations.count];
// self.commonPolyline = commonPolyline;
MKPolyline *commonPolyline = [MKPolyline polylineWithCoordinates:commonPolylineCoords count:self.locations.count];
self.commonPolyline = commonPolyline;
// self.commonPolyline.boundingMapRect
//在地图上添加折线对象
[self.mapView addOverlay: commonPolyline];
......@@ -236,8 +248,26 @@
[weakSelf.trackTimeView.trackButton setEnabled:true];
[weakSelf.trackTimeView.trackButton setSelected:false];
}];
[self.mapView setVisibleMapRect:self.commonPolyline.boundingMapRect edgePadding:UIEdgeInsetsMake(250, 50, 50, 50) animated:true];
MKMapRect rect = MKMapRectMake(self.commonPolyline.boundingMapRect.origin.x, self.commonPolyline.boundingMapRect.origin.y, self.commonPolyline.boundingMapRect.size.width, self.commonPolyline.boundingMapRect.size.height);
[self.mapView setVisibleMapRect:rect edgePadding:UIEdgeInsetsMake(250, 50, 50, 50) animated:true];
//
// CGPoint *points = [self pointsForCoordinates:commonPolylineCoords count:count];
// CGPathRef path = [self pathForPoints:points count:count];
// [self initShapeLayerWithPath:path];
// self.shapeLayer.frame = self.mapView.bounds;
//
// CAAnimation *shapeLayerAnimation = [self constructShapeLayerAnimation];
// shapeLayerAnimation.delegate = self;
// shapeLayerAnimation.removedOnCompletion = NO;
// shapeLayerAnimation.fillMode = kCAFillModeForwards;
//
// [self.shapeLayer addAnimation:shapeLayerAnimation forKey:@"shape"];
//
// [self.mapView.layer addSublayer:self.shapeLayer];
}
- (ZJAlertView *)getAlertView:(NSString *)title subtitle:(NSString *)subtitle leftButtonTitle:(NSString *)leftButtonTitle rightButtonTitle:(NSString *)rightButtonTitle {
......@@ -259,29 +289,40 @@
#pragma mark - Map Delegate
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if (annotation == self.track) {
if (annotation == (id<MKAnnotation>)self.track) {
NSString *pointReuseIndetifier = @"pointReuseIndetifier1";
MAAnnotationView *annotationView = (MAAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
// MAAnnotationView *annotationView = (MAAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
// if(!annotationView) {
// annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
//
// annotationView.canShowCallout = YES;
//
// UIImage *imge = [UIImage imageNamed:@"map_track_move_icon"];
// annotationView.image = imge;
//
// }
MKAnnotationView *annotationView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
if(!annotationView) {
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
annotationView = [[MKAnnotationView alloc] initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:pointReuseIndetifier];
annotationView.canShowCallout = YES;
UIImage *imge = [UIImage imageNamed:@"map_track_move_icon"];
annotationView.image = imge;
// UIImage *imge = [UIImage imageNamed:@"map_track_move_icon"];
// annotationView.image = imge;
}
return annotationView;
} else if([annotation isKindOfClass:[MAPointAnnotation class]]) {
}
else if([annotation isKindOfClass:[MAPointAnnotation class]]) {
NSString *pointReuseIndetifier = @"pointReuseIndetifier3";
MAAnnotationView *annotationView = (MAAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
NSString *pointReuseIndetifier = @"pointReuseIndetifier2";
MKAnnotationView *annotationView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
if (annotationView == nil) {
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
annotationView = [[MKAnnotationView alloc] initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:pointReuseIndetifier];
annotationView.canShowCallout = YES;
}
......@@ -291,28 +332,71 @@
} else if ([annotation.title isEqualToString:@"end"]) {
annotationView.enabled = NO;
annotationView.image = [UIImage imageNamed:kLocalizedString(@"map_track_end_icon")];
} else {
}
// else {
// annotationView.enabled = NO;
// annotationView.image = [UIImage imageNamed:@"userPosition"];
// }
return (MKAnnotationView *)annotationView;
}
else if([annotation isKindOfClass:[MKUserLocation class]]){
NSString *pointReuseIndetifier = @"pointReuseIndetifier1";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:pointReuseIndetifier];
annotationView.canShowCallout = YES;
}
annotationView.enabled = NO;
annotationView.image = [UIImage imageNamed:@"userPosition"];
}
return annotationView;
}
// else{
//
// NSString *pointReuseIndetifier = @"pointReuseIndetifier3";
// MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
// if (annotationView == nil) {
// annotationView = [[MKAnnotationView alloc] initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:pointReuseIndetifier];
// annotationView.canShowCallout = YES;
// }
//
// if ([annotation.title isEqualToString:@"start"]) {
// annotationView.enabled = NO;
// annotationView.image = [UIImage imageNamed:kLocalizedString(@"map_track_start_icon")];
// } else if ([annotation.title isEqualToString:@"end"]) {
// annotationView.enabled = NO;
// annotationView.image = [UIImage imageNamed:kLocalizedString(@"map_track_end_icon")];
// }
// return annotationView;
// }
return nil;
}
/// 设置折线的样式
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MAPolyline class]]) {
MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:(MAPolyline *)overlay];
polylineRenderer.lineWidth = 4.f;
polylineRenderer.strokeColor = [UIColor colorWithHex:0x568EFC];
polylineRenderer.lineJoinType = kMALineJoinRound;
polylineRenderer.lineCapType = kMALineCapRound;
return polylineRenderer;
return (MKOverlayRenderer *)polylineRenderer;
}
else if([overlay isKindOfClass:[MKPolyline class]]){
MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
render.lineWidth = 4.f;
render.strokeColor = [UIColor colorWithHex:0x568EFC];
render.lineJoin = kMALineJoinRound;
render.lineCap = kMALineCapRound;
return render;
}
return nil;
}
......@@ -442,7 +526,7 @@
double lat = [response[@"result"][@"data"][@"lat"] doubleValue];
double lon = [response[@"result"][@"data"][@"lon"] doubleValue];
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init];
pointAnnotation.coordinate = CLLocationCoordinate2DMake(lat, lon);
pointAnnotation.title = @"latestLocation";
[_self.mapView addAnnotation:pointAnnotation];
......@@ -455,7 +539,56 @@
} failure:^(NSError * _Nonnull error) {
NSLog(@"失败");
}];
}
#pragma mark - 构造shapeLayer
- (void)initShapeLayerWithPath:(CGPathRef)path{
self.shapeLayer = [[CAShapeLayer alloc] init];
self.shapeLayer.strokeColor = [UIColor greenColor].CGColor;
self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
self.shapeLayer.lineJoin = kCALineCapRound;
self.shapeLayer.path = path;
}
#pragma mark - 经纬度转屏幕坐标, 调用者负责释放内存!
- (CGPoint *)pointsForCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count{
if (coordinates == NULL || count <= 1){
return NULL;
}
/* 申请屏幕坐标存储空间. */
CGPoint *points = (CGPoint *)malloc(count * sizeof(CGPoint));
/* 经纬度转换为屏幕坐标. */
for (int i = 0; i < count; i++)
{
CGPoint point = CGPointMake([self.mapView convertCoordinate:coordinates[i] toPointToView:self.mapView].x, [self.mapView convertCoordinate:coordinates[i] toPointToView:self.mapView].y);
points[i] = point;
}
return points;
}
#pragma mark - 构建path, 调用者负责释放内存!
- (CGMutablePathRef)pathForPoints:(CGPoint *)points count:(NSUInteger)count{
if (points == NULL || count <= 1){
return NULL;
}
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddLines(path, NULL, points, count);
return path;
}
#pragma mark - 构建shapeLayer的basicAnimation
- (CAAnimation *)constructShapeLayerAnimation{
CABasicAnimation *theStrokeAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
theStrokeAnimation.duration = 3;
theStrokeAnimation.fromValue = @0.f;
theStrokeAnimation.toValue = @1.f;
theStrokeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return theStrokeAnimation;
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment