?
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,446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 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