GoogleMaps Mini

Tutorials that use the MapActivity. Many using GPS functionality.

GoogleMaps Mini

Postby lordhong » Tue Dec 11, 2007 2:20 pm

GoogleMaps Mini


:?: What is this: Basically it's a small map application that you can enter an address and lookup map in street/satellite/traffic views, including zoom in/out functions. the addresses are stored in db and utilize the auto-complete to show your historical inputs. Geocode translation is done through Yahoo Geo API.

Source codes are included and GPLV2, anyway, feel free to grab it and any comment/question please let me know.

:?: Problems/Questions: post right below...

Difficulty: 3 of 5 :)

What it will look like:

Image Image
Image Image
Image


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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private static final String DATABASE_CREATE =
  2.         "create table addresses (rowid integer primary key autoincrement, "
  3.             + "address text not null, latitude integer, longitude integer);";
  4.  
Parsed in 0.030 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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Cursor c =
  2.             db.query(false, DATABASE_TABLE, new String[] {
  3.                 "rowid", "address", "latitude", "longitude"}, "address='" + address+"'", null, null,
  4.                 null, null);
Parsed in 0.032 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. :cry: I thought maybe android api is smart enough to figure out the data type and surround it w/ single quotes if needed... :roll: apparently not yet :lol: :lol: :lol:

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... :oops:

To translate the location to geocode, I use Yahoo geo API for that, the code is really simple:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class YahooGeoAPI {
  2.  
  3.      private static final String LOGGER = "lordhong.yahoo";
  4.      private static final String APPID = "your yahoo app id goes here";
  5.      private static final String YAHOO_GEO_API_URL = "http://local.yahooapis.com/MapsService/V1/geocode?appid=";
  6.      private static HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
  7.      
  8.      public static String getGeoCode(String location) throws IOException {
  9.           StringBuffer url = new StringBuffer(YAHOO_GEO_API_URL);
  10.           url.append(APPID).append("&location=").append(location.replaceAll(" ", "+"));
  11.          
  12.           Log.i(LOGGER, "yahoo geo request: " + url.toString());
  13.           HttpURL httpURL = new HttpURL(url.toString());
  14.           HostConfiguration host = new HostConfiguration();
  15.           host.setHost(httpURL.getHost(), httpURL.getPort());
  16.           HttpConnection connection = connectionManager.getConnection(host);
  17.           connection.open();
  18.           GetMethod get = new GetMethod(url.toString());
  19.           get.execute(new HttpState(), connection);
  20.           String response = get.getResponseBodyAsString();
  21.           Log.i(LOGGER, "yahoo geo response: " + response);
  22.           connection.close();
  23.           return response;
  24.      }
  25. }
  26.  
Parsed in 0.039 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... :oops:

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class YahooGeocodeHandler extends DefaultHandler
Parsed in 0.035 seconds, using GeSHi 1.0.8.4



Finally, from what I learned HERE AT ANDDEV.org, I put together this mini google map layout:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7.  
  8.      <AutoCompleteTextView id="@+id/address"
  9.          android:layout_width="fill_parent"
  10.          android:layout_height="wrap_content"
  11.          android:text="@string/search_term"
  12.          android:completionThreshold="1"
  13.          android:selectAllOnFocus="true" />
  14.      
  15.      <view id="@+id/mapview"
  16.          class="com.google.android.maps.MapView"
  17.          android:layout_width="fill_parent"
  18.          android:layout_height="fill_parent"
  19.          android:layout_weight="1" />
  20.          
  21.      <view class="lordhong.apps.ToolBar$ToolBarView"
  22.          id="@+id/toolbar"
  23.          android:layout_width="fill_parent"
  24.          android:layout_height="wrap_content"/>
  25.  
  26. </LinearLayout>
