Chris Pollett > Students >
Shengyu

    ( Print View )

    [Bio]

    [Project Blog]

    [CS297 Proposal]

    [Deliverable 1]

    [Deliverable 2]

    [Deliverable 3]

    [Deliverable 4]

    [CS298 Proposal]

    [CS297 Report - PDF]

    [CS298 Report - PDF]

    [CS298 Presentation - PPT]

    [CS298 Code - ZIP]

                          

























Deliverable #2

Description:

  • Deliverable 2 is about writing a test program to obtain a list of zip codes which have
    geographic short distance to the given zip code. Besides integrating deliverable #2, the program
    is getting the current location zip code and obtain the list of zip codes for the related cities.

  • When we are searching for one place according to the zip code, we expect not only the inputted zip code, but
    also the zip code geographically close to the searching zip code. The Deliverable 2 is the feature for my
    phone application to get a list of zip codes near to the search location. The way I achieve this is by sending
    a request to a free web service and getting the results back to the phone.
    The following is part of some source code of this deliverable:

Source code


package net.learn2develop;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class GPSMap extends MapActivity 
{    
    private LocationManager lm;
    private LocationListener locationListener;

    private MapView mapView;
    private MapController mc;
    
    


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	Log.d("onCreate", "testing in onCreate");
        super.onCreate(savedInstanceState);
        
 
        setContentView(R.layout.main); 
        
        //---use the LocationManager class to obtain GPS locations---
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    
        
        locationListener = new MyLocationListener();
        
        lm.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);
        
        mapView = (MapView) findViewById(R.id.mapview1);

         mc = mapView.getController();
         
         boolean on = true;



         Log.d("onCreate","setSatllite mode on = "+on);
         

    }
    public void onPause(){
    	
     lm.removeUpdates(locationListener);
     
     Log.d("onPause", "testing in onPause");
    	
    	
     super.onPause();
    
    }

    @Override
    protected boolean isRouteDisplayed() {
       
        return false;
    }        
    
    private class MyLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {
            if (loc != null) {                
              Toast.makeText(getBaseContext(), 
                    "Location changed : Lat: " + loc.getLatitude() + 
                    " Lng: " + loc.getLongitude(), 
                    Toast.LENGTH_SHORT).show();
                
                GeoPoint p = new GeoPoint(
                        (int) (loc.getLatitude() * 1E6), 
                        (int) (loc.getLongitude() * 1E6));
                
                
                Geocoder geocoder = new Geocoder(getBaseContext(),Locale.getDefault());
                try{
              		
                   	   
                   	      List<Address> addresses1 = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
                   	      StringBuilder sb = new StringBuilder();
                   	      if (addresses1.size() < 0) {
                   	        Address address1 = addresses1.get(0);

                   	        sb.append(address1.getLocality()).append("\n");
                   	        sb.append(address1.getCountryName()+"\n");
                   	        sb.append(address1.getThoroughfare()+"\n");
                   	     sb.append(address1.getPostalCode()+"\n");
                   	     
                   	  getNearCities(address1.getPostalCode());
                   	     String addressString = sb.toString();
                   	  Log.i("testing addressString", addressString );
                    	 
                   	      }
                   	      	else {
                   	    String latLongString = "No location found";
                   	 Log.i("testing latLongString", latLongString );
                   	  }
                   	      
                   	    
                }
                catch(Exception e){
                	Log.i("Catch exception:", e.getMessage());
                }
                mc.setCenter(p);
                mc.setZoom(16);  

                

            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(getBaseContext(), 
                    "onProviderDisabled provider = " + provider,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(getBaseContext(), 
                    "onProviderEnabled provider = " + provider,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(getBaseContext(), 
                    "onStatusChanged provider = " + provider
                    +"status = " + status,
                    Toast.LENGTH_SHORT).show();
        }
    }
    private void saveResult(String result){

        
        FileOutputStream fos = null;
        try
        {
          fos = this.openFileOutput("result", MODE_PRIVATE);
          fos.write(result.getBytes());
                  	           
        }
        catch (IOException ie)
        {
           Log.e("AndroidPersistence", "File Write Error");
        }
        finally
        {
           if(fos !=null) 
           {
              try {fos.close();} catch(Exception e) {}
           }
        } 	  
    }
   public void getNearCities(String zipcode){ 
	   Log.i("getNearCities function >>>", zipcode);
    String location="http://codebump.com/services/placelookup.asmx/GetPlacesWithin?AuthenticationHeader=hXhFedrtFe%2F6K6wLhXGHgnzCAOFh%2F800kU3Fm2M3f2SKR1zSmw3ILG%2BZs1iRszJqeCeuJLJikkSRbNVcwFR4kIfz%2BWfUM31S&place="+zipcode+"&state=&distance=4&placeTypeToFind=ZipCode";
    String result = null;
    URL url = null;
	try {
       url = new URL(location);
    } catch (MalformedURLException e) {
    	Log.i("webservices", "in catch:"+ e.getMessage());

    }
    if(url != null) {
       try {
          HttpURLConnection urlConn =
             (HttpURLConnection) url.openConnection();

          BufferedReader in =
             new BufferedReader(
                new InputStreamReader(
                   urlConn.getInputStream()));

          String inputLine;
          while((inputLine = in.readLine()) != null)
          {
             result += "\n" + inputLine;
          }
          
       }
       catch (IOException e) {
    	   
       	Log.i("webservices", "in catch:"+ e.getMessage());

       }
       this.saveResult(result);
       Log.d("webservices", "result = "+ result);

    }
   }

}