iOS在高德地图上展示谷歌瓦片地图
特别提醒:请下载高德地图3D版本SDK,2D版本加载层级有限。
谷歌瓦片URL链接格式
https://mt2.google.cn/vt/lyrs=y&hl=zh-CN&gl=cn&x=%ld&y=%ld&z=%ld&scale=%d
mt0~mt3:对应不同的瓦片服务器;
lyrs=s:卫星图;
lyrs=y:带标签的卫星图;
x、y分别对应图片横向、纵向的序号,z对应地图缩放级别,scale对应屏幕分辨率倍数。
代码实现
@interface MATileGoogleOverlay : MATileOverlay
@end
@implementation MATileGoogleOverlay
- (NSURL *)URLForTilePath:(MATileOverlayPath)path {
NSString *tileUrl = [NSString stringWithFormat:@"https://mt2.google.cn/vt/lyrs=y&hl=zh-CN&gl=cn&x=%ld&y=%ld&z=%ld&scale=%d", path.x, path.y, path.z, (int)path.contentScaleFactor];
return [NSURL URLWithString:tileUrl];
}
- (void)loadTileAtPath:(MATileOverlayPath)pathresult:(void(^)(NSData*,NSError*))result {
NSURL *tileUrl = [self URLForTilePath:path];
//通过链接下载图片并回调(缓存已经下载的图片以便下次请求时从本地获取)
result(UIImagePNGRepresentation(downloadImg), nil);
}
@end
@interface GoogleMapViewController () <MAMapViewDelegate>
{
IBOutlet MAMapView*mapView;
}
@property (nonatomic, strong) MATileGoogleOverlay *googleOverlay;
@end
@implementation
- (void)viewDidLoad {
[super viewDidLoad];
[mapView addOverlay:self.googleOverlay];
}
- (MATileGoogleOverlay *)googleOverlay {
if (_googleOverlay == nil) {
NSString *tileUrl = [NSString stringWithFormat:@"https://mt2.google.cn/vt/lyrs=y&hl=zh-CN&gl=cn&x={x}&y={y}&z={z}&scale={scale}"];
_googleOverlay = [[MATileGoogleOverlay alloc] initWithURLTemplate:tileUrl];
_googleOverlay.minimumZ = 3;
_googleOverlay.maximumZ = 19;
_googleOverlay.boundingMapRect = MAMapRectWorld;
}
return _googleOverlay;
}
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay {
if([overlay isKindOfClass:[MATileGoogleOverlay class]]) {
MATileOverlayRenderer *tileRenderer = [[MATileOverlayRenderer alloc] initWithTileOverlay:overlay];
return tileRenderer;
}
return nil;
}
网友评论