Parsed in 0.004 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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MiniGMap extends MapActivity {
  2.      
  3.      private static MapView mapview;
  4.      private static AutoCompleteTextView address;
  5.      private DBHelper dbHelper;
  6.      private ArrayList<String> addresses;
  7.      private static final String LOGGER = "lordhong.miniGmap";
Parsed in 0.036 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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle icicle) {
  2.      super.onCreate(icicle);
  3.      
  4.      requestWindowFeature(Window.FEATURE_NO_TITLE);
  5.         setContentView(R.layout.main);
  6.        
  7.         ToolBar.setup(this);
  8.         mapview = (MapView)findViewById(R.id.mapview);
  9.         address = (AutoCompleteTextView)findViewById(R.id.address);
  10.         address.setKeyListener(new OnKeyListener() {
  11.  
  12.                public boolean onKey(View v, int keyCode, KeyEvent event) {
  13.                     if (keyCode==KeyEvent.KEYCODE_DPAD_CENTER) {
  14.                          // get value from address auto-complete
  15.                          String addr = address.getText().toString();
  16.                          Log.i(LOGGER, "addr: " + addr);
  17.                          // get lat. and long. from db
  18.                          DBHelper.AddressRow row = dbHelper.fetchRow(addr);
  19.                          if (row.rowId!=-1) {
  20.                               // navigate to geo location
  21.                               navigateToGeoCode(row.latitude, row.longitude);
  22.                          } else {
  23.                               // address not found, maybe a new address?
  24.                               try {
  25.                                    String yahooGeoResponse = YahooGeoAPI.getGeoCode(addr);
  26.                                    YahooGeocodeHandler handler = new YahooGeocodeHandler();
  27.                                    try {
  28.                                    Xml.parse(yahooGeoResponse, handler);
  29.                                } catch (SAXException e) {
  30.                                    Log.e(LOGGER, e.toString(), e);
  31.                                }
  32.                                Log.i(LOGGER, "latitude: " + handler.getLatitude());
  33.                                Log.i(LOGGER, "Longitude: " + handler.getLongitude());
  34.                                if (handler.getLatitudeAsLong()!=-1 && handler.getLongitudeAsLong()!=-1) {
  35.                                    dbHelper.createRow(addr, handler.getLatitudeAsLong(), handler.getLongitudeAsLong());
  36.                                    navigateToGeoCode(handler.getLatitudeAsLong(), handler.getLongitudeAsLong());
  37.                                    // re-populate the addresses, including the new address
  38.                                    populateAddresses();
  39.                                }
  40.                               } catch (Exception e) {
  41.                                    Log.e(LOGGER, "addr error from yahoo geo lookup: " + addr, e);
  42.                               }
  43.                              
  44.                               //Log.i("lordhong.miniGmap", "NOT FOUND!: " + addr);
  45.                          }
  46.                     }
  47.                     return false;
  48.                }
  49.          
  50.         });
  51.         dbHelper = new DBHelper(this);
  52.         populateAddresses();
  53.     }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4



Then I provide some static methods for map operations:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static void zoomIn() {
  2.      mapview.getController().zoomTo(mapview.getZoomLevel() + 1);
  3.     }
  4.    
  5.     public static void zoomOut() {
  6.      mapview.getController().zoomTo(mapview.getZoomLevel() - 1);
  7.     }
  8.    
  9.     public static void satellite() {
  10.      mapview.toggleSatellite();
  11.     }
  12.    
  13.     public static void traffic() {
  14.      mapview.toggleTraffic();
  15.     }
  16.    
  17.     public static void enterAddress() {
  18.      address.requestFocus();
  19.      address.setSelectAllOnFocus(true);
  20.     }
  21.  
  22. private void navigateToGeoCode(long latitude, long longitude) {
  23.      Log.i(LOGGER, latitude+"--"+longitude);
  24.      Point p = new Point((int)latitude, (int)longitude);
  25.           MapController mc = mapview.getController();
  26.           mc.animateTo(p);
  27.           mc.zoomTo(21);
  28.     }
  29.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Here's the populate address method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void populateAddresses() {
  2.      addresses = new ArrayList<String>();
  3.      
  4.      List<AddressRow> rows = dbHelper.fetchAllRows();
  5.      for (AddressRow row : rows) {
  6.           addresses.add(row.address);
  7.      }
  8.      
  9.      ArrayAdapter<String> addressAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, addresses);
  10.      address.setAdapter(addressAdapter);
  11.     }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



