Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps iOS SDK check if path is in visible region

Is it possible to check if a part of GMSPath is in a visible region?

like image 521
Tim Tuffley Avatar asked May 13 '14 05:05

Tim Tuffley


2 Answers

The path is made up of coordinates. And the mapview has a visible Region. You can easily check if a coordinate is in a region eithout even going to pixelspace:

- (void)checkPath:(GMSPath*)path {
    GMSVisibleRegion visibleRegion = _googleMap.projection.visibleRegion;
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion: visibleRegion];

    for(int i = 0; i < path.count; i++) {
        CLLocationCoordinate2D coordinate=[path coordinateAtIndex:i];
        if([bounds containsCoordinate:coordinate]) {
            NSLog("Visible");
        }
    }
}
like image 89
Daij-Djan Avatar answered Nov 20 '22 14:11

Daij-Djan


It depends on how accurate you need it to be.

Daij-Djan's answer uses an axis-aligned bounding box of the visible region, which will be bigger than the actual region if the view is rotated / tilted.

Sunny Shah's answer will be more accurate as it will fit exactly to the visible region of the view. However it will probably be slower as it has to project each point into screen coordinates.

Both of these answers only check if a point on the path is within the visible region. If you have a line in the path which crosses the visible region but the two vertices are outside of the visible region, both of these answers will report the path as being invisible. To solve this you would need some kind of line-vs-box collision test.

like image 1
Saxon Druce Avatar answered Nov 20 '22 13:11

Saxon Druce