Show me the code! – By Davanum Srinivas

November 15, 2007

Working with Google Android’s Maps (aka Search for Pizza)

Filed under: Uncategorized — Davanum Srinivas @ 12:14 pm

Here’s some sample code to zoom in/out, switch on traffic/satellite images. Note that the MapView stuff is not fleshed out yet!! Hence lots of undocumented stuff that is likely to change.

package org.apache.maps;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Point;
import com.google.googlenav.Placemark;
import com.google.googlenav.Search;
import com.google.googlenav.map.Map;
import com.google.googlenav.map.MapPoint;
import com.google.googlenav.map.Zoom;

public class BrowseMap extends MapActivity {
    private MapView mMapView;

    private String LOG_TAG = "BrowseMap";

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mMapView = new MapView(this);

        // Use Yahoo Geo code to find the lat/long.
        // Click on the Sample Request URL here for example
        // http://developer.yahoo.com/maps/rest/V1/geocode.html
        Point p = new Point((int) (37.416402 * 1000000), (int)
                (-122.025078 * 1000000));
        MapController mc = mMapView.getController();
        mc.animateTo(p);
        mc.zoomTo(9);
        setContentView(mMapView);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_I) {
            // Zoom In
            int level = mMapView.getZoomLevel();
            mMapView.getController().zoomTo(level + 1);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_O) {
            // Zoom Out
            int level = mMapView.getZoomLevel();
            mMapView.getController().zoomTo(level - 1);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_S) {
            // Switch on the satellite images
            mMapView.toggleSatellite();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_T) {
            // Switch on traffic overlays
            mMapView.toggleTraffic();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_P) {
            // W00t! Search for Pizza.
            // Create a MapPoint from the map's coordinates
            MapPoint mapPoint = new MapPoint(mMapView.getMapCenter().getLatitudeE6(),
                    mMapView.getMapCenter().getLongitudeE6());
            // Create a dummy Map for use in Search
            Map map = new Map(getDispatcher(), null, 0, 0, 0, mapPoint,
                    Zoom.getZoom(mMapView.getZoomLevel()), 0);
            // Search for Pizza near the specified coordinates
            Search search = new Search("Pizza", map, 0);
            // add the request the dispatcher
            getDispatcher().addDataRequest(search);
            // Wait for the search to complete, Should do this
            // in another thread ideally, this is just for illustration here.
            while (!search.isComplete()) {
                Log.i(LOG_TAG, ".");
            }

            // Print the details.
            Log.i(LOG_TAG, "Done - " + search.numPlacemarks());
            MapPoint point = null;
            for (int i = 0; i < search.numPlacemarks(); i++) {
                Placemark placemark = search.getPlacemark(i);
                point = placemark.getLocation();
                Log.i(LOG_TAG, " - i : " + Integer.toString(i));
                Log.i(LOG_TAG, "- Bubble : " + placemark.getBubbleDescriptor());
                Log.i(LOG_TAG, "- Detail : " + placemark.getDetailsDescriptor());
                Log.i(LOG_TAG, "- Title : " + placemark.getTitle());
                Log.i(LOG_TAG, "- Location : " + placemark.getLocation().toString());
                Log.i(LOG_TAG, "- routable : " + placemark.routableString());
            }

            // Animate to the last location.
            if (point != null) {
                MapController mc = mMapView.getController();
                Point point1 = new Point(point.getLatitude(), point.getLongitude());
                mc.animateTo(point1);
                mc.zoomTo(12);
                Log.i("animateTo", point1.toString());
            }
            return true;
        }
        return false;
    }
}

Here’s the complete zip


http://people.apache.org/~dims/android/BrowseMap.zip

UPDATE (Nov 19, 2007): Please see here for Version 2.0 Drawing overlays for Android Maps (aka Search for Starbucks)

