FlutterAmapView.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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=[UIImage imageWithContentsOfFile:@"Assets/Images/dog.png"];
  60. // r.locationDotFillColor = [UIColor redColor];
  61. return r;
  62. }
  63. - (UIView *)view{
  64. return _mapView;
  65. }
  66. -(void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  67. if ([call.method isEqualToString:@"setMarkers"]) {
  68. NSArray<NSDictionary*> * markers = [call.arguments objectForKey:@"markers"];
  69. for (NSDictionary* marker in markers) {
  70. CustomAnnotation *pointAnnotation = [[CustomAnnotation alloc] init];
  71. NSNumber* lat = marker[@"lat"];
  72. NSNumber* lon = marker[@"lon"];
  73. CLLocationDegrees latitude = lat.doubleValue;
  74. CLLocationDegrees longitude = lon.doubleValue;
  75. pointAnnotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  76. pointAnnotation.phoneNumber = marker[@"tel"];
  77. pointAnnotation.map = marker;
  78. pointAnnotation.title = marker[@"title"];
  79. pointAnnotation.subtitle = marker[@"content"];
  80. [_mapView addAnnotation:pointAnnotation];
  81. }
  82. result(nil);
  83. }else if([call.method isEqualToString:@"onCreate"]){
  84. result(nil);
  85. }else if([call.method isEqualToString:@"onPause"]){
  86. result(nil);
  87. }else if([call.method isEqualToString:@"onResume"]){
  88. result(nil);
  89. }else if([call.method isEqualToString:@"moveCamera"]){
  90. NSDictionary *data = [call.arguments objectForKey:@"data"];
  91. NSNumber *x = data[@"x"];
  92. NSNumber *y = data[@"y"];
  93. _mapView.centerCoordinate=CLLocationCoordinate2DMake(x.doubleValue, y.doubleValue);
  94. result(nil);
  95. }else {
  96. result(FlutterMethodNotImplemented);
  97. }
  98. }
  99. - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
  100. {
  101. if ([annotation isKindOfClass:[CustomAnnotation class]])
  102. {
  103. static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
  104. CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
  105. if (annotationView == nil)
  106. {
  107. annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
  108. }
  109. annotationView.channel = self.channel;
  110. annotationView.image = [UIImage imageNamed:@"i2"];
  111. // annotationView.pinColor = MAPinAnnotationColorPurple;
  112. annotationView.canShowCallout= NO; //设置气泡可以弹出,默认为NO
  113. // annotationView.animatesDrop = YES; //设置标注动画显示,默认为NO
  114. // annotationView.draggable = YES; //设置标注可以拖动,默认为NO
  115. return annotationView;
  116. }
  117. return nil;
  118. }
  119. @end