amap_map/ios/Classes/Category/MAMapView+Flutter.m

277 lines
13 KiB
Mathematica
Raw Normal View History

2023-12-22 21:23:24 +08:00
//
// MAMapView+Flutter.m
// amap_map
//
// Created by lly on 2020/10/30.
// Updated by kuloud on 2024/07/29.
2023-12-22 21:23:24 +08:00
//
#import "MAMapView+Flutter.h"
#import "AMapCameraPosition.h"
#import "AMapConvertUtil.h"
#import "AMapJsonUtils.h"
#import <Flutter/Flutter.h>
@implementation MAMapView (Flutter)
- (void)setCameraPosition:(AMapCameraPosition *)cameraPosition animated:(BOOL)animated duration:(CFTimeInterval)duration {
if (cameraPosition == nil) {
return;
}
MAMapStatus *mapStatus = [MAMapStatus statusWithCenterCoordinate:cameraPosition.target
zoomLevel:cameraPosition.zoom
rotationDegree:cameraPosition.bearing
cameraDegree:cameraPosition.tilt
screenAnchor:self.screenAnchor];
[self setMapStatus:mapStatus animated:animated duration:duration];
}
- (AMapCameraPosition *)getCurrentCameraPosition {
AMapCameraPosition *position = [[AMapCameraPosition alloc] init];
position.target = self.centerCoordinate;
position.zoom = self.zoomLevel;
position.bearing = self.rotationDegree;
position.tilt = self.cameraDegree;
return position;
}
- (void)setCameraUpdateDict:(NSDictionary *)updateDict {
if (updateDict == nil || updateDict.count == 0) {
return;
}
BOOL animated;
if ([updateDict[@"animated"] isKindOfClass:[NSNull class]]) {
animated = NO;
} else {
animated = [updateDict[@"animated"] boolValue];
}
double duration;
if ([updateDict[@"duration"] isKindOfClass:[NSNull class]]) {
duration = 0;
} else {
duration = [updateDict[@"duration"] doubleValue]/1000.0;
}
NSArray *cameraUpdate = updateDict[@"cameraUpdate"];
//
NSAssert(cameraUpdate.count >= 1, @"cameraUpdate 参数错误");
NSString *operation = cameraUpdate.firstObject;
if ([operation isEqualToString:@"newCameraPosition"] && cameraUpdate.count == 2) {//cameraPositon
AMapCameraPosition *newCameraPosition = [AMapJsonUtils modelFromDict:cameraUpdate[1] modelClass:[AMapCameraPosition class]];
[self setCameraPosition:newCameraPosition animated:animated duration:duration];
} else if ([operation isEqualToString:@"newLatLng"] && cameraUpdate.count == 2) {//
CLLocationCoordinate2D position = [AMapConvertUtil coordinateFromArray:cameraUpdate[1]];
MAMapStatus *currentStatus = [self getMapStatus];
currentStatus.centerCoordinate = position;
[self setMapStatus:currentStatus animated:animated duration:duration];
} else if ([operation isEqualToString:@"newLatLngBounds"] && cameraUpdate.count == 3) {//
// TODO: 使duration
MAMapRect boundRect = [AMapConvertUtil mapRectFromArray:cameraUpdate[1]];
double padding = [cameraUpdate[2] doubleValue];
UIEdgeInsets inset = UIEdgeInsetsMake(padding, padding, padding, padding);
[self setVisibleMapRect:boundRect edgePadding:inset animated:animated];
} else if ([operation isEqualToString:@"newLatLngZoom"] && cameraUpdate.count == 3) {//zoomLevel
CLLocationCoordinate2D position = [AMapConvertUtil coordinateFromArray:cameraUpdate[1]];
CGFloat zoomLevel = [cameraUpdate[2] floatValue];
MAMapStatus *currentStatus = [self getMapStatus];
currentStatus.centerCoordinate = position;
currentStatus.zoomLevel = zoomLevel;
[self setMapStatus:currentStatus animated:animated duration:duration];
} else if ([operation isEqualToString:@"scrollBy"] && cameraUpdate.count == 3) {//
CGPoint pixelPointOffset = CGPointMake([cameraUpdate[1] doubleValue], [cameraUpdate[2] doubleValue]);
CGPoint updateCenter = CGPointMake(self.center.x + pixelPointOffset.x/[UIScreen mainScreen].scale, self.center.y + pixelPointOffset.y/[UIScreen mainScreen].scale);
CLLocationCoordinate2D centerCoord = [self convertPoint:updateCenter toCoordinateFromView:self];
MAMapStatus *currentStatus = [self getMapStatus];
currentStatus.centerCoordinate = centerCoord;
[self setMapStatus:currentStatus animated:animated duration:duration];
} else if ([operation isEqualToString:@"zoomIn"]) {
MAMapStatus *currentStatus = [self getMapStatus];
currentStatus.zoomLevel = currentStatus.zoomLevel + 1;
[self setMapStatus:currentStatus animated:animated duration:duration];
} else if ([operation isEqualToString:@"zoomOut"]) {
MAMapStatus *currentStatus = [self getMapStatus];
currentStatus.zoomLevel = currentStatus.zoomLevel - 1;
[self setMapStatus:currentStatus animated:animated duration:duration];
} else if ([operation isEqualToString:@"zoomTo"] && cameraUpdate.count == 2) {
MAMapStatus *currentStatus = [self getMapStatus];
currentStatus.zoomLevel = [cameraUpdate[1] doubleValue];
[self setMapStatus:currentStatus animated:animated duration:duration];
}
}
- (void)updateMapViewOption:(NSDictionary *)dict withRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
if ([dict isKindOfClass:[NSDictionary class]] == NO || dict == nil || dict.count == 0) {
return;
}
//
NSNumber *mapType = dict[@"mapType"];
if (mapType) {
self.mapType = mapType.integerValue;
}
NSDictionary *customStyleOptions = dict[@"customStyleOptions"];
if (customStyleOptions) {
BOOL customMapStyleEnabled = [customStyleOptions[@"enabled"] boolValue];
self.customMapStyleEnabled = customMapStyleEnabled;
if (customMapStyleEnabled) {
MAMapCustomStyleOptions *styleOption = [[MAMapCustomStyleOptions alloc] init];
styleOption.styleData = ((FlutterStandardTypedData*)customStyleOptions[@"styleData"]).data;
styleOption.styleExtraData = ((FlutterStandardTypedData*)customStyleOptions[@"styleExtraData"]).data;
[self setCustomMapStyleOptions:styleOption];
}
}
NSDictionary *locationStyleDict = dict[@"myLocationStyle"];
if (locationStyleDict) {
BOOL showUserLocation = [locationStyleDict[@"enabled"] boolValue];
self.showsUserLocation = showUserLocation;
if (showUserLocation) {
self.userTrackingMode = MAUserTrackingModeNone;//demo使
if (locationStyleDict[@"circleFillColor"] != nil
|| locationStyleDict[@"circleStrokeColor"] != nil
|| locationStyleDict[@"circleStrokeWidth"] != nil
|| locationStyleDict[@"icon"] != nil) {//
MAUserLocationRepresentation *locationStyle = [[MAUserLocationRepresentation alloc] init];
if (locationStyleDict[@"circleFillColor"]) {
locationStyle.fillColor = [AMapConvertUtil colorFromNumber:locationStyleDict[@"circleFillColor"]];
}
if (locationStyleDict[@"circleStrokeColor"]) {
locationStyle.strokeColor = [AMapConvertUtil colorFromNumber:locationStyleDict[@"circleStrokeColor"]];
}
if (locationStyleDict[@"circleStrokeWidth"]) {
locationStyle.lineWidth = [locationStyleDict[@"circleStrokeWidth"] doubleValue];
}
if (locationStyleDict[@"icon"]) {
locationStyle.image = [AMapConvertUtil imageFromRegistrar:registrar iconData:locationStyleDict[@"icon"]];
}
[self updateUserLocationRepresentation:locationStyle];
}
}
}
///
NSArray *screenAnchor = dict[@"screenAnchor"];
if (screenAnchor) {
CGPoint anchorPoint = [AMapConvertUtil pointFromArray:screenAnchor];
self.screenAnchor = anchorPoint;
}
//
NSArray* zoomData = dict[@"minMaxZoomPreference"];
if (zoomData) {
CGFloat minZoom = (zoomData[0] == [NSNull null]) ? 3.0 : [zoomData[0] doubleValue];
self.minZoomLevel = minZoom;
CGFloat maxZoom = (zoomData[1] == [NSNull null]) ? 20.0 : [zoomData[1] doubleValue];
self.maxZoomLevel = maxZoom;
}
NSArray *limitBounds = dict[@"limitBounds"];
if (limitBounds) {
MAMapRect limitRect = [AMapConvertUtil mapRectFromArray:limitBounds];
self.limitMapRect = limitRect;
}
//
NSNumber *showTraffic = dict[@"trafficEnabled"];
if (showTraffic) {
self.showTraffic = [showTraffic boolValue];
}
// poi
NSNumber *touchPOIEnable = dict[@"touchPoiEnabled"];
if (touchPOIEnable) {
self.touchPOIEnabled = [touchPOIEnable boolValue];
}
//3D
NSNumber *showBuilding = dict[@"buildingsEnabled"];
if (showBuilding) {
self.showsBuildings = [showBuilding boolValue];
}
//
NSNumber *showLable = dict[@"labelsEnabled"];
if (showLable) {
self.showsLabels = [showLable boolValue];
}
//
NSNumber *showCompass = dict[@"compassEnabled"];
if (showCompass) {
self.showsCompass = [showCompass boolValue];
}
//
NSNumber *showScale = dict[@"scaleEnabled"];
if (showScale) {
self.showsScale = [showScale boolValue];
}
//
NSNumber *zoomEnable = dict[@"zoomGesturesEnabled"];
if (zoomEnable) {
self.zoomEnabled = [zoomEnable boolValue];
}
//
NSNumber *scrollEnable = dict[@"scrollGesturesEnabled"];
if (scrollEnable) {
self.scrollEnabled = [scrollEnable boolValue];
}
//
NSNumber *rotateEnable = dict[@"rotateGesturesEnabled"];
if (rotateEnable) {
self.rotateEnabled = [rotateEnable boolValue];
}
//
NSNumber *rotateCameraEnable = dict[@"tiltGesturesEnabled"];
if (rotateCameraEnable) {
self.rotateCameraEnabled = [rotateCameraEnable boolValue];
}
NSNumber *logoPosition = dict[@"logoPosition"];
NSNumber *logoBottomMargin = dict[@"logoBottomMargin"];
NSNumber *logoLeftMargin = dict[@"logoLeftMargin"];
CGSize mapSize = self.bounds.size;
CGSize logoSize = CGSizeMake(110, 40); // _mapView.logoCenter = CGPointMake(CGRectGetWidth(self.view.bounds)-55, 450);logo110x40
CGPoint logoCenter = CGPointZero;
if (logoBottomMargin || logoLeftMargin) {
CGFloat bottomMargin = logoBottomMargin ? [logoBottomMargin floatValue] : 0;
CGFloat leftMargin = logoLeftMargin ? [logoLeftMargin floatValue] : 0;
logoCenter.x = leftMargin + logoSize.width / 2;
logoCenter.y = mapSize.height - bottomMargin - logoSize.height / 2;
} else if (logoPosition) {
NSInteger position = [logoPosition integerValue];
switch (position) {
case 0: // LOGO_POSITION_BOTTOM_LEFT
logoCenter = CGPointMake(logoSize.width / 2, mapSize.height - logoSize.height / 2);
break;
case 1: // LOGO_POSITION_BOTTOM_CENTER
logoCenter = CGPointMake(mapSize.width / 2, mapSize.height - logoSize.height / 2);
break;
case 2: // LOGO_POSITION_BOTTOM_RIGHT
logoCenter = CGPointMake(mapSize.width - logoSize.width / 2, mapSize.height - logoSize.height / 2);
break;
default:
//
logoCenter = CGPointMake(logoSize.width / 2, mapSize.height - logoSize.height / 2);
break;
}
} else {
logoCenter = CGPointMake(logoSize.width / 2, mapSize.height - logoSize.height / 2);
}
// logoCentermapView.bounds
logoCenter.x = MAX(logoSize.width / 2, MIN(logoCenter.x, mapSize.width - logoSize.width / 2));
logoCenter.y = MAX(logoSize.height / 2, MIN(logoCenter.y, mapSize.height - logoSize.height / 2));
// logo
self.logoCenter = logoCenter;
//
NSString *mapLanguage = dict[@"mapLanguage"];
// :@0: :@1. 使1使2MAMapTypeStandard
if ([mapLanguage isEqualToString:@"zh_cn"]) {
self.mapLanguage = @0;
} else if ([mapLanguage isEqualToString:@"en"]) {
self.mapLanguage = @1;
}
2023-12-22 21:23:24 +08:00
}
@end