require 'ipdb/location' module Ipdb class Map # # Creates a new Map object. # # @param [Array] addresses/domians # An array of addresses or domains. # # @param [Hash] options The options to create a Map with. # # @option options [Integer] :timeout ('10') The Timeout. # @option options [Symbol] :units (':px') The units for width and height. # @option options [Integer] :width ('600') The width of the Map. # @option options [Integer] :height ('350') The height of the Map. # @option options [Integer] :zoom ('10') The zoom of the Map. # @option options [String] :div_id ('map_canvas') The div id of the Map container. # @option options [String] :div_class ('ipdb') The div class of the Map container. # # @return [MAP] map The newly created Map Object. # # @example # Ipdb::Map.new(@ips, :zoom => 1, :width => 100, :height => 100, :units => :%) def initialize(addr=nil, options={}) @timeout = options[:timeout] || 10 @units = (options[:units] || :px).to_sym @width = options[:width] || 600 @height = options[:height] || 350 @zoom = options[:zoom] || 10 @div_id = options[:div_id] || 'map_canvas' @div_class = options[:div_class] || 'ipdb' ip = [addr].flatten @url = "#{SCRIPT}?ip=#{URI.escape(ip.join(','))}" @xml = Nokogiri::XML.parse(open(@url)).xpath('//Location') end # # Return a Google Map for a Query Object # # @params [Boolean] js ('true') Exclude js with map if false. # # @return [String] map The Google Map html and JavaScript. # def render(js=true) @map = ""; @id = 0 @xml.each do |x| location = Location.new(x, @timeout) id = "ip_#{@id += 1}_id" @start = new_location(location.latitude, location.longitude) if @id == 1 @map += add_marker(id, location.address, location.latitude, location.longitude) @map += add_window(id, location.address, location.city, location.country) @map += add_listener(id) end if js build_map(@start, @map) else build_map_without_js(@start, @map) end end def render_js @map = ""; @id = 0 @xml.each do |x| location = Location.new(x, @timeout) id = "ip_#{@id += 1}_id" @start = new_location(location.latitude, location.longitude) if @id == 1 @map += add_marker(id, location.address, location.latitude, location.longitude) @map += add_window(id, location.address, location.city, location.country) @map += add_listener(id) end build_map_just_js(@start, @map) end def render_div "
" end private # # Build the Google maps options hash. # # @private # def build_options(start) "var myOptions = {zoom: #{@zoom},center: #{start},mapTypeId: google.maps.MapTypeId.ROADMAP};var map = new google.maps.Map(document.getElementById('#{@div_id}'), myOptions);" end # # Build a new Google Map Location object. # # @param [Float] latitude The new location objects latitude. # @param [Float] longitude The new location objects longitude. # # @private # def new_location(latitude,longitude) "new google.maps.LatLng(#{latitude}, #{longitude})" end # # Build a Google Map marker for the Location. # # @param [String] id The Location id. # @param [String] address The Location address. # @param [Float] latitude The new location objects latitude. # @param [Float] longitude The new location objects longitude. # # @private # def add_marker(id,address,latitude,longitude) "var marker_#{id} = new google.maps.Marker({position: new google.maps.LatLng(#{latitude}, #{longitude}), map: map, title: '#{address}'});" end # # Build a Google Map Window for the Location. # # @param [String] id The Location id. # @param [String] address The Location address. # @param [String] city The location city. # @param [String] country The location country. # # @private # def add_window(id, address, city, country) "var infowindow_#{id} = new google.maps.InfoWindow({content: 'IP Address: #{address}