Skip to content

Instantly share code, notes, and snippets.

@metadaddy
Created September 22, 2011 23:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metadaddy/1236289 to your computer and use it in GitHub Desktop.
Save metadaddy/1236289 to your computer and use it in GitHub Desktop.
Parse a Google Maps response from a String
public class GeocodingUtil {
//@future(callout=true)
public static void callGoogleGeocodingService() {
/*
Http httpProtocol = new Http();
// Create HTTP request to send.
HttpRequest request = new HttpRequest();
// Set the endpoint URL.
String endpoint = 'https://maps.googleapis.com/maps/api/' +
'geocode/json?address=1+Market+Street,+Suite+300,' +
'+San+Francisco,+CA&sensor=true';
request.setEndPoint(endpoint);
// Set the HTTP verb to GET.
request.setMethod('GET');
// Send the HTTP request and get the response.
// The response is in JSON format.
HttpResponse response = httpProtocol.send(request);
// Create JSON parser with the HTTP response body
// as an input string.
JSONParser parser = JSON.createParser(response.getBody());
*/
String str = '{' +
' "results" : [' +
' {' +
' "address_components" : [' +
' {' +
' "long_name" : "300",' +
' "short_name" : "300",' +
' "types" : [ "subpremise" ]' +
' },' +
' {' +
' "long_name" : "1",' +
' "short_name" : "1",' +
' "types" : [ "street_number" ]' +
' },' +
' {' +
' "long_name" : "Market St",' +
' "short_name" : "Market St",' +
' "types" : [ "route" ]' +
' },' +
' {' +
' "long_name" : "San Francisco",' +
' "short_name" : "San Francisco",' +
' "types" : [ "locality", "political" ]' +
' },' +
' {' +
' "long_name" : "San Francisco",' +
' "short_name" : "San Francisco",' +
' "types" : [ "administrative_area_level_3", "political" ]' +
' },' +
' {' +
' "long_name" : "San Francisco",' +
' "short_name" : "San Francisco",' +
' "types" : [ "administrative_area_level_2", "political" ]' +
' },' +
' {' +
' "long_name" : "California",' +
' "short_name" : "CA",' +
' "types" : [ "administrative_area_level_1", "political" ]' +
' },' +
' {' +
' "long_name" : "United States",' +
' "short_name" : "US",' +
' "types" : [ "country", "political" ]' +
' },' +
' {' +
' "long_name" : "94105",' +
' "short_name" : "94105",' +
' "types" : [ "postal_code" ]' +
' }' +
' ],' +
' "formatted_address" : "1 Market St #300, San Francisco, CA 94105, United States",' +
' "geometry" : {' +
' "location" : {' +
' "lat" : 37.79410130,' +
' "lng" : -122.39510960' +
' },' +
' "location_type" : "APPROXIMATE",' +
' "viewport" : {' +
' "northeast" : {' +
' "lat" : 37.79545028029150,' +
' "lng" : -122.3937606197085' +
' },' +
' "southwest" : {' +
' "lat" : 37.79275231970851,' +
' "lng" : -122.3964585802915' +
' }' +
' }' +
' },' +
' "partial_match" : true,' +
' "types" : [ "subpremise" ]' +
' }' +
' ],' +
' "status" : "OK"' +
'}';
JSONParser parser = JSON.createParser(str);
Double latitude = 0.0;
Double longitude = 0.0;
// Parse JSON response to get individual field values.
while (parser.nextToken() != null) {
// Extract the latitude and longitude values from
// the geometry object.
// Example of part of the JSON response
// that contains the geometry object:
// "geometry": {
// "location": {
// "lat": 37.7941078,
// "lng": -122.3951158
// },
// Look for the geometry field.
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'geometry')) {
// Advance to the location field.
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'location')) {
// Advance to the latitude and longitude fields.
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() ==
JSONToken.FIELD_NAME)
&& (parser.getText() == 'lat')) {
parser.nextToken();
// Store the latitude value in a variable.
latitude = parser.getDoubleValue();
system.Debug(parser.getCurrentName() + ':'+ latitude);
}
else if ((parser.getCurrentToken() ==
JSONToken.FIELD_NAME)&&
(parser.getText() == 'lng')) {
parser.nextToken();
// Store the longitude value in a variable.
longitude = parser.getDoubleValue();
system.Debug(parser.getCurrentName() + ':'+ longitude);
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment