1

所以,我有一个在 IBOutlet 中声明的谷歌地图

@IBOutlet weak var mapView: GMSMapView!

我像这样在 idleAtCameraPosition 函数中创建我的 GMSSyncTileLayer

func mapView(mapView: GMSMapView!, idleAtCameraPosition position: GMSCameraPosition!) {

    var layer = TestTileLayer()
    layer.map = mapView
}

这是我的 GMSSyncTileLayer

class TestTileLayer: GMSSyncTileLayer {
    //MainActivityX() is the name of the View Controller my GMSMapView is in ^
    let mainActivity = MainActivityX()

override func tileForX(x: UInt, y: UInt, zoom: UInt) -> UIImage! {

    NSLog("X: \(x)")
    NSLog("Y: \(y)")

    //This is nil :(
    NSLog("mapView: \(mainActivity.mapView)")

    var boost:Float = 1.00
    if let heatmap: UIImage = LFHeatMap.heatMapForMapView(mainActivity.mapView, boost:boost, locations:locations as[AnyObject]) {
        NSLog("heatmap")
        return heatmap
    }
    else {
        NSLog("kGMSTileLayerNoTile")
        return kGMSTileLayerNoTile
    }
}

这是我的日志摘录——

2015-06-07 22:18:15.586 Hightide[7011:2387342] Y: 3133
2015-06-07 22:18:15.586 Hightide[7011:2387342] mapView: nil
2015-06-07 22:18:15.587 Hightide[7011:2387344] kGMSTileLayerNoTile
2015-06-07 22:18:15.587 Hightide[7011:2387344] X: 2339

我的地图视图为零:(

有人可以告诉我如何将我的 mapView 传递给这个 GMSSyncTileLayer。

或者告诉我如何使用 x 和 y 来获得等效的 'mapView.projection' ?因为无论如何这就是我真正需要的 mapView 。

这些 x 和 y 值对我来说也没有任何意义......

尽管我阅读了有关Tile Layers的文档

在此处输入图像描述

^根据我为 X 和 Y 获得的值,这没有意义

2015-06-07 22:18:15.586 Hightide[7011:2387342] Y: 3133  ???
2015-06-07 22:18:15.587 Hightide[7011:2387344] X: 2339  ???

我仍然很难理解这些 x 和 y 值代表什么。当我一直缩小时,它们会变为零,当我放大时,它们会越来越高。我知道这一点......但不明白这些价值观的含义。

我真的在坐标 (2339,3133) 吗?它在我地图的中心吗?我在这里看什么?

谢谢!

4

1 回答 1

1

您需要返回通常由支持 x、y、缩放格式而不是 lat、lng 的 api 生成的平铺图像。

我们使用天气 api:

https://mycompany.madesmart.nl/fcst/get?zoom=4&x=3&y=5&type=precipitation_pressure&id=1566&token=MYTOKEN




@interface SNGMSSyncTileLayer : GMSSyncTileLayer

@end

执行

- (UIImage *)tileForX:(NSUInteger)x y:(NSUInteger)y zoom:(NSUInteger)zoom {
    NSLog(@"tileForX:(%lu) y:(%lu) zoom:(%lu)", x , y, zoom);
    UIImage * imageFrame = nil;

    //----------------------------------------------------------------------------------------
    //v2 - get tile image - problem - no caching will get same tile if you move away then back over it
    //----------------------------------------------------------------------------------------
    NSString *weatherType = @"cloudcoverage_pressure";
    weatherType = @"precipitation_pressure";

    NSString *urlString =
                    [NSString stringWithFormat:@"https://mycompany.madesmart.nl/fcst/get?zoom=%lu&x=%lu&y=%lu&type=%@&id=1566&token=MYTOKEN,
                                     (unsigned long)zoom,   /* %lu */
                                     (unsigned long)x,      /* %lu */
                                     (unsigned long)y,      /* %lu */
                                        weatherType
                     ];

    //----------------------------------------------------------------------------------------
    //V1 - DOWNLOAD SYNC
    //----------------------------------------------------------------------------------------
    //SYNCHRONOUS CALL IS OK AND REQUIRED
    // - the outer method is called async and result cached
    // - we need to return an UIImage in this method ANYWAY so have to wait for a result
    //----------------------------------------------------------------------------------------
    //OK but not cahced and redraws frame on itself
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];

    UIImage *sourceImage = [UIImage imageWithData: data];

    return imageFrame;

}

打电话

SNGMSSyncTileLayer *layer = [[SNGMSSyncTileLayer alloc] init];
// Display on the map at a specific zIndex
layer.zIndex = 100;
layer.opacity = 0.1;
layer.map = self.mapView;
于 2017-03-07T16:06:34.800 回答