17 Comments »

  1. [...] [working-with-google-androids-maps-aka-search-for-pizza]-Working with Google Android’s Maps FYI, [link] [...]

    Pingback by [working-with-google-androids-maps-aka-search-for-pizza]-Working with Google Android’s Maps « Malnews3’s Weblog — November 16, 2007 @ 3:17 am

  2. [...] have already made sample applications. This is a good thing because early adopters make the current buzz even stronger. Now, if these [...]

    Pingback by that’s great… » android says, “hello world”… — November 16, 2007 @ 11:20 am

  3. Good work! Just curious, where did you get the api doc for com.google.googlenav.*? I couldnt find the javadoc on code.google.com

    cheers
    raja

    Comment by raja — November 20, 2007 @ 11:39 am

  4. [...] http://davanum.wordpress.com/2007/11/15/ (Cet exemple ci montre comment trouver des pizza avec un GPhone ;) ) [...]

    Pingback by Helyx Weblog » Blog Archive » Première contributions et démos intéressantes pour Android — November 21, 2007 @ 4:15 pm

  5. [...] the weekend, he released his BrowseMap 2.0 sample code for using Overlay to draw Starbucks locations onto a map, as an update to his earlier BrowseMap [...]

    Pingback by Tutorials: TwitterClient and BrowseMap 2.0 | Android Notes — November 23, 2007 @ 12:38 pm

  6. Hi, I am from Kanpur, i am behind the proxy and it will ask authentication. My port number is: 3128.

    Why am tell all these things is , I can’t open map application which is given by emulator itself.
    I have read one article in Google group; it says that, In India Map application will not open without proxy tunnel and proxifire. Is it true?
    I set Http Proxy settings using following sqlite3 Command,
    adb shell sqlite3 /data/data/com.google.android.providers.settings/databases/settings.db “\” insert into system values(99,’http_proxy’,’proxy.com:3128’);\””

    I set User Name and password using following command,
    adb shell sqlite3 /data/data/com.google.android.browser/databases/browser.db “\” insert into password values(99,’username’,’passwords’);\””

    Can you help me solve this problem?

    Regards,
    Venkat. :)

    Comment by Venkat — December 28, 2007 @ 1:50 pm

  7. Do you know how to incorporate driving directions into your program? If so, can you tell me how?

    Thanx a million,

    Comment by Tarek — December 30, 2007 @ 5:32 pm

  8. Search snippet shown in this sample only returns 9 results. How do I get results updated for 2nd set of 9 results? (i.e. beyond the 1st 9 results returned.)
    I still can’t find any documentation on com.google.googlenav.* :(

    Comment by Jay Kim — January 16, 2008 @ 2:23 am

  9. Davanum,

    I wonder if this has been changed in latest SDK? or Android team is trying to prevent such actions.

    I’ve got the latest SDK from dec 14th. and whenever i create a dummy map for the search, it always has inside MapPoint set to (36.149777,-95.993398), without considering the point that i pass in. I’ve tried your coordinates, palo alto… doesn’t work, the default map is always created with the above location.

    Has anyone got it working?

    Comment by Alexey — January 25, 2008 @ 2:56 am

  10. Hi,
    its vey useful code give by you.
    Could please provide me the API reference for com.google.googlenav as i did not find it.

    Thanks in advance
    Kishor

    Comment by kishor — February 4, 2008 @ 6:45 am

  11. Placemark is not present in com.google.googlenav

    Comment by rahul — March 9, 2008 @ 12:54 pm

  12. Hi guys,

    I could not find “com.google.googlenav.Search”.

    Can anyone please tell me where I can find googlenav.Search

    Thanks
    Chris

    Comment by Chris Sammut — April 13, 2008 @ 5:57 am

  13. Hi,

    Thanks a lot for the wonderful and useful information. I was trying to access the zip file but could not download the same from the site. Could you please provide me the same.

    Thanks,
    Sid

    Comment by Sidhartha — September 16, 2008 @ 12:49 am

  14. I am finding some problems in compiling the code. What version of android SDK to be used??

    Comment by Ellen — November 1, 2008 @ 11:04 pm

  15. The link to download the zip file is not working. Can anyone please send it to me at sagar AT gatech DOT edu

    Thanks

    Comment by Sag — November 29, 2008 @ 5:57 pm

  16. [...] Working with Google Android’s Maps (aka Search for Pizza) « Show me the code! [...]

    Pingback by Android Developer Resources, useful links « Eulogik — July 24, 2009 @ 4:46 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.