环境

  • Mapbox v10.5.0
  • Xcode 14.3.1

HttpServiceInterface自定义网络请求

设置网络请求类

MapboxCommon.HttpServiceFactory.setUserDefinedForCustom(HttpServiceInterface)

实现网络请求类

MapboxCommon.HttpServiceFactory.getInstance()返回的是自定义请求类,所以不能直接返回网络请求

class CustomHttpServiceInterface:HttpServiceInterface{
public func setInterceptorForInterceptor(_ interceptor: HttpServiceInterceptorInterface?) {

}

public func setMaxRequestsPerHostForMax(_ max: UInt8) {

}

public func request(for request: HttpRequest, callback: @escaping HttpResponseCallback) -> UInt64 {

}

public func cancelRequest(forId id: UInt64, callback: @escaping ResultCallback) {

}

public func supportsKeepCompression() -> Bool {

}

public func download(for options: DownloadOptions, callback: @escaping DownloadStatusCallback) -> UInt64 {

}

}

HttpServiceInterceptorInterface拦截修改网络请求

设置网络请求代理

MapboxCommon.HttpServiceFactory.getInstance().setInterceptorForInterceptor(HttpServiceInterceptorInterface)

实现网络请求代理

class HttpServiceHack:HttpServiceInterceptorInterface{
public func onRequest(for request: HttpRequest) -> HttpRequest {
//修改请求
return request
}

public func onDownload(forDownload download: DownloadOptions) -> DownloadOptions {
return download
}

public func onResponse(for response: HttpResponse) -> HttpResponse {
//修改返回
return response
}
}

N个瓦片涂层合并

需求

N个本地地图包和M个在线地图包链接,根据时间叠加,显示为一个layer图层

实现

  1. 自定义local:///localtiles?x={x}&y={y}&z={y}链接标识 (这里不能使用file:///,因为不能拦截)
  2. 修改设置HttpServiceInterceptorInterface,在onRequest中修改HttpRequest到本地文件链接,或者在前请求图片保存为本地后,再设置本地文件链接
  3. 因为本地图片请求,返回码是0,所以需要在onResponse中将,将返回码设置为200

    其他实现方式

    使用第三方库GCDWebServer,作为中间人,进行请求拦截,自定义返回