Solved

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

Posted on 2011-03-07
11
1,417 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
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 …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

18 Experts available now in Live!

Get 1:1 Help Now