Merge pull request #32 from Luzifer/allow-disable-of-attribution-rendering

Allow override of attribution rendering
This commit is contained in:
Florian Pigorsch 2018-04-04 20:51:16 +02:00 committed by GitHub
commit 320790ed53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,6 +42,8 @@ type Context struct {
userAgent string
tileProvider *TileProvider
overrideAttribution *string
}
// NewContext creates a new instance of Context
@ -147,6 +149,14 @@ func (m *Context) ClearOverlays() {
m.overlays = nil
}
// OverrideAttribution sets a custom attribution string (or none if empty)
//
// Pay attention you might be violating the terms of usage for the
// selected map provider - only use the function if you are aware of this!
func (m *Context) OverrideAttribution(attribution string) {
m.overrideAttribution = &attribution
}
func (m *Context) determineBounds() s2.Rect {
r := s2.EmptyRect()
for _, marker := range m.markers {
@ -380,18 +390,23 @@ func (m *Context) Render() (image.Image, error) {
img, image.Point{trans.pCenterX - int(m.width)/2, trans.pCenterY - int(m.height)/2},
draw.Src)
attribution := m.tileProvider.Attribution
if m.overrideAttribution != nil {
attribution = *m.overrideAttribution
}
// draw attribution
if m.tileProvider.Attribution == "" {
if attribution == "" {
return croppedImg, nil
}
_, textHeight := gc.MeasureString(m.tileProvider.Attribution)
_, textHeight := gc.MeasureString(attribution)
boxHeight := textHeight + 4.0
gc = gg.NewContextForRGBA(croppedImg)
gc.SetRGBA(0.0, 0.0, 0.0, 0.5)
gc.DrawRectangle(0.0, float64(m.height)-boxHeight, float64(m.width), boxHeight)
gc.Fill()
gc.SetRGBA(1.0, 1.0, 1.0, 0.75)
gc.DrawString(m.tileProvider.Attribution, 4.0, float64(m.height)-4.0)
gc.DrawString(attribution, 4.0, float64(m.height)-4.0)
return croppedImg, nil
}