:shock: :shock: this completes my first tutorial... please forgive my writing as I'm having a pretty bad cold right now :oops: any question, please reply in this thread.

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... :roll:
Attachments
MiniGMap0.9.1.rar
(21.81 KiB) Downloaded 1458 times
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Top

Postby piyush » Tue Dec 11, 2007 2:26 pm

Hi...
i have execute apllication [MiniGMap0.9.1] but unable to find MAP Question on screen..
how u getting map with ur amppliaction.

Thanks in advance.
Piyush
piyush
Freshman
Freshman
 
Posts: 2
Joined: Mon Dec 10, 2007 6:22 am

Postby plusminus » Tue Dec 11, 2007 2:29 pm

Hello piyush,

I accidentally deleted this topic :oops: :oops: :oops:
could restore posts, but lost the zip-source, can you append it to your post please :?: :!: :?:


Does your Application show NOTHING or pretty soft gridlines :?:

Best Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby lordhong » Tue Dec 11, 2007 4:09 pm

:shock: i will attach the source later... need to find it somewhere, dont have my laptop w/ me...

as for the map, sometimes the initial map shows nothing/grid, you can drag the map a bit to force it to render...
does this answer your question?

Thanks,
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

SOURCE!!!

Postby lordhong » Tue Dec 11, 2007 5:37 pm

:oops: here's the source zip... :wink:
Attachments
MiniGMap0.9.1.rar
:)
(21.81 KiB) Downloaded 853 times
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby plusminus » Wed Dec 12, 2007 5:44 pm

Thanks lord ;)
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

great tutorial, thanks!

Postby kosirm » Thu Dec 13, 2007 3:17 pm

great tutorial, thanks!
kosirm
Freshman
Freshman
 
Posts: 3
Joined: Sat Dec 01, 2007 10:27 pm

Icons

Postby johnny » Sun Dec 23, 2007 4:43 pm

I know that +- wrote it in his frist reply on the undeleted post:

Which is the iconset you are using for your App? i would like to use it too.

Thank you
johnny
Freshman
Freshman
 
Posts: 3
Joined: Tue Nov 27, 2007 11:46 pm

Postby lordhong » Sun Dec 23, 2007 7:26 pm

lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby plusminus » Sun Dec 23, 2007 7:33 pm

Hey johnny,

nice memory :D
I assumed it was the Tango Icon Library and I was right :) .

[Edit]
damn i was to slow ;)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby johnny » Mon Dec 24, 2007 3:43 am

Nice memory would've been, if i could have remembered the icontheme ;)

Thank you for the quick response.

MERRY CHRISTMAS!!!!
johnny
Freshman
Freshman
 
Posts: 3
Joined: Tue Nov 27, 2007 11:46 pm

Postby plusminus » Mon Dec 24, 2007 2:43 pm

:roll: You at least remembered where you read it ^^
My memory is less good :P

Merry Christmas,
plusminus :xmas:
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Fail to launch miniGMap

Postby lujasone » Wed Feb 20, 2008 7:34 am

I meet error when I try to launch miniGMap as follows:
Application Error: lordhong.apps

An error has occurred in lordhong.apps. Unable to start activity ComponentInfo{lordhong.apps/lordhong.apps.miniGMap}:java.lang.NullPointerException.

Can anyone help to figure out the reason ? Thanks,

-Jason Lu
lujasone
Freshman
Freshman
 
Posts: 3
Joined: Thu Feb 14, 2008 8:55 am

Postby plusminus » Wed Feb 20, 2008 10:27 am

Hello Jason,

are you using SDK-Version m5 :?:

You'll have to make some adaptions, because lordhong made this code on SDK-Version m3.

On upgrading issues, have a look at these posts:
:rarrow: viewtopic.php?t=879
:rarrow: viewtopic.php?t=881

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

It works now !

Postby lujasone » Thu Feb 21, 2008 3:47 am

It works now with some suggestion from plusminus. (m3->m5). :D

plusminus, Thanks for your help !!

-Jason
lujasone
Freshman
Freshman
 
Posts: 3
Joined: Thu Feb 14, 2008 8:55 am

Top
Next

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests