Solved

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

Posted on 2011-03-07
11
1,422 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now