Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdonthemic committed Apr 25, 2011
0 parents commit 69ab531
Show file tree
Hide file tree
Showing 46 changed files with 2,192 additions and 0 deletions.
51 changes: 51 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
===========================
Setup:
===========================

Install apex-lang 1.17 managed package https://login.salesforce.com/?startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t80000000jMfG

Deploy code to org

Obtain Simplegeo oath key and secret key

Go to Setup | App Setup | Develop | Custom Settings
Click Manage for Global Variables
Click New
For name, enter SimpleGeo.OAuthKey and in the Value field, enter your oauth key
Click Save
Click New
For name, enter SimpleGeo.SecretKey and in the Value field, enter your secret key
Click Save

Add a Remote Site to: http://api.simplegeo.com

Create some accounts with valid Billing Addresses in the United States (that's all Simplegeo supports)

Run following anonymously (to geocode accounts):

System.debug( //after running this command, the batch apex job ID will be output to the log
GeocodeService.geocodeEnMasse(
'BillingAddress__c' //address field name
,'Latitude__c' //the latitude field to update
,'Longitude__c' //the longitude field to update
,'Account' //the object name which contains the latitude and longitude fields
,null //criteria; null means attempt to geocode all records on the object
,'richard.vanhook@gmail.com' //optional email to notify when complete
)
);

Once that's complete, go to VF page GeocodeSearchNearbyDemo and execute searches.

===========================
Notes:
===========================
Since the idea is to create a toolkit, my main goal
was to make my code reusable for any SObject (not
just Account) and also define an easy way for
other geocoding service to be plugged in. The main
class is GeocodeService and I suggest the following
methods in that class as a starting place for review:

GeocodeService.geocode()
GeocodeService.geocodeEnMasse()
GeocodeService.findNearbyRecords()
23 changes: 23 additions & 0 deletions eclipse/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>richard.vanhook@simplegeo.demo.com_DE</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.salesforce.ide.builder.online</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.salesforce.ide.builder.default</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.salesforce.ide.nature.default</nature>
<nature>com.salesforce.ide.nature.online</nature>
</natures>
</projectDescription>
13 changes: 13 additions & 0 deletions eclipse/.settings/com.salesforce.ide.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#Fri Apr 15 07:14:07 CDT 2011
eclipse.preferences.version=1
endpointApiVersion=20.0
endpointEnvironment=Production/Developer Edition
endpointServer=www.salesforce.com
httpsProtocol=true
ideVersion=20.0
keependpoint=false
metadataFormatVersion=20.0
namespacePrefix=
packageName=unpackaged
readTimeout=400
username=richard.vanhook@simplegeo.demo.com
60 changes: 60 additions & 0 deletions eclipse/src/classes/GeoBounds.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* ============================================================
* This code is part of Richard Vanhook's submission to the
* Cloudspokes Geolocation Toolkit challenge.
*
* This software is provided "AS IS," and you, its user,
* assume all risks when using it.
* ============================================================
*/
global class GeoBounds {

global static final GeoBounds MAX = new GeoBounds(
new GeoPoint(-90,-180)
,new GeoPoint(90,180)
);

global GeoPoint northeast {get;set;}
global GeoPoint southwest {get;set;}

global GeoBounds(List<GeoPoint> points){
initialize(points);
}

global GeoBounds(GeoPoint southwest, GeoPoint northeast){
initialize(new GeoPoint[]{southwest,northeast});
}

private void initialize(List<GeoPoint> points){
if(points != null && points.size()>0){
Double minLat = 90;
Double maxLat = -90;
Double minLng = 180;
Double maxLng = -180;
for(GeoPoint point : points){
minLat = point.lat() < minLat ? point.lat() : minLat;
maxLat = point.lat() > maxLat ? point.lat() : maxLat;
minLng = point.lng() < minLng ? point.lng() : minLng;
maxLng = point.lng() > maxLng ? point.lng() : maxLng;
}
this.northeast = new GeoPoint(maxLat,maxLng);
this.southwest = new GeoPoint(minLat,minLng);
}
}

global Boolean contains(GeoPoint point){
if(point == null || northeast == null || southwest == null){
return false;
}
return
this.northeast.lat() > point.lat()
&& this.southwest.lat() < point.lat()
&& this.northeast.lng() > point.lng()
&& this.southwest.lng() < point.lng()
;
}

global String toStr(){
return '<#GeoBounds(northeast=' + (northeast==null ? null : northeast.toStr())
+ ',southwest=' + (southwest==null ? null : southwest.toStr()) + ')>';
}
}
10 changes: 10 additions & 0 deletions eclipse/src/classes/GeoBounds.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>20.0</apiVersion>
<packageVersions>
<majorNumber>1</majorNumber>
<minorNumber>17</minorNumber>
<namespace>al</namespace>
</packageVersions>
<status>Active</status>
</ApexClass>
43 changes: 43 additions & 0 deletions eclipse/src/classes/GeoBoundsTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* ============================================================
* This code is part of Richard Vanhook's submission to the
* Cloudspokes Geolocation Toolkit challenge.
*
* This software is provided "AS IS," and you, its user,
* assume all risks when using it.
* ============================================================
*/
@IsTest
private class GeoBoundsTest {

private static testmethod void test1(){
final GeoBounds bounds = new GeoBounds(new List<GeoPoint>{
new GeoPoint(1,-1)
,new GeoPoint(-1,2)
,new GeoPoint(-2,1)
});

System.assertEquals(1,bounds.northeast.lat());
System.assertEquals(2,bounds.northeast.lng());
System.assertEquals(-2,bounds.southwest.lat());
System.assertEquals(-1,bounds.southwest.lng());

System.assertEquals(true,bounds.contains(new GeoPoint(0,1)));
System.assertEquals(false,bounds.contains(new GeoPoint(3,3)));
}

private static testmethod void test2(){
final GeoBounds bounds = new GeoBounds(
new GeoPoint(1,-1)
,new GeoPoint(-1,2)
);

System.assertEquals(1,bounds.northeast.lat());
System.assertEquals(2,bounds.northeast.lng());
System.assertEquals(-1,bounds.southwest.lat());
System.assertEquals(-1,bounds.southwest.lng());

System.assertEquals(true,bounds.contains(new GeoPoint(0,1)));
System.assertEquals(false,bounds.contains(new GeoPoint(3,3)));
}

}
10 changes: 10 additions & 0 deletions eclipse/src/classes/GeoBoundsTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>20.0</apiVersion>
<packageVersions>
<majorNumber>1</majorNumber>
<minorNumber>17</minorNumber>
<namespace>al</namespace>
</packageVersions>
<status>Active</status>
</ApexClass>
42 changes: 42 additions & 0 deletions eclipse/src/classes/GeoPoint.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* ============================================================
* This code is part of Richard Vanhook's submission to the
* Cloudspokes Geolocation Toolkit challenge.
*
* This software is provided "AS IS," and you, its user,
* assume all risks when using it.
* ============================================================
*/
global class GeoPoint {

global static final Double PI = 3.1415926535897932384626433832795;

global Double latitude {get; private set;}
global Double longitude {get; private set;}

global GeoPoint (Decimal latitude, Decimal longitude){
this(latitude.doubleValue(), longitude.doubleValue());
}

global GeoPoint (Double latitude, Double longitude){
if(latitude < -90 || latitude > 90 || longitude < -180 || longitude > 180){
throw new al.IllegalArgumentException('Invalid latitude (' + latitude + ') or longitude (' + longitude + ')');
}
this.latitude = latitude;
this.longitude = longitude;
}

global Double lat() { return this.latitude; }
global Double lng() { return this.longitude; }

global Double latRadians() { return this.latitude * (PI/180); }
global Double lngRadians() { return this.longitude * (PI/180); }

global Boolean equals(GeoPoint point){
if(point == null){
return false;
}
return this.latitude == point.latitude && this.longitude == point.longitude;
}

global String toStr(){ return '<#GeoPoint(latitude=' + latitude + ',longitude=' + longitude + ')>'; }
}
10 changes: 10 additions & 0 deletions eclipse/src/classes/GeoPoint.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>20.0</apiVersion>
<packageVersions>
<majorNumber>1</majorNumber>
<minorNumber>17</minorNumber>
<namespace>al</namespace>
</packageVersions>
<status>Active</status>
</ApexClass>
43 changes: 43 additions & 0 deletions eclipse/src/classes/GeoPointTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* ============================================================
* This code is part of Richard Vanhook's submission to the
* Cloudspokes Geolocation Toolkit challenge.
*
* This software is provided "AS IS," and you, its user,
* assume all risks when using it.
* ============================================================
*/
@IsTest
private class GeoPointTest {

private static testmethod void testNormal(){
GeoPoint p1 = new GeoPoint(1.23,4.56);

System.assertEquals(p1.latitude,p1.lat());
System.assertEquals(p1.longitude,p1.lng());

System.assertEquals(p1.latitude*(GeoPoint.PI/180),p1.latRadians());
System.assertEquals(p1.longitude*(GeoPoint.PI/180),p1.lngRadians());

GeoPoint p2 = new GeoPoint(1.23,4.56);
System.assert(p1.equals(p2));
System.assert(p2.equals(p1));
GeoPoint p3 = new GeoPoint(10,10);
System.assert(p1.equals(null)==false);
System.assert(p1.equals(p3)==false);
System.assert(p3.equals(p1)==false);
System.assertEquals('<#GeoPoint(latitude=1.23,longitude=4.56)>',p1.toStr());
}

private static testmethod void testInvalidArgs(){
Boolean exceptionThrown = false;
try{GeoPoint p1 = new GeoPoint(-91,0); }catch(al.IllegalArgumentException e){exceptionThrown = true;}
System.assert(exceptionThrown);
try{GeoPoint p1 = new GeoPoint(91,0); }catch(al.IllegalArgumentException e){exceptionThrown = true;}
System.assert(exceptionThrown);
try{GeoPoint p1 = new GeoPoint(0,-181); }catch(al.IllegalArgumentException e){exceptionThrown = true;}
System.assert(exceptionThrown);
try{GeoPoint p1 = new GeoPoint(0,181); }catch(al.IllegalArgumentException e){exceptionThrown = true;}
System.assert(exceptionThrown);
}

}
10 changes: 10 additions & 0 deletions eclipse/src/classes/GeoPointTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>20.0</apiVersion>
<packageVersions>
<majorNumber>1</majorNumber>
<minorNumber>17</minorNumber>
<namespace>al</namespace>
</packageVersions>
<status>Active</status>
</ApexClass>
Loading

0 comments on commit 69ab531

Please sign in to comment.