andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

Android - Using Microsoft Map point web service


 
       anddev.org - Android Development Community | Android Tutorials | Index -> Map Tutorials
Author Message
gvkreddyvamsi
Developer


Joined: 21 Jan 2008
Posts: 43
Location: INDIA

PostPosted: Thu Jan 31, 2008 10:09 am    Post subject: Android - Using Microsoft Map point web service Reply with quote

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

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

1. Introcuction and getting started
http://msdn2.microsoft.com/en-us/library/aa492550.aspx
2. Signup for mappoint account
https://mappoint-css.live.com/MwsSignUp/Default.aspx
3. Mappoint WSDl
http://staging.mappoint.net/standard-30/mappoint.wsdl
4. Web based Demo Page
http://demo.mappoint.net/fourthcoffeecompany/display.aspx?uc=findservicesoapgetlocationinfo

Here is the application





Java:
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();
    }




You can download complete source code at ReverseGeoCode.zip

by vamsi
Back to top
View user's profile Send private message Send e-mail Visit poster's website
cabernet1976
Senior Developer


Joined: 16 Nov 2007
Posts: 146
Location: China

PostPosted: Thu Jul 03, 2008 8:00 am    Post subject: Reply with quote

Hi gvkreddyvamsi,
How do you think about this class android.location.Address? It seems be able to implement your feature.
Thanks.

_________________
EveryAlbum - http://www.anddev.org/viewtopic.php?p=7117#7117
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Map Tutorials All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.