internal/geocode: use viewport if bounds is not defined

The bounds response from google geo api is optional and for many searches not
returned at all. Use viewport in those cases.

Change-Id: Iaf0a055720fd6cc0a2c87e93b8e22315798a0b35
This commit is contained in:
Viktor 2018-08-23 19:39:47 +02:00
parent 9a49f0959d
commit e6fb16557f
1 changed files with 7 additions and 10 deletions

View File

@ -181,17 +181,14 @@ func decodeGoogleResponse(r io.Reader) (rects []Rect, err error) {
return nil, err
}
for _, res := range resTop.Results {
if res.Geometry != nil && res.Geometry.Bounds != nil {
r := res.Geometry.Bounds
if r.NorthEast.Lat == 90 && r.NorthEast.Long == 180 &&
r.SouthWest.Lat == -90 && r.SouthWest.Long == -180 {
// Google sometimes returns a "whole world" rect for large addresses (like "USA")
// so instead use the viewport in that case.
if res.Geometry.Viewport != nil {
rects = append(rects, *res.Geometry.Viewport)
}
} else {
if res.Geometry != nil {
if r := res.Geometry.Bounds; r != nil && !(r.NorthEast.Lat == 90 && r.NorthEast.Long == 180 &&
r.SouthWest.Lat == -90 && r.SouthWest.Long == -180) {
// Google sometimes returns a "whole world" rect for large addresses (like "USA"), so we only
// use the Bounds when they exist and make sense. Otherwise we use the Viewport if available.
rects = append(rects, *r)
} else if res.Geometry.Viewport != nil {
rects = append(rects, *res.Geometry.Viewport)
}
}
}