Monday, October 25, 2010

SHARE - SpotCrime feed api integration

One of my client want to feed in the spotcrime map to one page for each his community site.
Each community have info about country, state/province, city, zipcode/postal code.
We can use this info for search it on google map.

First we need add this to your view

<%= javascript_include_tag('spotcrime/maps.js', 'spotcrime/main.js', 'spotcrime/spotcrime.js') %>


add this to view

<script type="text/javascript">
/* <[[CDATA] */
function initializeMap() {
SpotCrime.newsEmbed(
"map", //DOM that you want to add map
[<%= @lat %>, <%= @lng %>, 14], //latitude, longitude, zoom
[600,473], //width, height map
"<b><%= community.zipcode %><\/b>",
"<p style='font-size: smaller;'>Recent incident reports, MPD dispatches, and local crime news in the <%= community.zipcode %> zip code area.<\/p><br\/><br\/>");
}
/* ]> */
</script>



add this to your view

<div id="map"></div>


we need to get latitude and longitude for our community

add this to your controller

require 'open-uri'
require 'openssl'

url = "http://maps.googleapis.com/maps/api/geocode/xml?address=#{((community.address.to_s+' ')+(community.city.to_s+' ' if community.city)+(community.state.to_s+' ' if community.state)+(community.zipcode.to_s if community.zipcode)).gsub(' ', '+')}&sensor=true"
xml = open(url).read
hash = Hash.from_xml(xml)
if hash['GeocodeResponse'] and hash['GeocodeResponse']['result'] and hash['GeocodeResponse']['result']['geometry'] and hash['GeocodeResponse']['result']['geometry']['location']
@lat= hash['GeocodeResponse']['result']['geometry']['location']['lat'] if hash['GeocodeResponse']['result']['geometry']['location']['lat']
@lng= hash['GeocodeResponse']['result']['geometry']['location']['lng'] if hash['GeocodeResponse']['result']['geometry']['location']['lng']
end


for the js file you can ask me about it.