Showing posts with label js. Show all posts
Showing posts with label js. Show all posts

Friday, February 11, 2011

SHARE - Event handler for page refresh and back button


<input type="hidden" id="refreshed" value="no"/>
<script type="text/javascript">
onload=function(){
var e=document.getElementById("refreshed");
if(getCookie('location_url')==location.href){
e.value="no";
}
setTimeout('setCookie(\'location_url\',location.href);', 100)
if(e.value=="no"){ //page loaded on the first time
e.value="yes";
}else{ //page refreshed or back to this page
e.value="no";
//run your code for the event here
e.value="yes";
}
}
onkeypress=function(event){ //page refreshed
if(event.keyCode==116){
var e=document.getElementById("refreshed");
e.value="no";
}
}
onkeydown=function(event){ //page refreshed
if(event.ctrlKey && event.keyCode == 82){
var e=document.getElementById("refreshed");
e.value="no";
}
}
</script>

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.