Reputation: 75
I have been trying to use the new Geolocation API. I got an API key too. But somehow, the output is given as 'Not found'. Can someone please tell me where the error is?
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
The output the above code gives is :
{"cellTowers":[{"cellId":15471,"locationAreaCode":50039,"mobileCountryCode":404,"mobileNetworkCode":86,"signalStrength":-69}]} Not Found Not Found
The link to the documentation of Google Maps Geolocation API : https://developers.google.com/maps/documentation/business/geolocation/
When I create a json object manually and run it on command prompt using 'curl' command, the output is coming out right.
Upvotes: 3
Views: 3084
Reputation: 75
With all your help, I could solve the issue.Thanks! Thought I'll share the code.It might be helpful.
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"]
Upvotes: 1
Reputation: 305
So I was struggling with this exact same issue, but using a simple http post in .Net...
Seems the API documentation concerning the URL is wrong.
Instead of: https://www.googleapis.com/maps/api/geolocation/v1/geolocate?key=API_key
It's actually: https://www.googleapis.com/geolocation/v1/geolocate?key=API_key
Hope this helps!
Upvotes: 2