3

我一直在尝试使用新的 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”命令在命令提示符下运行它时,输出正确。

4

2 回答 2

2

所以我在这个完全相同的问题上苦苦挣扎,但是在.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

希望这可以帮助!

于 2012-11-09T23:24:52.403 回答
1

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

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"]
于 2012-11-23T09:07:02.063 回答