Solved

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

Posted on 2011-03-07
11
1,425 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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