• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 子类化的 MKTileOverlay 不在 map 上显示

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:33:18 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试从我们的 WMS 服务器显示 WMS 磁贴。我用旧的 MKOverlay 实现了这一点,但我现在正在尝试更新它以使用 MKTileOverlay 但我无法显示瓷砖。这是我的课:

@interface WMSTileOverlay ()

@property (strong, nonatomic) NSString *API, *mapContext, *IDToken;

@end

@implementation WMSTileOverlay

-(instancetype)initWithAPINSString *)API IDTokenNSString *)IDToken andMapContextNSString *)mapContext {
    self = [super init];

    if (self) {
        self.API = API;
        self.mapContext = mapContext;
        self.IDToken = IDToken;
    }

    return self;
}

-(NSURL *)URLForTilePathMKTileOverlayPath)path {
    DLog(@"fired");

    DLog(@"path | x:%ld y: %ld z: %ld scale: %f", (long)path.x, (long)path.y, (long)path.z, path.contentScaleFactor);

    NSString *resolvedURL = [self createWMSURLStringForTileX:path.x tileY:path.y zoomLevel:path.z];
    DLog(@"resolvedURL: %@", resolvedURL);
    return  [NSURL URLWithString:resolvedURL];
}

-(NSString *)createWMSURLStringForTileXNSInteger)tileX tileYNSInteger)tileY zoomLevelNSInteger)zoomLevel {
    NSString *WMSservice = @"wms";
    NSString *WMSversion = @"1.1.1";
    NSString *WMSrequest = @"GetMap";
    NSString *WMSMode = @"tiled";
    NSString *WMSTile = [NSString stringWithFormat"%d+%d+%d", tileX, tileY, zoomLevel];
    NSString *WMSFormat = @"image/png";
    NSString *WMSTransparancy = @"true";
    NSString *WMSSRS = @"EPSG:4326";
    NSString *WMSLayer = @"displayimg";

    DLog(@"%@", [NSString stringWithFormat"%@?map=%@&idtoken=%@&service=%@&version=%@&request=%@&SRS=%@&layers=%@&mode=%@&tile=%@&format=%@&transparent=%@", self.API, self.mapContext, self.IDToken, WMSservice, WMSversion, WMSrequest, WMSSRS, WMSLayer, WMSMode, WMSTile, WMSFormat, WMSTransparancy]);

    return [NSString stringWithFormat"%@?map=%@&idtoken=%@&service=%@&version=%@&request=%@&SRS=%@&layers=%@&mode=%@&tile=%@&format=%@&transparent=%@", self.API, self.mapContext, self.IDToken, WMSservice, WMSversion, WMSrequest, WMSSRS, WMSLayer, WMSMode, WMSTile, WMSFormat, WMSTransparancy];

}

-(void)loadTileAtPathMKTileOverlayPath)path resultvoid (^)(NSData *, NSError *))result {
    DLog(@"fired");

    NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) {
            result (nil, connectionError);
        }
        else {
            result (data, nil);
        }
    }];
}

@end

我正在生成磁贴请求的 URL:

DEBUG | -[WMSTileOverlay URLForTilePath:] | path | x:60597 y: 92299 z: 18 scale: 1.000000
DEBUG | -[WMSTileOverlay createWMSURLStringForTileX:tileY:zoomLevel:] | https://WMS_SERVER?map=yupv9bvejd15fbmc0i1k62bvmdsbcafn.map&idtoken=852a468e65e5ed4023e7b704ea8c768f53c2df13da44e2f0c68fb5a559a130c8&service=wms&version=1.1.1&request=GetMap&SRS=EPSG:4326&layers=displayimg&mode=tiled&tile=60597+92299+18&format=image/png&transparent=true

我正在取回数据(它与我使用有效的旧 MKOverlay 类进行的 URL 调用相同)并且该类正在传回数据。但是没有绘制瓷砖。我这样创建类:

WMSTileOverlay *wmsTileOverlay = [[WMSTileOverlay alloc] initWithAPI:functionCall.API IDToken:functionCall.IDToken andMapContext:[self.mapper fetchMapContextForState:self.activeBoundarier.state]];
wmsTileOverlay.canReplaceMapContent = FALSE;

[self.mapView insertOverlay:wmsTileOverlay atIndex:0];

并实现了方法mapView:rendererForOverlay::

-(MKOverlayRenderer *)mapViewMKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    DLog(@"fired");

    if ([overlay isKindOfClass:[MKTileOverlay class]]) {
        MKTileOverlay *tileOverlay = (MKTileOverlay *)overlay;

        if ([tileOverlay isKindOfClass:[WMSTileOverlay class]]) {
            WMSTileOverlay *wmsTileOverlay = (WMSTileOverlay *)tileOverlay;
            DLog(@"wmsTileOverlay.URLTemplate: %@", wmsTileOverlay.URLTemplate);

            MKTileOverlayRenderer *renderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:tileOverlay];

            return renderer;
        }
    }

    return nil;
}

我做错了什么?



Best Answer-推荐答案


您是否设置了您的 MKMapView 委托(delegate)并被 mapView:rendererForOverlay: 调用?

我做的事情和你几乎一样,而且对我来说效果很好。

#pragma mark - UIViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    WMSTileOverlay *overlay = [WMSTileOverlay new];
    overlay.canReplaceMapContent = YES;

    [self.mapView addOverlayverlay level:MKOverlayLevelAboveLabels];
}

#pragma mark - MKMapViewDelegate

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[WMSTileOverlay class]]) {
        return [[MKTileOverlayRenderer alloc] initWithTileOverlayverlay];
    }

    return nil;
}

还有,

- (NSURL *)URLForTilePath:(MKTileOverlayPath)path
{
    double left, bottom, right, top;

    // Conversion to bbox mechanism
    // ...

    NSString *urlString = [NSString stringWithFormat"%@&bbox=%f,%f,%f,%f", kWMSServerURL, left, bottom, right, top];

    return [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}

- (void)loadTileAtPath:(MKTileOverlayPath)path result:(void (^)(NSData *data, NSError *error))result
{
    if (!result) {
        return;
    }

    // Cache
    // ...
    if (cache) {
        // ...      
    } else {
        NSURLRequest *request = [NSURLRequest requestWithURL:[self URLForTilePath:path]];
        [NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            // Caching mechanism
            // ...

            result(data, connectionError);
        }];
    }
}

关于ios - 子类化的 MKTileOverlay 不在 map 上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26763085/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap