Tuesday, October 5, 2010

Google Maps distanceFrom in MySQL

So, step two of the website I was working on here was to actually search my MySQL database for points within a given distance of a second point. I simply converted the Haversine formula to a MySQL query:

SELECT *
FROM Point
WHERE
6378137
*
(
2
*
ATAN2(
SQRT(
SIN((Point.Lat - 59.332526) * PI() / 180)
*
SIN((Point.Lat - 59.332526) * PI() / 180)
+
COS((59.332526 * PI() / 180))
*
COS((Point.Lat * PI() / 180))
*
SIN(((Point.Lng - 18.064091) * PI() / 180) / 2)
*
SIN(((Point.Lng - 18.064091) * PI() / 180) / 2)
),
SQRT(
1
-
(
SIN((Point.Lat - 59.332526) * PI() / 180)
*
SIN((Point.Lat - 59.332526) * PI() / 180)
+
COS((59.332526 * PI() / 180))
*
COS((Point.Lat * PI() / 180))
*
SIN(((Point.Lng - 18.064091) * PI() / 180) / 2)
*
SIN(((Point.Lng - 18.064091) * PI() / 180) / 2)
)
)
)
) <= 300 -- 300 meters

In the above example I use the LatLng position of central Stockholm (59.332526, 18.064091) as the base point and select all points within a 300-meter radius from it. Of course this can be simplified, but you get the Point.

Wednesday, September 15, 2010

Google Maps API V3 - distanceFrom method

I recently started upgrading a website, which uses Google Maps, from V2 to V3. On one page I needed to calculate the distance between two GLatLngs. This was easily done in V2 with the GLatLng.distanceFrom method. Although this method seems to have disappeared when V3 was released, so I created it:

google.maps.LatLng.prototype.distanceFrom = function(p2) {
var R = 6378137; // earth's mean radius in meters (this was a parameter in V2)
var rad = function(x) {return x*Math.PI/180;}
var dLat = rad(p2.lat() - this.lat());
var dLong = rad(p2.lng() - this.lng());

var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(rad(this.lat())) * Math.cos(rad(p2.lat())) * Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;

return d.toFixed(3);
}


Using the Haversine formula (found here) I created a custom method within the LatLng class. I altered it a little in order to get the distance in meters instead of kilometres. Seems to work as intended!

Monday, May 31, 2010

EMAILProtect: Secure e-mail addresses in Wordpress

I wrote a plugin for Wordpress which secures e-mail addresses from being found by spam spiders (and other crawlers).

The plugin rewrites e.g. "something@something.com" to a JavaScript. The script displays a clickable link for the user, but the original string is hidden from "non-JavaScript crawlers".

Instead of updating the actual script here - and on other sites - will I only be using the Wordpress SVN.

I will, however, be answering questions and taking suggestions through the comments.

Wordpress plugin site:
http://wordpress.org/extend/plugins/emailprotect/

Fun fact #1: In less than one week on Wordpress, EMAILProtect was downloaded more than 1000 times.

Wednesday, May 5, 2010

Display hidden files in Finder

You may have been in the situation where you want to display hidden files in Finder. I've found that necessary when I want to edit system files e.g. ".htaccess" .

There's a bunch of applications and script to do this but what I've noticed is that everyone of these are unnecessarily advanced.

What I did is that I developed a _simple_ AppleScript application which you execute to display hidden files and when/if you want to hide them you just execute it again.

set currentStatus to (do shell script "defaults read com.apple.Finder AppleShowAllFiles")

if currentStatus is "YES" then
do shell script "defaults write com.apple.Finder AppleShowAllFiles NO"
else
do shell script "defaults write com.apple.Finder AppleShowAllFiles YES"
end if

do shell script "KillAll Finder"


Download file
Mirror site #1
Mirror site #2