PlaceEngineから位置を取得してGoogle Earthで表示

ここでは、PlaceEngineサービスを利用して無線LAN電波から現在位置情報を取得して、Google Earthに表示するサンプルを紹介したいと思います。 PlaceEngineの仕組み等に関してはブログ記事「PlaceEngineの仕組み」をご覧下さい。


#!/usr/bin/ruby

require 'socket'
require 'net/http'
require 'win32ole'

appk = String.new


appk='アプリケーションキーを入れてください'

now = Time.now.to_i

http=Net::HTTP.new('localhost', 5448)
req= Net::HTTP::Get.new("/rtagjs?t=#{now}&appk=#{appk}")

rtag=''
num_ap=0
result=-100
time=0

begin
  res = http.request(req)
  #puts res.body
  if res.body =~ /^recvRTAG\((.*)\)/
    message = $1
    rtagq, num_ap, time = message.split(',') 
    if rtagq =~ /^\"(.*)\"$/
      rtag = $1
    end
    puts rtag
    puts num_ap
    result = num_ap.to_i
    #puts time
  end
rescue Errno::ECONNREFUSED
  puts "Cannot access PlaceEngine Client"
  exit
end

puts "accessing server..."

ge = WIN32OLE.new('GoogleEarth.ApplicationGE')
cam = WIN32OLE.new('GoogleEarth.CameraInfoGE')

if ARGV.size > 0
    cam.FocusPointLatitude = 35
    cam.FocusPointLongitude = 135
    cam.Range = 10000000
    cam.Tilt = 0
    ge.SetCamera(cam, 1)
  exit
end

if result > 0
  peserver=Net::HTTP.new('www.placeengine.com', 80)
  req2= Net::HTTP::Get.new("/api/loc?t=#{now}&rtag=#{rtag}&appk=#{appk}&fmt=json")
  res2 = peserver.request(req2)
  if res2.body =~ /\[(.*),(.*),(.*),\{(.*)\}\]/
    x = $1
    y = $2
    range = $3
    info = $4
    puts "#{x} #{y}"
    puts "#{range}"
    puts "#{info}"
    x = x.to_f
    y = y.to_f
      cam.FocusPointLatitude = y
      cam.FocusPointLongitude = x
      cam.Range = 300
      cam.Tilt = 40
      ge.SetCamera(cam, 0.3)
  end
elsif result == -1
  puts "Cannot access Wi-Fi device" 
elsif result == -2
  puts "No APs found"
elsif result == -4
  puts "Client refused Wi-Fi scan"
elsif result == -5
  puts "Timeout error"
elsif result == -6
  puts "Something wrong with API key"
elsif result == -110
  puts "Can't determine location"
else
  puts "Fatal error #{result}"
end