Skip to content

Accessing the Bing Maps Web Services in Java

September 6, 2009
Introduction

There is a lot of documentation and code samples for using the web services in .NET and most of it can be carried over to Java, but there are some key differences that potentially cause headaches. We already saw there is a difference in how the proxy classes need to be created, which Visual Studio does an excellent job in performing these chores for the user. It also hides the creation of SOAP calls that the web services use. This may be trivial for experienced SOAP programmers, but for those of us that are less experienced (including me), it can be a little confusing. We will go through the process of creating the service client so that we can use the cool features of the Bing Maps Web Services.

Getting the VE Token
It’s the same process as in .NET, except for the use of Stubs. Before we start, a few import statements are needed.

import org.apache.axis.*;
import net.mappoint.s.mappoint_30.*;
Now to get the token.


CommonServiceSoapStub cs = (CommonServiceSoapStub) (new CommonServiceLocator()).getCommonServiceSoap();
cs.setUsername(username);
cs.setPassword(password);
TokenSpecification spec = new TokenSpecification(clientIP, tokenValidTime);
String token = cs.getClientToken(spec);

Getting the Geocode Client
Creating the service client in .NET is straight forward – simply create the service client object.  This is a bit different in Java, as we saw before when getting the token. Again, some import statements before we begin.
import net.virtualearth.dev.webservices.v1.common.*;
import net.virtualearth.dev.webservices.v1.geocode.*;
And to create the service client.
BasicHttpBinding_IGeocodeServiceStub geocodeServiceClient = (BasicHttpBinding_IGeocodeServiceStub) (new GeocodeServiceLocator()).getBasicHttpBinding_IGeocodeService();
Now that we have the service client ready to go, we want to start geocoding. It should start looking more familiar.
GeocodeRequest request = new GeocodeRequest();
/* Set the request properties here */
GeocodeResponse response = geocodeServiceClient.geocode(request);
GeocodeResult[] results = response.getResults();
Java doesn’t have {get; set;} so it becomes an explicit method call to get the result set. The GeocodeRequest constructor is also overloaded, allowing you to specify all of the properties as arguments. For now we’ll set the properties the old way.
Example
GeocodeRequest request = new GeocodeRequest();
request.setQuery("1 Microsoft Way, Redmond, WA");
Credentials credentials = new Credentials("1", token); // "1" is just an arbitrary identifier
request.setCredentials(credentials);
GeocodeResponse response = geocodeService.geocode(request);
GeocodeResult[] results = response.getResults();
Output

Conclusion
I decided that the process of creating the stub was a bit annoying, especially with the ridiculously long class name that WSDL2Java created for me. So I wrote a class that created the stub in the background and made its functionality similar to that of .NET.
5 Comments
  1. Yuzhe permalink

    hi, I have problems about "import net.virtualearth.dev.webservices.v1.common.*;" where should I get the code in package net.virtualearth.dev.webservices.v1.common.*. Thx

  2. Jonathan permalink

    The common package is part of the geocode WSDL file. When you run WSDL2Java on that wsdl file, you should see common and geocode directories created under the v1 directory.

  3. Byron permalink

    Is there any way to just make a simple geocode request via http and parse the json/xml/string/etc in my jsp?

  4. Jonathan permalink

    I haven\’t done this in Java, but you can construct the XML SOAP request and submit it directly to the appropriate service URL as a regular HTTP request. You\’ll still need to retrieve the token or use an application key to authenticate.

  5. Unknown permalink

    what exactly does the username and password refer to?

Leave a comment