【问题标题】:Interpolate Floats into Url将浮点数插入 URL
【发布时间】:2014-01-22 03:03:09
【问题描述】:

我正在尝试使用谷歌静态地图 api 根据一组经度和纬度点呈现居中的地图视图。

google map api 提供此 URL。

http://maps.googleapis.com/maps/api/staticmap?center=40.689249,-74.044500&zoom=12&size=400x400&maptype=roadmap&sensor=true

我正在尝试使用数据库中的值并像这样动态插入它们

<%= image_tag ('http://maps.googleapis.com/maps/api/staticmap?center=#{@location.latitude},#{@location.longitude}&zoom=12&size=400x400&maptype=roadmap&sensor=true')%>

但是生成的图像不断损坏

注意:

纬度和经度都是浮点型属性。

【问题讨论】:

    标签: ruby-on-rails google-maps-api-3 ruby-on-rails-4


    【解决方案1】:

    尝试改变这个(用单引号):

    'http://maps.googleapis.com/maps/api/staticmap?center=#{@location.latitude},#{@location.longitude}&zoom=12&size=400x400&maptype=roadmap&sensor=true'
    

    对此(双引号):

    "http://maps.googleapis.com/maps/api/staticmap?center=#{@location.latitude},#{@location.longitude}&zoom=12&size=400x400&maptype=roadmap&sensor=true"
    

    单引号是按字面意思解释的,所以你的插值可能没有被渲染

    【讨论】:

    • 太棒了,很高兴它有帮助:)
    猜你喜欢
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 2023-01-07
    相关资源
    最近更新 更多