Skip to content

Commit f343a3a

Browse files
authored
Merge pull request #74 from benedictstrube/fix-annotation-view
fix(standard user location annotation view): restored standard user location annotation view
2 parents 0f4df97 + cf9e189 commit f343a3a

File tree

1 file changed

+1
-310
lines changed

1 file changed

+1
-310
lines changed

src/ui-mapbox/index.ios.ts

Lines changed: 1 addition & 310 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class MGLMapViewDelegateImpl extends NSObject implements MGLMapViewDelegate {
6161
private cameraChangedListener: (reason, animated?: boolean) => void;
6262
private cameraIdledListener: () => void;
6363
private userLocationRenderMode: any;
64-
private userLocationAnnotationView: CustomUserLocationAnnotationView;
6564

6665
/**
6766
* initialize with the mapReady callback
@@ -100,7 +99,7 @@ class MGLMapViewDelegateImpl extends NSObject implements MGLMapViewDelegate {
10099
* set user location marker modes
101100
*/
102101
changeUserLocationRenderMode(userLocationRenderMode) {
103-
this.userLocationAnnotationView.changeUserLocationRenderMode(userLocationRenderMode);
102+
// nothing to do here
104103
}
105104

106105
/**
@@ -315,23 +314,6 @@ class MGLMapViewDelegateImpl extends NSObject implements MGLMapViewDelegate {
315314
}
316315
}
317316

318-
/**
319-
* override the standard location marker
320-
*/
321-
mapViewViewForAnnotation(mapView: MGLMapView, annotation: MGLAnnotation): MGLAnnotationView {
322-
if (Trace.isEnabled()) {
323-
CLog(CLogTypes.info, 'MGLMapViewDelegateImpl::mapViewViewForAnnotation() top');
324-
}
325-
326-
if (annotation.isKindOfClass(MGLUserLocation.class())) {
327-
this.userLocationAnnotationView = CustomUserLocationAnnotationView.alloc().init() as CustomUserLocationAnnotationView;
328-
329-
return this.userLocationAnnotationView;
330-
}
331-
332-
return null;
333-
}
334-
335317
mapViewRegionIsChangingWithReason(mapView: MGLMapView, reason: MGLCameraChangeReason) {
336318
if (Trace.isEnabled()) {
337319
CLog(CLogTypes.info, 'MGLMapViewDelegateImpl::mapViewRegionIsChanging()');
@@ -894,297 +876,6 @@ export class MapboxView extends MapboxViewBase {
894876
}
895877
}
896878

897-
/**
898-
* a custom user location marker
899-
*
900-
* We want to add some behavior to the user location marker to visibly
901-
* show the user when locations are being stored and when they are not.
902-
*
903-
* Sadly, it's not as easy under iOS as it is on Android. It involves
904-
* creating a custom annotation view.
905-
*
906-
* @link https://docs.mapbox.com/ios/maps/examples/user-location-annotation/
907-
*/
908-
909-
@NativeClass
910-
class CustomUserLocationAnnotationView extends MGLUserLocationAnnotationView implements MGLUserLocationAnnotationView {
911-
public size: number;
912-
public dot: CALayer;
913-
public arrow: CAShapeLayer;
914-
915-
// may be NORMAL, COMPASS, or GPS.
916-
917-
private userLocationRenderMode: string;
918-
private renderModeChanged: boolean;
919-
920-
/**
921-
* init
922-
*
923-
* @link https://docs.nativescript.org/core-concepts/ios-runtime/HelloWorld
924-
*/
925-
926-
public init() {
927-
this.size = 48;
928-
super.initWithFrame(CGRectMake(0, 0, this.size, this.size));
929-
930-
this.renderModeChanged = true;
931-
this.userLocationRenderMode = 'NORMAL';
932-
933-
return this;
934-
}
935-
936-
/**
937-
* update
938-
*
939-
* The note from the Objective-C sample indicates this method may be called quite
940-
* often so it needs to be kept lightweight.
941-
*/
942-
943-
update() {
944-
if (CLLocationCoordinate2DIsValid(this.userLocation.coordinate)) {
945-
// if it's the first time here, setup the layers that make up the
946-
// location marker.
947-
948-
if (!this.dot) {
949-
this.drawNonTrackingLocationMarker();
950-
}
951-
952-
if (this.userLocationRenderMode === 'GPS') {
953-
this.updateHeading();
954-
}
955-
}
956-
}
957-
958-
/**
959-
* Draw the GPS tracking arrow.
960-
*
961-
* @link https://docs.nativescript.org/ns-framework-modules/color
962-
*/
963-
964-
drawTrackingLocationMarker() {
965-
if (Trace.isEnabled()) {
966-
CLog(CLogTypes.info, 'CustomerUserLocationAnnotatinView::drawTrackingLocationMarker()');
967-
}
968-
969-
this.drawTrackingDot();
970-
this.drawArrow();
971-
} // end of setupLayers()
972-
973-
/**
974-
* draw the non-tracking marker
975-
*/
976-
977-
drawNonTrackingLocationMarker() {
978-
if (Trace.isEnabled()) {
979-
CLog(CLogTypes.info, 'CustomerUserLocationAnnotatinView::drawNonTrackingLocationMarker()');
980-
}
981-
982-
this.drawNonTrackingDot();
983-
984-
if (this.arrow) {
985-
this.arrow.removeFromSuperlayer();
986-
this.arrow = null;
987-
}
988-
}
989-
990-
/**
991-
* draw the tracking dot.
992-
*/
993-
994-
drawTrackingDot() {
995-
this.size = 48;
996-
997-
// we need to adjust the size of the bounds of the marker. The Tracking marker
998-
// is larger than the non tracking marker.
999-
1000-
this.bounds = CGRectMake(0, 0, this.size, this.size);
1001-
1002-
const dot = CALayer.layer();
1003-
1004-
dot.frame = this.bounds;
1005-
1006-
// user corner radius to turn the layer into a circle
1007-
1008-
dot.cornerRadius = this.size / 2;
1009-
dot.backgroundColor = this.tintColor.CGColor;
1010-
dot.borderWidth = 4;
1011-
1012-
const whiteColor = new Color('#FFFFFF');
1013-
dot.borderColor = whiteColor.ios.CGColor;
1014-
1015-
if (!this.dot) {
1016-
this.layer.addSublayer(dot);
1017-
} else {
1018-
this.layer.replaceSublayerWith(this.dot, dot);
1019-
}
1020-
1021-
// QUESTION: does GC catch this?
1022-
1023-
this.dot = dot;
1024-
}
1025-
1026-
/**
1027-
* draw the non-tracking dot.
1028-
*/
1029-
1030-
drawNonTrackingDot() {
1031-
this.size = 24;
1032-
this.bounds = CGRectMake(0, 0, this.size, this.size);
1033-
const dot = CALayer.layer();
1034-
1035-
dot.frame = this.bounds;
1036-
1037-
// user corner radius to turn the layer into a circle
1038-
1039-
dot.cornerRadius = this.size / 2;
1040-
dot.backgroundColor = this.tintColor.CGColor;
1041-
1042-
dot.borderWidth = 1;
1043-
1044-
const whiteColor = new Color('#FFFFFF');
1045-
dot.borderColor = whiteColor.ios.CGColor;
1046-
1047-
if (!this.dot) {
1048-
this.layer.addSublayer(dot);
1049-
} else {
1050-
this.layer.replaceSublayerWith(this.dot, dot);
1051-
}
1052-
1053-
// QUESTION: does GC catch this?
1054-
1055-
this.dot = dot;
1056-
}
1057-
1058-
/**
1059-
* draw an arrow
1060-
*/
1061-
1062-
drawArrow() {
1063-
const arrow = CAShapeLayer.layer();
1064-
1065-
arrow.path = this.arrowPath();
1066-
arrow.frame = CGRectMake(0, 0, this.size / 2, this.size / 2);
1067-
arrow.position = CGPointMake(CGRectGetMidX(this.dot.frame), CGRectGetMidY(this.dot.frame));
1068-
arrow.fillColor = this.dot.borderColor;
1069-
1070-
if (!this.arrow) {
1071-
this.layer.addSublayer(arrow);
1072-
} else {
1073-
this.layer.replaceSublayerWith(this.arrow, arrow);
1074-
}
1075-
1076-
// QUESTION: Does GC catch this?
1077-
1078-
this.arrow = arrow;
1079-
}
1080-
1081-
/**
1082-
* update arrow heading
1083-
*
1084-
* @link https://docs.nativescript.org/core-concepts/ios-runtime/types/C-Functions
1085-
*/
1086-
1087-
updateHeading() {
1088-
// just to avoid a possible race condition where the arrow isnt' drawn yet
1089-
1090-
if (!this.arrow) {
1091-
return;
1092-
}
1093-
1094-
if (typeof this.userLocation == 'undefined') {
1095-
return;
1096-
}
1097-
1098-
if (typeof this.userLocation.heading == 'undefined' || this.userLocation.heading === null) {
1099-
return;
1100-
}
1101-
1102-
if (typeof this.userLocation.heading.trueHeading == 'undefined' || this.userLocation.heading.trueHeading === null) {
1103-
return;
1104-
}
1105-
1106-
if (this.userLocation.heading.trueHeading > 0) {
1107-
this.arrow.hidden = false;
1108-
1109-
// get the difference between the map's current direction and the
1110-
// user's heading, then convert it from degrees to radians
1111-
//
1112-
// The original Objective-C example uses the inline C function MGLRadiansFromDegrees but because
1113-
// it's declared as inline it is not available for NativeScript. See linked article above.
1114-
1115-
// let rotation : number = MGLRadiansFromDegrees( this.mapView.direction - this.userLocation.heading.trueHeading );
1116-
1117-
const degrees: number = this.mapView.direction - this.userLocation.heading.trueHeading;
1118-
1119-
// in radians
1120-
1121-
let rotation: number = (degrees * Math.PI) / 180;
1122-
1123-
rotation = -rotation;
1124-
1125-
// if the difference would be perceptible, rotate the arrow.
1126-
1127-
if (fabs(rotation) > 0.01) {
1128-
// Disable implicit animations of this rotation, which reduces lag between updates
1129-
1130-
CATransaction.begin();
1131-
CATransaction.setDisableActions(true);
1132-
1133-
this.arrow.setAffineTransform(CGAffineTransformRotate(CGAffineTransformIdentity, rotation));
1134-
1135-
CATransaction.commit();
1136-
}
1137-
} else {
1138-
this.arrow.hidden = true;
1139-
}
1140-
}
1141-
1142-
/**
1143-
* Calculate the vector path for an arrow
1144-
*/
1145-
1146-
arrowPath() {
1147-
const max: number = this.size / 2;
1148-
const pad: number = 3;
1149-
1150-
const top: CGPoint = CGPointMake(max * 0.5, 0);
1151-
const left: CGPoint = CGPointMake(0 + pad, max - pad);
1152-
const right: CGPoint = CGPointMake(max - pad, max - pad);
1153-
const center: CGPoint = CGPointMake(max * 0.5, max * 0.6);
1154-
1155-
const bezierPath = UIBezierPath.bezierPath();
1156-
bezierPath.moveToPoint(top);
1157-
bezierPath.addLineToPoint(left);
1158-
bezierPath.addLineToPoint(center);
1159-
1160-
bezierPath.addLineToPoint(right);
1161-
bezierPath.addLineToPoint(top);
1162-
bezierPath.closePath();
1163-
1164-
return bezierPath.CGPath;
1165-
}
1166-
1167-
/**
1168-
* change Render mode
1169-
*
1170-
* @param {string} renderMode
1171-
*/
1172-
1173-
changeUserLocationRenderMode(renderMode) {
1174-
if (Trace.isEnabled()) {
1175-
CLog(CLogTypes.info, "CustomUserLocationAnnotatinView::changeUserLocationRenderMode(): changing mode to '" + renderMode + "'");
1176-
}
1177-
1178-
this.userLocationRenderMode = renderMode;
1179-
1180-
if (renderMode === 'GPS') {
1181-
this.drawTrackingLocationMarker();
1182-
} else {
1183-
this.drawNonTrackingLocationMarker();
1184-
}
1185-
}
1186-
} // end of class CustomUserLocationAnnotationView
1187-
1188879
export class Mapbox extends MapboxCommon implements MapboxApi {
1189880
// reference to the native mapbox API
1190881

0 commit comments

Comments
 (0)