GoogleMaps Mini
Source codes are included and GPLV2, anyway, feel free to grab it and any comment/question please let me know.
Difficulty: 3 of 5

What it will look like:


Description:
First, I modified the DBHelper from Notepad tutorial 1 to use for storing the address/location history.
Nothing fancy here, just need to make a few changes to accommodate the location stuff:
Using java Syntax Highlighting
- private static final String DATABASE_CREATE =
- "create table addresses (rowid integer primary key autoincrement, "
- + "address text not null, latitude integer, longitude integer);";
Parsed in 0.029 seconds, using GeSHi 1.0.8.4
I also need to be able to look up by location (String), so the fetchRow query is modified a bit:
Using java Syntax Highlighting
- Cursor c =
- db.query(false, DATABASE_TABLE, new String[] {
- "rowid", "address", "latitude", "longitude"}, "address='" + address+"'", null, null,
- null, null);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
You can see the single quote ' used in the criteria field, and yes, you do need single quotes around your value for String/Text fields.

Then for the 5 buttons on the toolbar, I pretty much referenced from Steven Osborn's Undroid project at: http://code.google.com/p/undroid/w/list
Many thanks to Steven's great contribution, along with Tango icons, it looks really nice, at least for someone lack basic graphical design sense, such as me...
To translate the location to geocode, I use Yahoo geo API for that, the code is really simple:
Using java Syntax Highlighting
- public class YahooGeoAPI {
- private static final String LOGGER = "lordhong.yahoo";
- private static final String APPID = "your yahoo app id goes here";
- private static final String YAHOO_GEO_API_URL = "http://local.yahooapis.com/MapsService/V1/geocode?appid=";
- private static HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
- public static String getGeoCode(String location) throws IOException {
- StringBuffer url = new StringBuffer(YAHOO_GEO_API_URL);
- url.append(APPID).append("&location=").append(location.replaceAll(" ", "+"));
- Log.i(LOGGER, "yahoo geo request: " + url.toString());
- HttpURL httpURL = new HttpURL(url.toString());
- HostConfiguration host = new HostConfiguration();
- host.setHost(httpURL.getHost(), httpURL.getPort());
- HttpConnection connection = connectionManager.getConnection(host);
- connection.open();
- GetMethod get = new GetMethod(url.toString());
- get.execute(new HttpState(), connection);
- String response = get.getResponseBodyAsString();
- Log.i(LOGGER, "yahoo geo response: " + response);
- connection.close();
- return response;
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
This returns the Yahoo geo api XML response as String. I don't have any error-handling here for invalid location, multiple locations, etc. I just assume it works...
Then I build the XML SAX parsing part by referenced from Davanum Srinivas' LocateMe at: http://davanum.wordpress.com/2007/11/30 ... -location/
So I created a YahooGeocodeHandler to do just that:
Using java Syntax Highlighting
- public class YahooGeocodeHandler extends DefaultHandler
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Finally, from what I learned HERE AT ANDDEV.org, I put together this mini google map layout:
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <AutoCompleteTextView id="@+id/address"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/search_term"
- android:completionThreshold="1"
- android:selectAllOnFocus="true" />
- <view id="@+id/mapview"
- class="com.google.android.maps.MapView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1" />
- <view class="lordhong.apps.ToolBar$ToolBarView"
- id="@+id/toolbar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
In the main mini app class, I have some basic stuff/variables, and adapter for the address/location:
Using java Syntax Highlighting
- public class MiniGMap extends MapActivity {
- private static MapView mapview;
- private static AutoCompleteTextView address;
- private DBHelper dbHelper;
- private ArrayList<String> addresses;
- private static final String LOGGER = "lordhong.miniGmap";
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
In the onCreate() method, all the views are init.ed, key events are registered, db is init.ed, and historical addresses are populated:
Using java Syntax Highlighting
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.main);
- ToolBar.setup(this);
- mapview = (MapView)findViewById(R.id.mapview);
- address = (AutoCompleteTextView)findViewById(R.id.address);
- address.setKeyListener(new OnKeyListener() {
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- if (keyCode==KeyEvent.KEYCODE_DPAD_CENTER) {
- // get value from address auto-complete
- String addr = address.getText().toString();
- Log.i(LOGGER, "addr: " + addr);
- // get lat. and long. from db
- DBHelper.AddressRow row = dbHelper.fetchRow(addr);
- if (row.rowId!=-1) {
- // navigate to geo location
- navigateToGeoCode(row.latitude, row.longitude);
- } else {
- // address not found, maybe a new address?
- try {
- String yahooGeoResponse = YahooGeoAPI.getGeoCode(addr);
- YahooGeocodeHandler handler = new YahooGeocodeHandler();
- try {
- Xml.parse(yahooGeoResponse, handler);
- } catch (SAXException e) {
- Log.e(LOGGER, e.toString(), e);
- }
- Log.i(LOGGER, "latitude: " + handler.getLatitude());
- Log.i(LOGGER, "Longitude: " + handler.getLongitude());
- if (handler.getLatitudeAsLong()!=-1 && handler.getLongitudeAsLong()!=-1) {
- dbHelper.createRow(addr, handler.getLatitudeAsLong(), handler.getLongitudeAsLong());
- navigateToGeoCode(handler.getLatitudeAsLong(), handler.getLongitudeAsLong());
- // re-populate the addresses, including the new address
- populateAddresses();
- }
- } catch (Exception e) {
- Log.e(LOGGER, "addr error from yahoo geo lookup: " + addr, e);
- }
- //Log.i("lordhong.miniGmap", "NOT FOUND!: " + addr);
- }
- }
- return false;
- }
- });
- dbHelper = new DBHelper(this);
- populateAddresses();
- }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4
Then I provide some static methods for map operations:
Using java Syntax Highlighting
- public static void zoomIn() {
- mapview.getController().zoomTo(mapview.getZoomLevel() + 1);
- }
- public static void zoomOut() {
- mapview.getController().zoomTo(mapview.getZoomLevel() - 1);
- }
- public static void satellite() {
- mapview.toggleSatellite();
- }
- public static void traffic() {
- mapview.toggleTraffic();
- }
- public static void enterAddress() {
- address.requestFocus();
- address.setSelectAllOnFocus(true);
- }
- private void navigateToGeoCode(long latitude, long longitude) {
- Log.i(LOGGER, latitude+"--"+longitude);
- Point p = new Point((int)latitude, (int)longitude);
- MapController mc = mapview.getController();
- mc.animateTo(p);
- mc.zoomTo(21);
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Here's the populate address method:
Using java Syntax Highlighting
- public void populateAddresses() {
- addresses = new ArrayList<String>();
- List<AddressRow> rows = dbHelper.fetchAllRows();
- for (AddressRow row : rows) {
- addresses.add(row.address);
- }
- ArrayAdapter<String> addressAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, addresses);
- address.setAdapter(addressAdapter);
- }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
Again, many thanks to Steven, Davanum, and plusminus for their great contributions, and last but not the least, our anddev.org forum, THE BEST ANDROID FORUM!!!
/me drinking vick's...






Tango Icons!


