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)

23 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. […] https://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

  17. It maybe a little too late, but still helpful for others.
    I have written a few simple classes to get and display driving directions in MapView for Android:

    http://home.ameliemedia.com/android-app-aroundme/#tips

    Hope it helps!

    Andrea.

    Comment by Andrea — December 2, 2009 @ 12:13 pm

  18. how to get API google doc ? its very confuse operating it without the doc

    Comment by atics — June 13, 2010 @ 9:35 am

  19. I’m new to android apps development,

    I want to show more than one markers in the map, how do i get lan and lon value for displaying more than one one markers.

    My apps is similar to friend finder. Could you suggest any ideas or link for the coding

    thanks in advance,
    kumar.

    Comment by Kumar — November 9, 2010 @ 5:33 am

  20. Hi

    This tutorial is very helpful for me to develop the map application for android

    Thank you

    Comment by Krishna — November 9, 2010 @ 11:51 am

  21. i can’t download source your aplication…
    i want read this full source…
    for my thesis

    Comment by Java Android UPN Community — November 2, 2011 @ 2:18 pm

  22. how can i place pinpoint in google map by giving latitude and longitude???

    Comment by saran — January 26, 2012 @ 1:54 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: