Allow initializing MLNMapView with style json (#3240)
Co-authored-by: Yingfang <junew@microsoft.com> Co-authored-by: Bart Louwers <bart@emeel.net>
This commit is contained in:
@@ -207,6 +207,18 @@ MLN_EXPORT
|
||||
*/
|
||||
- (instancetype)initWithFrame:(CGRect)frame styleURL:(nullable NSURL *)styleURL;
|
||||
|
||||
/**
|
||||
* Initializes and returns a newly allocated map view with the specified frame
|
||||
* and style JSON.
|
||||
*
|
||||
* @param frame The frame for the view, measured in points.
|
||||
* @param styleJSON JSON string of the map style to display. The JSON must conform to the
|
||||
* <a href="https://maplibre.org/maplibre-style-spec/">MapLibre Style Specification</a>.
|
||||
* Specify `nil` for the default style.
|
||||
* @return An initialized map view.
|
||||
*/
|
||||
- (instancetype)initWithFrame:(CGRect)frame styleJSON:(NSString *)styleJSON;
|
||||
|
||||
// MARK: Accessing the Delegate
|
||||
|
||||
/**
|
||||
@@ -253,6 +265,20 @@ MLN_EXPORT
|
||||
*/
|
||||
@property (nonatomic, null_resettable) NSURL *styleURL;
|
||||
|
||||
/**
|
||||
* The style JSON representation of the map.
|
||||
*
|
||||
* Setting this property results in an asynchronous style change. If you wish to know when the style
|
||||
* change is complete, observe the ``MLNMapViewDelegate/mapView:didFinishLoadingStyle:`` method
|
||||
* on ``MLNMapViewDelegate``.
|
||||
*
|
||||
* The JSON must conform to the
|
||||
* <a href="https://maplibre.org/maplibre-style-spec/">MapLibre Style Specification</a>.
|
||||
*
|
||||
* @throws NSInvalidArgumentException if styleJSON is nil or invalid JSON
|
||||
*/
|
||||
@property (nonatomic, copy) NSString *styleJSON;
|
||||
|
||||
/**
|
||||
Reloads the style.
|
||||
|
||||
|
||||
@@ -424,6 +424,7 @@ public:
|
||||
@property (nonatomic, copy) MLNMapCamera *residualCamera;
|
||||
@property (nonatomic) MLNMapDebugMaskOptions residualDebugMask;
|
||||
@property (nonatomic, copy) NSURL *residualStyleURL;
|
||||
@property (nonatomic, copy, nullable) NSString *initialStyleJSON;
|
||||
|
||||
/// Tilt gesture recognizer helper
|
||||
@property (nonatomic, assign) CGPoint dragGestureMiddlePoint;
|
||||
@@ -522,6 +523,20 @@ public:
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame styleJSON:(NSString *)styleJSON
|
||||
{
|
||||
if (self = [super initWithFrame:frame])
|
||||
{
|
||||
MLNLogInfo(@"Starting %@ initialization.", NSStringFromClass([self class]));
|
||||
MLNLogDebug(@"Initializing frame: %@ styleJSON: %@", NSStringFromCGRect(frame), styleJSON);
|
||||
[self commonInit];
|
||||
self.styleJSON = styleJSON;
|
||||
_initialStyleJSON = [styleJSON copy];
|
||||
MLNLogInfo(@"Finalizing %@ initialization.", NSStringFromClass([self class]));
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithCoder:(nonnull NSCoder *)decoder
|
||||
{
|
||||
if (self = [super initWithCoder:decoder])
|
||||
@@ -552,6 +567,11 @@ public:
|
||||
return self.residualStyleURL;
|
||||
}
|
||||
|
||||
if (self.mbglMap.getStyle().getJSON().length() > 0 &&
|
||||
self.mbglMap.getStyle().getURL().empty()) {
|
||||
return [NSURL URLWithString:@"local://style.json"];
|
||||
}
|
||||
|
||||
NSString *styleURLString = @(self.mbglMap.getStyle().getURL().c_str()).mgl_stringOrNilIfEmpty;
|
||||
MLNAssert(styleURLString, @"Invalid style URL string %@", styleURLString);
|
||||
return styleURLString ? [NSURL URLWithString:styleURLString] : nil;
|
||||
@@ -569,6 +589,16 @@ public:
|
||||
self.mbglMap.getStyle().loadURL([[styleURL absoluteString] UTF8String]);
|
||||
}
|
||||
|
||||
- (NSString *)styleJSON {
|
||||
return self.style.styleJSON;
|
||||
}
|
||||
|
||||
- (void)setStyleJSON:(NSString *)styleJSON {
|
||||
// Reset style and load new JSON
|
||||
self.style = nil;
|
||||
self.mbglMap.getStyle().loadJSON([styleJSON UTF8String]);
|
||||
}
|
||||
|
||||
- (IBAction)reloadStyle:(__unused id)sender {
|
||||
MLNLogInfo(@"Reloading style.");
|
||||
NSURL *styleURL = self.styleURL;
|
||||
@@ -890,7 +920,9 @@ public:
|
||||
self.terminated = YES;
|
||||
self.residualCamera = self.camera;
|
||||
self.residualDebugMask = self.debugMask;
|
||||
self.residualStyleURL = self.styleURL;
|
||||
if (!_initialStyleJSON) {
|
||||
self.residualStyleURL = self.styleURL;
|
||||
}
|
||||
|
||||
// Tear down C++ objects, insuring worker threads correctly terminate.
|
||||
// Because of how _mbglMap is constructed, we need to destroy it first.
|
||||
|
||||
Reference in New Issue
Block a user