[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Use coldfusion to extract longitude and lattitude from java.io.ByteArrayOutputStream returned by google geocode

Posted on 2011-03-07
11
Medium Priority
?
1,469 Views
Last Modified: 2012-05-11
I get back longitude and lattitude but is is within java.io.ByteArrayOutputStream I have used various string functions without success. It appears google return a type of array. How do I use coldfusion to get out the first mention of lng and lat. I would rather extract from this array than string in case positions content changes. java.io.ByteArrayOutputStream is just a foreign stucture to me need help.
So I want the lat and lng values  out of:
"geometry": { "location": { "lat": 30.3450506, "lng": -97.3806623 },
Taken from outout below.

Once I extract them I will store them in database.

See code and returned output
<!--- get lat and lng based on location ---->
<cfoutput>
<cfhttp method="get"
        url="http://maps.googleapis.com/maps/api/geocode/json?address=#urlencodedformat(location)#&sensor=false"
        resolveurl="no">
</cfhttp>
</cfoutput>

<cfoutput>
   #cfhttp.FileContent# <br>
</cfoutput>

<cfdump var="#cfhttp.FileContent#">

This is the output:

{ "status": "OK", "results": [ { "types": [ "route" ], "formatted_address": "Central Ave, Elgin, TX 78621, USA", "address_components": [ { "long_name": "Central Ave", "short_name": "Central Ave", "types": [ "route" ] }, { "long_name": "Elgin", "short_name": "Elgin", "types": [ "locality", "political" ] }, { "long_name": "Elgin", "short_name": "Elgin", "types": [ "administrative_area_level_3", "political" ] }, { "long_name": "Bastrop", "short_name": "Bastrop", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "Texas", "short_name": "TX", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] }, { "long_name": "78621", "short_name": "78621", "types": [ "postal_code" ] } ], "geometry": { "location": { "lat": 30.3450506, "lng": -97.3806623 }, "location_type": "GEOMETRIC_CENTER", "viewport": { "southwest": { "lat": 30.3408600, "lng": -97.3884360 }, "northeast": { "lat": 30.3471553, "lng": -97.3700939 } }, "bounds": { "southwest": { "lat": 30.3411300, "lng": -97.3884360 }, "northeast": { "lat": 30.3468853, "lng": -97.3700939 } } }, "partial_match": true } ] }


<cfdump var="#cfhttp.FileContent#">
object of java.io.ByteArrayOutputStream
Methods toString (returns java.lang.String)
toString (returns java.lang.String)
toString (returns java.lang.String)
size (returns int)
write (returns void)
write (returns void)
reset (returns void)
toByteArray (returns [B)
close (returns void)
writeTo (returns void)
write (returns void)
flush (returns void)
hashCode (returns int)
getClass (returns java.lang.Class)
wait (returns void)
wait (returns void)
wait (returns void)
equals (returns boolean)
notify (returns void)
notifyAll (returns void)


0
Comment
Question by:Ian White
  • 6
  • 5
11 Comments
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35056375
<cfdump var="#DeserializeJSON(cfhttp.FileContent).results[1].geometry.viewport.northeast.lat#">
<cfdump var="#DeserializeJSON(cfhttp.FileContent).results[1].geometry.viewport.northeast.lng#">
<cfdump var="#DeserializeJSON(cfhttp.FileContent).results[1].geometry.viewport.southwest.lat#">
<cfdump var="#DeserializeJSON(cfhttp.FileContent).results[1].geometry.viewport.southwest.lng#">

Open in new window

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35056384
to get the result in a structure, dump..

<cfdump var="#DeserializeJSON(cfhttp.FileContent)#">

This will give you structre of results, you can then check on the things what you want...
0
 

Author Comment

by:Ian White
ID: 35061654
Hi Thanks but that is the bounds that you gave me. but I want the first occurance of lat long
GEOMETRIC_CENTER

geometry": { "location": { "lat": 30.3450506, "lng": -97.3806623 }, "location_type": "GEOMETRIC_CENTER"

What is the statement for that?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35062285
<cfdump var="#DeserializeJSON(cfhttp.FileContent).results[1].geometry.location.lat#">
<cfdump var="#DeserializeJSON(cfhttp.FileContent).results[1].geometry.location.lng#">

Open in new window

0
 

Author Comment

by:Ian White
ID: 35065803
Hi

<cfdump var="#DeserializeJSON(cfhttp.FileContent)#">

Returns error with CF6:

Variable DESERIALIZEJSON is undefined.  <br>The error occurred on line 114.

The CF docu says this was added in CF8  I run CF6

Is there a function out there I could use to do the same?

Or another alternative for CF6

Thanks



Please help
Thanks
Ian
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35065845
Download this UDF from here

http://cflib.org/index.cfm?event=page.udfbyid&udfid=1287

and then use it ..

<cfdump var="#jsondecode(cfhttp.FileContent)#">

it should work on CF 6
0
 

Author Comment

by:Ian White
ID: 35066137
Thanks <cfdump var="#jsondecode(cfhttp.FileContent)#">
 dumped the aray ok as show below into array/structure
But then how to extract the location lat long values into my cf variables longitude and lattitude form location structure?
Please advise code. Sorry I am more of a biz person.

<cfdump var="#jsondecode((cfhttp.FileContent).results[1].geometry.location.lat#">
<cfdump var="#jsondecode((cfhttp.FileContent).results[1].geometry.location.lng#">
Fails with an error about attribute

struct
results array  
1 struct
address_components array  
1 struct
long_name Central Ave
short_name Central Ave
types array  
1 route
 
2 struct
long_name Elgin
short_name Elgin
types array  
1 locality
2 political
 
3 struct
long_name Elgin
short_name Elgin
types array  
1 administrative_area_level_3
2 political
 
4 struct
long_name Bastrop
short_name Bastrop
types array  
1 administrative_area_level_2
2 political
 
 
5 struct
long_name Texas
short_name TX
types array  
1 administrative_area_level_1
2 political
 
 
6 struct
long_name United States
short_name US
types array  
1 country
2 political
 
 
7 struct
long_name 78621
short_name 78621
types array  
1 postal_code
 
 
 
formatted_address Central Ave, Elgin, TX 78621, USA
geometry struct
bounds struct
northeast struct
lat 30.3468853
lng -97.3700939
 
southwest struct
lat 30.3411300
lng -97.3884360
 
 
location struct
lat 30.3450506
lng -97.3806623
 
location_type GEOMETRIC_CENTER
viewport struct
northeast struct
lat 30.3471553
lng -97.3700939
 
southwest struct
lat 30.3408600
lng -97.3884360
 
 
 
partial_match true
types array  
1 route
 
 
 
status OK
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35066168
You have an extra bracket in code..

<cfdump var="#jsondecode(cfhttp.FileContent).results[1].geometry.location.lat#">
<cfdump var="#jsondecode(cfhttp.FileContent).results[1].geometry.location.lng#">

Open in new window



Use the above.. it works, I just checked it out...
0
 
LVL 11

Accepted Solution

by:
Brijesh Chauhan earned 2000 total points
ID: 35066191
You can assign to your CF variables...

<cfset latitude = jsondecode(cfhttp.FileContent).results[1].geometry.location.lat />
<cfset longitude = jsondecode(cfhttp.FileContent).results[1].geometry.location.lng />

<cfoutput> Latitude -> #latitude#  Longitude -> #longitude# </cfoutput>

Open in new window

0
 

Author Comment

by:Ian White
ID: 35066396
Thanks for your patience - that works perfectly
0
 

Author Closing Comment

by:Ian White
ID: 35066417
Wonderful - now I can add great functionality to my website
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question