【问题标题】:Google Maps Ground Overlay not caching Django served dynamic imageGoogle Maps Ground Overlay 不缓存 Django 提供的动态图像
【发布时间】:2013-02-06 22:46:00
【问题描述】:

我在 javascript 中设置了一个谷歌地图叠加层(v 3.10)来显示在服务器上生成的图像:

var overlay = new google.maps.GroundOverlay(url, MAP_BOUNDS, {map: gmap});

当 url 指向静态 url 时,覆盖效果很好。但是,当它指向 Django (v 1.4) 动态 url 时,图像会正确显示,但 它会在地图滚动时不断向 url 发出服务器请求。 例如:

url = 'localhost:8000/abcdef/maps/getMap'

在 Django urls.py 中:

url(r'^(?P<key>\S+)/maps/getMap/$', 'maps.views.getMap'),

网址重定向到此代码:

@csrf_exempt
def getMap(request, key):

    response = HttpResponse(mimetype="image/png")

    im=Image.open('media/maps' + id + '/map.png')
    im.save(response, 'PNG')

    return response

因此,在我看来,谷歌地图/浏览器出于缓存目的处理图像的方式存在一些差异,可能与 HTTP 标头中的某些内容有关。我怎样才能使它在检索到图像后不会继续发出服务器请求?

通过检查浏览器控制台中的响应标头,静态图像似乎具有“Content-Length”和“Last-Modified”字段,而动态图像没有。不确定这是否相关。

感谢任何帮助。

【问题讨论】:

    标签: django caching google-maps-api-3 http-caching django-caching


    【解决方案1】:

    原来解决方案比较简单:

    response = HttpResponse(image_data, mimetype="image/png")
    response['Cache-Control'] = 'max-age=86400'
    return response
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 2011-02-21
      • 2017-12-26
      • 1970-01-01
      • 2023-04-09
      • 2011-08-04
      • 2010-11-07
      相关资源
      最近更新 更多