// // AmapViewFactory.m // amap_location // // Created by i2edu on 2020/4/23. // #import "FlutterAmapView.h" #import "CustomCalloutView.h" #import "CustomAnnotation.h" static NSString* viewType = @"com.i2edu.mapView"; @implementation FlutterAmapViewFactory{ NSObject* _messenger; FlutterAmapView* amapView; } - (nonnull NSObject *)createWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId arguments:(id _Nullable)args { FlutterAmapView* amapView = [[FlutterAmapView alloc]initWithFrame:frame viewindentifier:viewId arguments:args binaryMessenger:_messenger]; return amapView; } - (NSObject *)createArgsCodec{ return [FlutterStandardMessageCodec sharedInstance]; } - (instancetype)initWithMessenger:(NSObject *)messenger{ self = [super init]; if (self) { _messenger=messenger; } return self; } @end @interface FlutterAmapView() @property(nonatomic ,strong) MAMapView* mapView; @property(nonatomic, strong)FlutterMethodChannel* channel; @end @implementation FlutterAmapView{ } - (instancetype)initWithFrame:(CGRect)frame viewindentifier:(int64_t)viewId arguments:(id)args binaryMessenger:(NSObject *)messenger{ if(self = [super init]){ NSString * channelName=[NSString stringWithFormat:@"com.i2edu.mapView/map_view_%lld",viewId]; _channel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:messenger]; __weak __typeof__(self) weakSelf = self; [weakSelf.channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) { [weakSelf onMethodCall:call result:result]; }]; _mapView = [[MAMapView alloc] initWithFrame:frame]; _mapView.backgroundColor = [UIColor clearColor]; _mapView.frame = frame; [_mapView setZoomLevel:17.5 animated:YES]; [_mapView updateUserLocationRepresentation:[self getRepresentation]]; _mapView.delegate=self; ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码 _mapView.showsUserLocation = YES; _mapView.userTrackingMode = MAUserTrackingModeFollow; } return self; } -(MAUserLocationRepresentation*)getRepresentation{ MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init]; r.image=[UIImage imageNamed:@"dog"]; r.image=[self scaleToSize: r.image size:CGSizeMake(50, 60)]; // r.image=[UIImage imageWithContentsOfFile:@"Assets/Images/dog.png"]; // r.locationDotFillColor = [UIColor redColor]; return r; } - (UIImage *)scaleToSize:(UIImage *)image size:(CGSize)size { // 创建一个bitmap的context // 并把它设置成为当前正在使用的context // Determine whether the screen is retina /* if([[UIScreen mainScreen] scale] == 2.0) { UIGraphicsBeginImageContextWithOptions(size, NO, 2.0); } else { UIGraphicsBeginImageContext(size); } */ UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]); // 绘制改变大小的图片 [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; // 从当前context中创建一个改变大小后的图片 UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext(); // 使当前的context出堆栈 UIGraphicsEndImageContext(); // 返回新的改变大小后的图片 return scaledImage; } - (UIView *)view{ return _mapView; } -(void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { if ([call.method isEqualToString:@"setMarkers"]) { NSArray * markers = [call.arguments objectForKey:@"markers"]; for (NSDictionary* marker in markers) { CustomAnnotation *pointAnnotation = [[CustomAnnotation alloc] init]; NSNumber* lat = marker[@"lat"]; NSNumber* lon = marker[@"lon"]; CLLocationDegrees latitude = lat.doubleValue; CLLocationDegrees longitude = lon.doubleValue; pointAnnotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude); pointAnnotation.phoneNumber = marker[@"tel"]; pointAnnotation.map = marker; pointAnnotation.title = marker[@"title"]; pointAnnotation.subtitle = marker[@"content"]; [_mapView addAnnotation:pointAnnotation]; } result(nil); }else if([call.method isEqualToString:@"onCreate"]){ result(nil); }else if([call.method isEqualToString:@"onPause"]){ result(nil); }else if([call.method isEqualToString:@"onResume"]){ result(nil); }else if([call.method isEqualToString:@"moveCamera"]){ NSDictionary *data = [call.arguments objectForKey:@"data"]; NSNumber *x = data[@"x"]; NSNumber *y = data[@"y"]; _mapView.centerCoordinate=CLLocationCoordinate2DMake(x.doubleValue, y.doubleValue); result(nil); }else { result(FlutterMethodNotImplemented); } } - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id )annotation { if ([annotation isKindOfClass:[CustomAnnotation class]]) { static NSString *pointReuseIndentifier = @"pointReuseIndentifier"; CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier]; if (annotationView == nil) { annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier]; } annotationView.channel = self.channel; annotationView.image = [UIImage imageNamed:@"i2"]; annotationView.image=[self scaleToSize: annotationView.image size:CGSizeMake(50, 60)]; // annotationView.pinColor = MAPinAnnotationColorPurple; annotationView.canShowCallout= NO; //设置气泡可以弹出,默认为NO // annotationView.animatesDrop = YES; //设置标注动画显示,默认为NO // annotationView.draggable = YES; //设置标注可以拖动,默认为NO return annotationView; } return nil; } @end