API for calling SimpleGeo web service from Apex Code
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
README.md

README.md

Force.com Toolkit for SimpleGeo

This BETA toolkit provides a simple API for calling SimpleGeo from Apex Code.

See SimpleGetTest.page and SimpleGeoController.cls for a simple working example.

Example usage:

// Create a client ready to use
SimpleGeo client = new SimpleGeo();

// Get context for an address - this includes latitude, longitude, 
// postal code, state etc
SimpleGeo.Context context = 
    client.getContextFromAddress('1 Market St, San Francisco, CA');

// Get context for a point (latitude, longitude)
SimpleGeo.Context context = client.getContext(37.793755, -122.395584);

// Get context for the caller's IP address
SimpleGeo.Context context = client.getContextFromIPAddress();

// Get context for an IP address
SimpleGeo.Context context = client.getContextFromIPAddress('8.8.8.8');

// Print out some information from the context
System.debug(LoggingLevel.ERROR,'lat '+context.query.latitude+
    ', lon '+context.query.longitude+', address '+context.query.address);

// These are SimpleGeo handles
String california = 'SG_2MySaPILVQG3MoXrsVehyR_37.215297_-119.663837';
String attpark = 'SG_4H2GqJDZrc0ZAjKGR8qM4D_37.778406_-122.389506';
String belltower = 
    'SG_5JkVsYK82eLj26eomFrI7S_37.795027_-122.421583@1291796505';

// Get details on a feature (identified by its handle)
SimpleGeo.Feature feature = client.getFeature(attpark);

// Print the name of the feature
System.debug(LoggingLevel.ERROR, feature.properties.get('name').getString());

// Get all the Starbucks within 0.5km of a point
SimpleGeo.Feature[] features = client.getPlaces(37.793755, -122.395584, 
    new Map<String, String>{'radius' => '0.5', 'q' => 'Starbucks'});

// Get all the Starbucks within 0.5km of an address
SimpleGeo.Feature[] features = 
    client.getPlacesNearAddress('1 Market St, San Francisco, CA', 
    new Map<String, String>{'radius' => '0.5', 'q' => 'Starbucks'});

// Get all the Starbucks within 10km of an IP address
SimpleGeo.Feature[] features = client.getPlacesNearIPAddress('8.8.8.8', 
    new Map<String, String>{'radius' => '10', 'q' => 'Starbucks'});

// Print the addresses of a list of features
for ( SimpleGeo.Feature feature: features ) {
    System.debug(LoggingLevel.ERROR, 
            feature.properties.get('address').getString());
}

// Put a record in a layer
SimpleGeo.Feature feature = new SimpleGeo.Feature();
feature.geometry = new SimpleGeo.Point(latitude, longitude);
feature.properties = new Map<String, SimpleGeo.PropertyValue>();
feature.properties.put('name', new SimpleGeo.PropertyValue(name));
client.putRecord('my.layer.name', id, feature);

// Get records close to a given point
List<Record> records = getNearby('my.layer.name', latitude, longitude, radius);

// Low level 'raw' API access - useful for returning JSON to JavaScript in a 
// Visualforce page
String url = '/0.1/records/my.layer.name/nearby/'+
    latitude+','+longitude+'.json?radius='+radius;
String json = client.sendRequestRaw(url, null, null);

Dependencies

You must install OAuth Playground before SimpleGeo.

You must also create an OAuthService named 'SimpleGeo' containing your SimpleGeo consumer key and secret (all other fields may be blank). To test access to SimpleGeo from the OAuth Playground, go to the API Tester tab and try to GET http://api.simplegeo.com/1.0/context/ip.json - this should result in a JSON object containing geographic data relating to your IP address.

This version of the Force.com Toolkit for SimpleGeo uses the native JSON implementation, available in the Winter '12 release.

Please file issues in the GitHub project.