Show me the code! – By Davanum Srinivas

December 9, 2007

Android – Reverse GeoCode – find information about a specified lat/long using MapPoint Web Service

Filed under: Uncategorized — Davanum Srinivas @ 2:55 pm

Here’s a sample on how to use Microsoft’s MapPoint SOAP API to get information on a specified latitude/longitude

1

Just enter your Mappoint userid, password, latitude, longitude and press lookup button.

Notes


Here’s the application:

package org.apache.geocode;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.List;

/**
 * Uses the MapPoint Web Service to get details (of nearby locations/addresses) on a specified lat/long
 */
public class ReverseGeoCoder extends Activity implements View.OnClickListener {

    // UI elements
    private Button mLookup;
    private EditText mUserId;
    private EditText mPassword;
    private EditText mLatitude;
    private EditText mLongitude;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        // Gather the troops
        mUserId = (EditText) findViewById(R.id.username);
        mPassword = (EditText) findViewById(R.id.password);
        mLatitude = (EditText) findViewById(R.id.latitude);
        mLongitude = (EditText) findViewById(R.id.longitude);
        mLookup = (Button) findViewById(R.id.lookup);

        // Set up a couple of button listeners
        mLookup.setOnClickListener(this);
    }

    // SOAP Request for the FindServiceSOAP.GetLocationInfo web service
    String soapRequestXML_1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
            "               xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
            "               xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
            "  <soap:Body>\n" +
            "    <GetLocationInfo xmlns=\"http://s.mappoint.net/mappoint-30/\">\n" +
            "      <location>";
    String soapRequestXML_2 = "      </location>\n" +
            "      <dataSourceName>MapPoint.NA</dataSourceName>\n" +
            "      <options/>\n" +
            "    </GetLocationInfo>\n" +
            "  </soap:Body>\n" +
            "</soap:Envelope>";

    /**
     * When the user clicks on the lookup button we use the MapPoint SOAP API to fetch the
     * information of the specified latitude longitude.
     *
     * @param view
     */
    public void onClick(View view) {

        String user = mUserId.getText().toString();
        String password = mPassword.getText().toString();
        String latitude = mLatitude.getText().toString();
        String longitude = mLongitude.getText().toString();

        // Use our customized Digest Auth scheme as per the following article
        // http://go.mappoint.net/MappointMPAC/default.aspx?main=article.aspx&id=J10002
        AuthPolicy.registerAuthScheme(AuthPolicy.DIGEST, DigestMapPointScheme.class);

        String url = "http://findv3.staging.mappoint.net/Find-30/FindService.asmx";
        HttpClient client = new HttpClient();
        client.setConnectionTimeout(8000);

        Credentials credentials = new UsernamePasswordCredentials(user, password);
        client.getState().setCredentials(AuthScope.ANY, credentials);

        PostMethod postMethod = new PostMethod(url);

        // Construct a SOAP request by hand
        StringBuffer request = new StringBuffer();
        request.append(soapRequestXML_1);
        request.append("<Latitude>").append(latitude).append("</Latitude>");
        request.append("<Longitude>").append(longitude).append("</Longitude>");
        request.append(soapRequestXML_2);
        postMethod.setRequestBody(request.toString());
        postMethod.setRequestHeader("Content-Type",
                "text/xml; charset=utf-8");
        postMethod.setRequestHeader("SOAPAction",
                "\"http://s.mappoint.net/mappoint-30/GetLocationInfo\"");

        int statusCode = 0;
        try {
            statusCode = client.executeMethod(postMethod);
        } catch (IOException e) {
            Log.d("ReverseGeoCoder", e.toString(), e);
        }

        Log.i("ReverseGeoCoder", "statusCode>>>" + statusCode);
        Log.i("ReverseGeoCoder", "statusLine>>>" + postMethod.getStatusLine());

        // Parse the SOAP Response
        MyContentHandler myContentHandler = new MyContentHandler();
        try {
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            parser.parse(postMethod.getResponseBodyAsStream(), myContentHandler);
        } catch (Exception e) {
            Log.d("ReverseGeoCoder", e.toString(), e);
        }

        // Display the response details.
        List list = myContentHandler.getLocations();
        String[] items = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            MyContentHandler.MyLocation location = (MyContentHandler.MyLocation) list.get(i);
            Log.i("ReverseGeoCoder", "Location : " + i);
            Log.i("ReverseGeoCoder", "Latitude : " + location.latitude);
            Log.i("ReverseGeoCoder", "Longitude : " + location.longitude);
            Log.i("ReverseGeoCoder", "Description : " + location.description);
            items[i] = location.description + " (geo:" + location.latitude + "," + location.longitude + ")";
        }
        // Show the data in the list view
        ListView listView = (ListView) findViewById(R.id.data);
        listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                items));
        postMethod.releaseConnection();
    }
}

Download the sources and Application – ReverseGeoCode.zip

About these ads

3 Comments »

  1. Hey Dims,

    Nice idea. Did not have time to play around with your code. Had a quick question before giving it a try. Do you know if such reverse is possible for europe?

    I ve been looking for such web service which would convert lat/long into a postal address (or a list of addresses) but never looked into MapQuest © by Microsoft ;)

    Anyway, keep up the good work. LBS are very interesting indeed.

    ./C

    Comment by Carl — December 9, 2007 @ 7:35 pm

  2. Hey. Just wondering if anyone had the complete source code for a application like this. I cant download the link at the bottom of the post. Thanks for any help

    Mg

    Comment by mg — March 13, 2010 @ 6:06 pm

  3. Download the sources and Application – ReverseGeoCode.zip

    http://www.anddev.org/download.php?id=103

    Comment by Nexus 1 — April 6, 2010 @ 2:42 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 133 other followers

%d bloggers like this: