【问题标题】:Google Maps Geolocation API implementation through Ruby通过 Ruby 实现 Google Maps Geolocation API
【发布时间】:2012-10-15 05:21:47
【问题描述】:

我一直在尝试使用新的 Geolocation API。我也有一个 API 密钥。 但不知何故,输出显示为“未找到”。 谁能告诉我错误在哪里?

require 'net/http'
require 'uri'
require 'json'
require 'httparty'


lac=50039
mnc=86
cid=15471
mcc=404
rssi=-69


    cell_towers = [{:cellId => cid,
  :locationAreaCode => lac,
  :mobileCountryCode => mcc,
  :mobileNetworkCode => mnc,
  :signalStrength => rssi }] 

    param = {:cellTowers => cell_towers}
   puts param.to_json
   #puts "https://www.googleapis.com/maps/api/geolocation/v1/geolocate?key=#{api_key}"

      response = HTTParty.post("https://www.googleapis.com/maps/api/geolocation/v1/geolocate?key=my_key",
      :body => param.to_json, 
      :header => {"Content-Type" => "application/json"})
      puts response
      temp= response.body
      puts temp

上面代码给出的输出是:

{"cellTowers":[{"cellId":15471,"locationAreaCode":50039,"mobileCountryCode":404,"mobileNetworkCode":86,"signalStrength":-69}]} 未找到 未找到

Google Maps Geolocation API 文档的链接: https://developers.google.com/maps/documentation/business/geolocation/

当我手动创建一个 json 对象并使用“curl”命令在命令提示符下运行它时,输出正确。

【问题讨论】:

    标签: ruby google-maps geolocation


    【解决方案1】:

    所以我一直在努力解决这个完全相同的问题,但在 .Net 中使用了一个简单的 http 帖子...

    似乎有关 URL 的 API 文档有误。

    代替:https://www.googleapis.com/maps/api/geolocation/v1/geolocate?key=API_key

    其实是:https://www.googleapis.com/geolocation/v1/geolocate?key=API_key

    希望这会有所帮助!

    【讨论】:

    • 谢谢!我已更改网址。不再显示“未找到”错误。但我收到代码 403 的错误,显示“已超过未经身份验证使用的每日限制。继续使用需要注册。”无法弄清楚为什么。身份验证已经完成,我得到了一个 API 密钥,我也检查了 API 控制台。你能帮我解决这个问题吗?
    • 您是否通过 API 控制台存档了信用卡?由于这是一个商业 API,我相信它是必需的。之后,您应该能够每天使用分配的 100 个请求。
    • 问题解决了,我必须为 SSL 认证设置 verify_none。非常感谢!
    【解决方案2】:

    在您的帮助下,我可以解决问题。谢谢! 想我会分享代码。它可能会有所帮助。

    cell_towers = [{:cellId => cid,
                    :locationAreaCode => lac,
                    :mobileCountryCode => mcc,
                    :mobileNetworkCode => mnc,
                    :signalStrength => rssi }] 
    
    param = {:cellTowers => cell_towers}
    uri = URI.parse('https://www.googleapis.com/geolocation/v1/geolocate?key=your_key')
    
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Post.new(uri.request_uri)
    request.body=param.to_json
    request["Content-Type"]="application/json"
    response = http.request(request)
    result=response.body
    res=JSON.parse(result)
    lat=res["location"]["lat"]
    long=res["location"]["lng"]
    

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 2013-07-15
      • 2020-08-11
      • 2017-12-29
      • 1970-01-01
      • 2014-11-05
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多