Is it possible to check if a part of GMSPath is in a visible region?
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");
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With