Highlight

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

2012-03-27

Quick & easy navigation.

String sql = "select name, address, zip, city, country, phone, url"
+ " from retailer"
+ " where latitude is not null and longitude is not null"
+ " order by pow(lat-lat2, 2) + pow(2 * min(abs(lon-lon2), 360-abs(lon-lon2)), 2) asc limit 10";
stmt = con.prepareStatement(sql);
stmt.setDouble(1, Double.parseDouble(request.getParameter("lat")));
stmt.setDouble(2, Double.parseDouble(request.getParameter("lng")));
Honolulu to Los Angeles and San Fransisco in Python:
>>> lat, long = (21.3069444, -157.8583333)
>>> lat2, lon2 = (34.0522342, -118.2436849)
>>> pow(lat-lat2, 2) + pow(2 * min(abs(lon-lon2), 360-abs(lon-lon2)), 2)
33839.327855007934
>>> lat2, lon2 = (37.7749295, -122.4194155)
>>> pow(lat-lat2, 2) + pow(2 * min(abs(lon-lon2), 360-abs(lon-lon2)), 2)
30952.629658700374
2 * lon is ok for latitude 45 or -45, but 1/cos(radians(lat)) is better.

2012-02-23

Setting & getting web browser cookies

Details here.

Client-side JavaScript/ ECMAScript:

function createCookie(name, value, days){
  if (days){
    var date = new Date()
    date.setTime(date.getTime()+(days*24*60*60*1000))
    var expires = "; expires="+date.toGMTString()
  }
  else var expires = ""
  document.cookie = name+"="+encodeURIComponent(value)+expires+"; path=/"
}
createCookie("best_location", "/this folder; it's short + great!", 3650)

Server-side Java in JSP using Liferay:

<%@page import="com.liferay.portal.kernel.util.HtmlUtil" %>
<%@page import="java.net.URLDecoder" %>
<%
Cookie cookies[] = request.getCookies();
if (cookies != null){
  for (Cookie cookie : cookies){
    if (cookie.getName().equals("best_location")) out.print("Best location: " + HtmlUtil.escape(URLDecoder.decode(cookie.getValue())));
  }
}
%>