FlutterAmapView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 updateUserLocationRepresentation:[self getRepresentation]];
  48. _mapView.delegate=self;
  49. ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
  50. _mapView.showsUserLocation = YES;
  51. _mapView.userTrackingMode = MAUserTrackingModeFollow;
  52. }
  53. return self;
  54. }
  55. -(MAUserLocationRepresentation*)getRepresentation{
  56. MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
  57. r.image=[UIImage imageNamed:@"dog"];
  58. // r.image=[UIImage imageWithContentsOfFile:@"Assets/Images/dog.png"];
  59. // r.locationDotFillColor = [UIColor redColor];
  60. return r;
  61. }
  62. - (UIView *)view{
  63. return _mapView;
  64. }
  65. -(void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  66. if ([call.method isEqualToString:@"setMarkers"]) {
  67. NSArray<NSDictionary*> * markers = [call.arguments objectForKey:@"markers"];
  68. for (NSDictionary* marker in markers) {
  69. CustomAnnotation *pointAnnotation = [[CustomAnnotation alloc] init];
  70. NSNumber* lat = marker[@"lat"];
  71. NSNumber* lon = marker[@"lon"];
  72. CLLocationDegrees latitude = lat.doubleValue;
  73. CLLocationDegrees longitude = lon.doubleValue;
  74. pointAnnotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  75. pointAnnotation.phoneNumber = marker[@"tel"];
  76. pointAnnotation.map = marker;
  77. pointAnnotation.title = marker[@"title"];
  78. pointAnnotation.subtitle = marker[@"content"];
  79. [_mapView addAnnotation:pointAnnotation];
  80. }
  81. result(nil);
  82. }else if([call.method isEqualToString:@"onCreate"]){
  83. result(nil);
  84. }else if([call.method isEqualToString:@"onPause"]){
  85. result(nil);
  86. }else if([call.method isEqualToString:@"onResume"]){
  87. result(nil);
  88. }else {
  89. result(FlutterMethodNotImplemented);
  90. }
  91. }
  92. - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
  93. {
  94. if ([annotation isKindOfClass:[CustomAnnotation class]])
  95. {
  96. static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
  97. CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
  98. if (annotationView == nil)
  99. {
  100. annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
  101. }
  102. annotationView.channel = self.channel;
  103. annotationView.image = [UIImage imageNamed:@"i2"];
  104. // annotationView.pinColor = MAPinAnnotationColorPurple;
  105. annotationView.canShowCallout= NO; //设置气泡可以弹出,默认为NO
  106. // annotationView.animatesDrop = YES; //设置标注动画显示,默认为NO
  107. // annotationView.draggable = YES; //设置标注可以拖动,默认为NO
  108. return annotationView;
  109. }
  110. return nil;
  111. }
  112. @end