Java Reference
In-Depth Information
Getting ready
In Chapter 5 , JavaFX Media , we learned how to use the Image API to download images
automatically. However, the API offers little control over the download process. In this recipe,
we are going to use HttpRequest, which provides granular control over the content of the
image downloaded. For some portions of the code, you will need to be familiar with the use of
JavaFX data binding and triggers (see Chapter 1 , Getting Started with JavaFX for details). For
a background on how to display images in JavaFX, review the recipe Loading and displaying
images with ImageView from Chapter 5 , JavaFX Media .
To illustrate the use of HttpRequest , we are going to build a Google Map application.
The application uses the Google Static Map API to download static map images based on
a provided address. The example requires a Google API Key to run. You can get your own
Google API Key at http://code.google.com/apis/maps/signup.html .
How to do it...
The abbreviated version of the code provided next shows the essential portions needed to
show how to pull down an image from Google's map server. The code listing intentionally
leaves out portion of the code used to assemble the GUI components (topics on GUI controls
are covered in Chapter 4 , Components and Skinning ). Refer to ch06/source-code/src/
http/HttpRequestGoogleMap.fx for a complete listing of the code.
var w = 640;
var h = 480;
var bytesToRead:Long;
var currentBytesRead:Long;
var imgW = w * 0.8;
var imgH = h * 0.7;
var converter = URLConverter{}
var loc = converter .encodeString("Atlanta, GA");
var zoom:Number = 15;
var mapType="roadmap";
def apiKey="PLACE_YOUR_GOOGLE_API_KEY_HERE";
var gmapUrl = bind "http://maps.google.com/maps/api/staticmap?"
"center={loc}"
"&zoom={zoom as Integer}"
"&size={imgW as Integer}x{imgH as Integer}"
"&maptype={mapType}"
"&format=png32"
"&markers=color:blue|{loc}"
"&sensor=false"
"&key={apiKey}"
on replace {
 
Search WWH ::




Custom Search