FlutterAmapView.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // AmapViewFactory.m
  3. // amap_location
  4. //
  5. // Created by i2edu on 2020/4/23.
  6. //
  7. #import "FlutterAmapView.h"
  8. #import "CustomCalloutView.h"
  9. #import "CustomAnnotation.h"
  10. static NSString* viewType = @"com.i2edu.mapView";
  11. @implementation FlutterAmapViewFactory{
  12. NSObject<FlutterBinaryMessenger>* _messenger;
  13. FlutterAmapView* amapView;
  14. }
  15. - (nonnull NSObject<FlutterPlatformView> *)createWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId arguments:(id _Nullable)args {
  16. FlutterAmapView* amapView = [[FlutterAmapView alloc]initWithFrame:frame viewindentifier:viewId arguments:args binaryMessenger:_messenger];
  17. return amapView;
  18. }
  19. - (NSObject<FlutterMessageCodec> *)createArgsCodec{
  20. return [FlutterStandardMessageCodec sharedInstance];
  21. }
  22. - (instancetype)initWithMessenger:(NSObject<FlutterBinaryMessenger> *)messenger{
  23. self = [super init];
  24. if (self) {
  25. _messenger=messenger;
  26. }
  27. return self;
  28. }
  29. @end
  30. @interface FlutterAmapView()<MAMapViewDelegate>
  31. @property(nonatomic ,strong) MAMapView* mapView;
  32. @property(nonatomic, strong)FlutterMethodChannel* channel;
  33. @end
  34. @implementation FlutterAmapView{
  35. }
  36. - (instancetype)initWithFrame:(CGRect)frame viewindentifier:(int64_t)viewId arguments:(id)args binaryMessenger:(NSObject<FlutterBinaryMessenger> *)messenger{
  37. if(self = [super init]){
  38. NSString * channelName=[NSString stringWithFormat:@"com.i2edu.mapView/map_view_%lld",viewId];
  39. _channel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:messenger];
  40. __weak __typeof__(self) weakSelf = self;
  41. [weakSelf.channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) {
  42. [weakSelf onMethodCall:call result:result];
  43. }];
  44. _mapView = [[MAMapView alloc] initWithFrame:frame];
  45. _mapView.backgroundColor = [UIColor clearColor];
  46. _mapView.frame = frame;
  47. [_mapView setZoomLevel:17.5 animated:YES];
  48. [_mapView updateUserLocationRepresentation:[self getRepresentation]];
  49. _mapView.delegate=self;
  50. ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
  51. _mapView.showsUserLocation = YES;
  52. _mapView.userTrackingMode = MAUserTrackingModeFollow;
  53. }
  54. return self;
  55. }
  56. -(MAUserLocationRepresentation*)getRepresentation{
  57. MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
  58. r.image=[UIImage imageNamed:@"dog"];
  59. r.image=[self scaleToSize: r.image size:CGSizeMake(50, 60)];
  60. // r.image=[UIImage imageWithContentsOfFile:@"Assets/Images/dog.png"];
  61. // r.locationDotFillColor = [UIColor redColor];
  62. return r;
  63. }
  64. - (UIImage *)scaleToSize:(UIImage *)image size:(CGSize)size {
  65. // 创建一个bitmap的context
  66. // 并把它设置成为当前正在使用的context
  67. // Determine whether the screen is retina
  68. /*
  69. if([[UIScreen mainScreen] scale] == 2.0) {
  70. UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
  71. } else {
  72. UIGraphicsBeginImageContext(size);
  73. }
  74. */
  75. UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
  76. // 绘制改变大小的图片
  77. [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  78. // 从当前context中创建一个改变大小后的图片
  79. UIImage * scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  80. // 使当前的context出堆栈
  81. UIGraphicsEndImageContext();
  82. // 返回新的改变大小后的图片
  83. return scaledImage;
  84. }
  85. - (UIView *)view{
  86. return _mapView;
  87. }
  88. -(void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  89. if ([call.method isEqualToString:@"setMarkers"]) {
  90. NSArray<NSDictionary*> * markers = [call.arguments objectForKey:@"markers"];
  91. for (NSDictionary* marker in markers) {
  92. CustomAnnotation *pointAnnotation = [[CustomAnnotation alloc] init];
  93. NSNumber* lat = marker[@"lat"];
  94. NSNumber* lon = marker[@"lon"];
  95. CLLocationDegrees latitude = lat.doubleValue;
  96. CLLocationDegrees longitude = lon.doubleValue;
  97. pointAnnotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  98. pointAnnotation.phoneNumber = marker[@"tel"];
  99. pointAnnotation.map = marker;
  100. pointAnnotation.title = marker[@"title"];
  101. pointAnnotation.subtitle = marker[@"content"];
  102. [_mapView addAnnotation:pointAnnotation];
  103. }
  104. result(nil);
  105. }else if([call.method isEqualToString:@"onCreate"]){
  106. result(nil);
  107. }else if([call.method isEqualToString:@"onPause"]){
  108. result(nil);
  109. }else if([call.method isEqualToString:@"onResume"]){
  110. result(nil);
  111. }else if([call.method isEqualToString:@"moveCamera"]){
  112. NSDictionary *data = [call.arguments objectForKey:@"data"];
  113. NSNumber *x = data[@"x"];
  114. NSNumber *y = data[@"y"];
  115. _mapView.centerCoordinate=CLLocationCoordinate2DMake(x.doubleValue, y.doubleValue);
  116. result(nil);
  117. }else {
  118. result(FlutterMethodNotImplemented);
  119. }
  120. }
  121. - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
  122. {
  123. if ([annotation isKindOfClass:[CustomAnnotation class]])
  124. {
  125. static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
  126. CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
  127. if (annotationView == nil)
  128. {
  129. annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
  130. }
  131. annotationView.channel = self.channel;
  132. annotationView.image = [UIImage imageNamed:@"i2"];
  133. annotationView.image=[self scaleToSize: annotationView.image size:CGSizeMake(50, 60)];
  134. // annotationView.pinColor = MAPinAnnotationColorPurple;
  135. annotationView.canShowCallout= NO; //设置气泡可以弹出,默认为NO
  136. // annotationView.animatesDrop = YES; //设置标注动画显示,默认为NO
  137. // annotationView.draggable = YES; //设置标注可以拖动,默认为NO
  138. return annotationView;
  139. }
  140. return nil;
  141. }
  142. @end