ruby - 通过 Ruby 实现 Google Maps Geolocation API

标签 ruby google-maps geolocation

我一直在尝试使用新的 Geolocation API。我也有一个 API key 。 但不知何故,输出为“未找到”。 有人可以告诉我错误在哪里吗?

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}]} Not Found Not Found

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

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

最佳答案

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

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

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

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

希望这对您有所帮助!

关于ruby - 通过 Ruby 实现 Google Maps Geolocation API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13061588/

相关文章:

ruby-on-rails - 有没有办法在带有 rails 的 yaml 中引用常量?

google-maps - Ionic native Google map 无法正确显示

ruby-on-rails - 使用 will_paginate 仅显示单个页面的分页

ruby - Ruby Mechanize 的名称字段空白

javascript - 谷歌地图点击标记平移到屏幕底部

javascript - 从谷歌地图外部打开信息窗口

android - Cordova 地理定位精度上限为 10 米

javascript - 开放层 : Attaching an ground layer image over an large building

javascript - AngularJS 中的半正矢公式和地理定位

ruby-on-rails - ruby rails : Server error with while trying to upload a file using attachment_fu: (can't convert nil into Integer)