1

So, I have a google map that's declared in an IBOutlet

@IBOutlet weak var mapView: GMSMapView!

I create my GMSSyncTileLayer in the idleAtCameraPosition function like this

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

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

And this is my 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
    }
}

This is an excerpt from my logs --

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

My mapView is nil :(

Can someone please show my how I can pass in my mapView to this GMSSyncTileLayer.

Or show me how I can use x and y to to get the equivalent of 'mapView.projection' ? Because that's what I really need from the mapView anyway.

Also these x and y values do not make any sense to me...

And although I read the docs on Tile Layers

enter image description here

^This doesn't make sense based on the values I'm getting for X and 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  ???

I still have a hard time understanding what these x and y values are representing. When I'm all the way zoomed out they go to zero, when I'm zooming in they go higher and higher. I know this...But do not understand the meaning of the values.

Am I literally at coordinate (2339,3133) ? Is it in the center of my map? What am I looking at here?

Thanks!

stepheaw
  • 1,683
  • 3
  • 22
  • 35
  • What zoom level are you at when you get (2339, 3133)? I think it's saying that the tile that contains the x,y pair (2339,3133) is nil. – not_a_bot Jun 09 '15 at 18:13
  • Im at zoom level 13. How can I use this x,y pair and zoom for something useful? Can I get the equivalent of mapView.projection with these values you think? – stepheaw Jun 11 '15 at 01:43
  • "Am I literally at coordinate (2339,3133) ? Is it in the center of my map? What am I looking at here?" The data are correct. [Look here](http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/) If you're as closely as possible you'll see similar data – Roman Sep 10 '15 at 10:45

1 Answers1

1

You need return an tile image usually generated by an api that supports x, y, zoom format instead of lat, lng.

We use a weather api:

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




@interface SNGMSSyncTileLayer : GMSSyncTileLayer

@end

Implementation

- (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;

}

TO CALL

SNGMSSyncTileLayer *layer = [[SNGMSSyncTileLayer alloc] init];
// Display on the map at a specific zIndex
layer.zIndex = 100;
layer.opacity = 0.1;
layer.map = self.mapView;
brian.clear
  • 5,277
  • 2
  • 41
  • 62