Simple Web Service for retreiving Yahoo Map Images

Simple Web Service for retreiving Yahoo Map Images

I’m preparing the background for my Final Year Project at University. I’m planning to develop a mobile application making a heavy use of Location Based Services. The mobile application will communicate with the application server (possibly Glassfish V2 or JBoss) using Web Services (JSR-172). This is the prototype web service for later use by the mobile application which allows to retrive the URL of PNG image from the yahoo server.
The reason for choosing Yahoo over Google is very simple, first, Google have very strict policy on using their map images (retriving image tiles from the Google servers directly is against their licence agreement), secondly, Yahoo provides very Simple Map Image API which allows retriving the images using simple REST queries on the yahoo servers.
Basically, using Yahoo Maps API is straightforward, the only problem is if you want to retrieve just the image URL without all the REST additional XML tags.
The prototype web service described here retrives the XML document and parse it using object created from one helper class.
The web service is created using EJB3 technology and takes number of parameters to specify the details of requested image:



@WebMethod(operationName = "getImageURL")
public String getImageURL(@WebParam(name = "latitude")
float latitude, @WebParam(name = "longitude")
float longitude, @WebParam(name = "width")
int width, @WebParam(name = "height")
int height, @WebParam(name = "zoom")
int zoom) {
MapRetriever retriever = new MapRetriever(latitude, longitude, width, height, zoom);
return retriever.getMapImageURL();
}

The parameters are self-describing, if you want to modify it please have a look the Yahoo Simple Map Image API for more options.
The MapRetriver class is actually responsible for retrieving the URL of the map image from the REST response. The code for this class is presented below:


/*
* MapRetriever.java
*
* Created on 03-Oct-2007, 15:56:47
*
*
*/

package fyp.helpers;
import java.io.BufferedInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
* The object initialized from this class gets lat and long coordinates from the
* client and retrives map from yahoo REST service, returns the map as an png image
* modified to the client requirements.
* @author kris
*/
public class MapRetriever {

private double latitude;
private double longitude;
private int imgWidth;
private int imgHeight;
private String yahooApplicationID;
private String baseURL;
private int zoom;
/**
* Constructor initializing the necessary elements for retriving the map url
* image. The parameters specify the location as well as image width and
* image height. The zoom element specify the precision of the map image.
*
* @param lat float: -90 to 90 The latitude of the starting location.
* @param lon float: -180 to 180 The longitude of the starting location.
* If both latitude and longitude are specified, they will take
* priority over all other location data. If only one of
* latitude or longitude is specified, both will be ignored.
* @param width integer: 10 to 2000 (default: 620)
* The width of the image being generated, in pixels.
* @param height integer: 10 to 2000 (default: 500)
* The height of the image being generated, in pixels.
* @param zoom integer: 1 to 12 (default: 6) The zoom level for
* the map, from 1 (street level) to 12 (country level).
*/
public MapRetriever(float lat, float lon, int width, int height, int zoom) {
this.latitude = lat;
this.longitude = lon;
this.imgWidth = width;
this.imgHeight = height;
this.zoom = zoom;
this.yahooApplicationID = "YOUR-OWN-YAHOO-ID";
this.baseURL = "http://local.yahooapis.com/MapsService/V1/mapImage?appid=";
}
/**
* This method calls constructInitialRequest() for creating initial
* request to the yahoo server and retrives XML document as a result of the
* REST query. Using XML parser the method retrives the proper image URL
* from the yahoo REST response.
*
* @see constructInitialRequest()
* @return url of the image on the yahoo server
*/
public String getMapImageURL() {
String url = this.constructInitialRequest();
String result = null;
URL u = null;
try {
if (url != null) {
u = new URL(url);
}
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
BufferedInputStream bis = new BufferedInputStream(uc.getInputStream());
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.parse(bis);
NodeList nl = doc.getElementsByTagName("Result");
result = nl.item(0).getFirstChild().getNodeValue();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
/**
* Method for gathering all initialized parameters and creating
* the initial request url for retriving the xml with the image url
* from the yahoo server.
*
* @return initinal url string for making the request to the yahoo server
*/
public String constructInitialRequest() {
return baseURL + yahooApplicationID + "&latitude=" + latitude + "&longitude=" + longitude + "&image_height=" + imgHeight + "&image_width=" + imgWidth + "&zoom=" + zoom;
}

}

To use the Yahoo service you would have to create an account first and request the unique Yahoo ID which you have to add in all your requests to Yahoo servers.
Again if you are interested in all possible arguments for the Yahoo REST queries please refer to the Yahoo Maps API web site.

One response on “Simple Web Service for retreiving Yahoo Map Images

  1. macromedia dreamweaver March 31, 2012 at 10:38 pm

    Nice your post.I will come back when provide new post.Thanks for sharing.

Leave a Reply