| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- //
- // 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<FlutterBinaryMessenger>* _messenger;
- FlutterAmapView* amapView;
-
- }
- - (nonnull NSObject<FlutterPlatformView> *)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<FlutterMessageCodec> *)createArgsCodec{
- return [FlutterStandardMessageCodec sharedInstance];
- }
- - (instancetype)initWithMessenger:(NSObject<FlutterBinaryMessenger> *)messenger{
- self = [super init];
- if (self) {
- _messenger=messenger;
- }
- return self;
- }
- @end
- @interface FlutterAmapView()<MAMapViewDelegate>
- @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<FlutterBinaryMessenger> *)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 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=[UIImage imageWithContentsOfFile:@"Assets/Images/dog.png"];
- // r.locationDotFillColor = [UIColor redColor];
- return r;
-
- }
- - (UIView *)view{
- return _mapView;
-
- }
- -(void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
- if ([call.method isEqualToString:@"setMarkers"]) {
- NSArray<NSDictionary*> * 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 {
- result(FlutterMethodNotImplemented);
- }
- }
- - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)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.pinColor = MAPinAnnotationColorPurple;
- annotationView.canShowCallout= NO; //设置气泡可以弹出,默认为NO
- // annotationView.animatesDrop = YES; //设置标注动画显示,默认为NO
- // annotationView.draggable = YES; //设置标注可以拖动,默认为NO
- return annotationView;
- }
- return nil;
- }
- @end
|