Basic Map-Application

Tutorials that use the MapActivity. Many using GPS functionality.

Basic Map-Application

Postby plusminus » Fri Nov 16, 2007 12:40 am

Basic Map-Application
This tutorial is updated, but working solutions can be found in the responses!


What is this: This tutorial shows how to extend the MapActivity to create relatively sophisticated applications in very short time.

What you learn: You will learn how to extend the MapActivity to create Map-Applications.

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

Difficulty: 1 of 5 :)

What it will look like: (Statue of Liberty, Eiffeltower, Broadway)
Image Image Image

Description:

What we will do is, create a MapActivity(extend it) and setup a MapView-Object.
The Main steps are:
    1. Setup MapView
    1.1. Locate a positon and zoom
    2. Toggle to sattelite mode
    (3. register some key-events on the MapActivity(==on our whole app))

The Code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.basicmapapp;
  2.  
  3. import android.os.Bundle;
  4. import android.view.KeyEvent;
  5.  
  6. import com.google.android.maps.MapActivity;
  7. import com.google.android.maps.MapController;
  8. import com.google.android.maps.MapView;
  9. import com.google.android.maps.Point;
  10.  
  11. public class BasicMapApp extends MapActivity {
  12.     private MapView myMapView;
  13.  
  14.     @Override
  15.     public void onCreate(Bundle icicle) {
  16.         super.onCreate(icicle);
  17.         myMapView = new MapView(this);
  18.  
  19.         // Lets start at the Statue of Liberty
  20.         // I grabbed the data from Google-Maps
  21.         Point p = new Point((int) (40.689213 * 1000000),
  22.                                                 (int) (-74.044558 * 1000000));
  23.         // Get the controller, that is used for translation and zooming
  24.         MapController mc = myMapView.getController();
  25.         // Translate to the Statue of Liberty
  26.         mc.animateTo(p);
  27.         // Zoom Very close
  28.         mc.zoomTo(21);
  29.         // Make myMapView the exilicit view of this app
  30.         setContentView(myMapView);
  31.         // Enable Sattelite-Mode, so we will se the
  32.         // Statue of liberty instantly on the screen
  33.         myMapView.toggleSatellite();
  34.     }
  35.  
  36.     public boolean onKeyDown(int keyCode, KeyEvent event) {
  37.         if (keyCode == KeyEvent.KEYCODE_I) {
  38.             // Zooming In
  39.             myMapView.getController().zoomTo(myMapView.getZoomLevel() + 1);
  40.             return true;
  41.         } else if (keyCode == KeyEvent.KEYCODE_O) {
  42.             // Zooming Out
  43.             myMapView.getController().zoomTo(myMapView.getZoomLevel() - 1);
  44.             return true;
  45.         } else if (keyCode == KeyEvent.KEYCODE_S) {
  46.                 // Switch to satellite view
  47.             myMapView.toggleSatellite();
  48.             return true;
  49.         } else if (keyCode == KeyEvent.KEYCODE_T) {
  50.             // Switch on traffic overlays
  51.             myMapView.toggleTraffic();
  52.             return true;
  53.         }
  54.         return false;
  55.     }
  56. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
Last edited by plusminus on Thu Sep 11, 2008 2:24 pm, edited 2 times in total.
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

I have a Question.

Postby Mark » Fri Nov 16, 2007 5:05 pm

Which menu can I run do it? :?: :?:
Mark
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 16, 2007 4:53 pm

Re: I have a Question.

Postby plusminus » Fri Nov 16, 2007 5:25 pm

Mark wrote:Which menu can I run do it? :?: :?:

I didn't get what you exactly mean...

Are you asking for, how to start this application :?:
:arrow: so this is the solution:
Image

Or do you mean the Process of creating a whole Project :?:
:arrow: Follow this example creation AND simply replace the Main-Code with the Code from above.

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 tagazok » Sun Nov 18, 2007 4:52 pm

hey, thx for your tutorials. I'm french and don't speak english very well and so, I prefere your tutorials than theses on the google code site.

So, when I have my map, I'd like to add a menu under the map but on the screen (not with the menu button) like that :
Image

with icons (zoom, vue which are in the button menu of my application for the moment) on the transparent menu at the bottom.
Or, a real barre (not transparent and not on the map), under the map where I can have buttons.

My problem is that only the last setContentView() I call in my code splash on sceen, if I make a setContentView(myMap) and then a setContentView(button), I only see the map.
I hope you understood what I said un my 'english'.

++
L'idiot ne savait pas que cela était impossible...
...alors il l'a fait.
tagazok
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sun Nov 18, 2007 4:39 pm
Location: Nice / Paris

Postby plusminus » Sun Nov 18, 2007 5:32 pm

Hey tagazok,
At first: THX :)
As my english is also not perfect I'm obviously writing an... simplified dialect... or whatever ^^

What comes to my mind is setting up an XML-Layout (I first just didn't get how that works, but it is so simple :roll: )
This should create an Layout with:
  • a main Map
  • a "+" and a "-" -Button (for Zoom in & out)
But on creation I get an Inflate-Exception on line #8 :?
Code: Select all
Error inflating class MapView.

From the logic point of view this should be valid... perhaps I forgot an attribute :?:
Do you have an explanation for that :?:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- This is the "Main-Layout" a relative one -->
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.                android:layout_width="fill_parent"
  5.                android:layout_height="fill_parent">
  6. <!-- What comes within here, is a child of the outer layout -->
  7.     <!-- This is the Map all actions will take place -->
  8.     <MapView id="@+id/main_map"
  9.              android:layout_width="fill_parent"
  10.              android:layout_height="wrap_content"
  11.              android:text="Type here:"/>
  12.  
  13.     <Button id="@+id/zoomin_button"
  14.            android:layout_width="wrap_content"
  15.            android:layout_height="wrap_content"
  16.            android:layout_below="@id/main_map"
  17.            android:layout_alignParentRight="true"
  18.            android:layout_marginLeft="10px"
  19.            android:text="+" />
  20.  
  21.     <Button id="@+id/zoomout_button"
  22.            android:layout_width="wrap_content"
  23.            android:layout_height="wrap_content"
  24.            android:layout_toLeft="@id/zoomin_button"
  25.            android:layout_alignTop="@id/zoomin_button"
  26.            android:text="-" />
  27. </RelativeLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


I don't know how easy it is, to create that transparency-thing you mentioned...
It would be like putting the MapView to the background OR painting some Overlay-Views over the MapView.
So if we knew how, this is probably not hard :P

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 tagazok » Sun Nov 18, 2007 6:21 pm

Yep, I also have an error here :

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <MapView id="@+id/main_map"
  2.  
  3.              android:layout_width="fill_parent"
  4.  
  5.              android:layout_height="wrap_content"
  6.  
  7.              android:text="Type here:"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


The error is quite obscure in the emulator, The only think I understand is that it's in the xml at this line. Inflating class Mapview seems to be a strange error lol.
L'idiot ne savait pas que cela était impossible...
...alors il l'a fait.
tagazok
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sun Nov 18, 2007 4:39 pm
Location: Nice / Paris

Top

Postby plusminus » Sun Nov 18, 2007 7:29 pm

tagazok wrote:Yep, I also have an error here :

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <MapView id="@+id/main_map"
  2.              android:layout_width="fill_parent"
  3.              android:layout_height="wrap_content"
  4.              android:text="Type here:"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


The error is quite obscure in the emulator, The only think I understand is that it's in the xml at this line. Inflating class Mapview seems to be a strange error lol.


I looked up the word "inflating". I think it means the same if you blow up a balloon to much...
So perhaps the MapView wants to be alone :?:

Really strange..

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 tagazok » Sun Nov 18, 2007 8:11 pm

It's strange, because it will be a good idea to put a map in a little square or like I want, with some others things on the screen.
So, I'm going to search for adding buttons on the map and no under, maybe It will work.

If you find a solution tell me ;)
L'idiot ne savait pas que cela était impossible...
...alors il l'a fait.
tagazok
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sun Nov 18, 2007 4:39 pm
Location: Nice / Paris

Postby russoue » Tue Nov 20, 2007 6:39 am

Can I mix the MapView with other view elements in a LinearLayout? Actually, I was trying to do this but Android cannot inflate my MapView class. I am using xml layout to create my MapView. Here are the codes:

The xml file:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    >
  13.  
  14.         <EditText id="@+id/location"
  15.  
  16.             android:layout_width="fill_parent"
  17.  
  18.             android:layout_height="wrap_content"
  19.  
  20.             android:text="Enter the location here"
  21.  
  22.             android:selectAllOnFocus="true"
  23.  
  24.             />
  25.  
  26.     <MapView id="@+id/mapv"
  27.  
  28.             android:layout_width="fill_parent"
  29.  
  30.         android:layout_height="fill_parent"
  31.  
  32.         />
  33.  
  34. </LinearLayout>
  35.  
  36.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


The only code file:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.russoue.testandroid;
  3.  
  4.  
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.widget.EditText;
  9.  
  10.  
  11.  
  12. import com.google.android.maps.MapActivity;
  13.  
  14.  
  15.  
  16. public class TestAndroid extends MapActivity {
  17.  
  18.     /** Called when the activity is first created. */
  19.  
  20.     @Override
  21.  
  22.     public void onCreate(Bundle icicle) {
  23.  
  24.         super.onCreate(icicle);
  25.  
  26.         setContentView(R.layout.main);
  27.  
  28.         EditText locationField = (EditText) findViewById(R.id.location);
  29.  
  30.         locationField.setSelection(0, locationField.getText().length());
  31.  
  32.     }
  33.  
  34. }
  35.  
  36.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
russoue
Freshman
Freshman
 
Posts: 7
Joined: Fri Nov 16, 2007 6:12 pm

Postby plusminus » Tue Nov 20, 2007 7:38 am

Hi russoue,

I'm having exactly the same problem and had it posted in the Google-Groups, but no one could answer it, yesterday a guy from google contacted me, to help understanding it:

qn@google.com wrote:Hi Daniel and Plusminus,

For the reason why MapView requires MapActivity, please see the first
paragraph on this page:

http://code.google.com/android/referenc ... pView.html

As for the term "inflate", I think the balloon analogy is good. When
the balloon has no air, it is just a flat, 2D piece of rubber. But
when it is inflated, it becomes a real balloon. By analogy, XML
specifications need to be "inflated" to become actual Views. Inflate
exceptions are exceptions encountered by the Inflator during the
inflating process. :)

Cheers,
Q


My answer was how to solve the problem
me wrote:...
So the XML-MapView could not be inflated to an "real" MapView-Object and so the InflateException gets thrown.
But why?
Are there Atrributes missing?
...


:arrow: I will keep you informed of any response :!:

A workaround is working withe the menu or Dialogs...

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 russoue » Tue Nov 20, 2007 4:51 pm

Thanks plusminus. I have also posted the problem to google groups and also trying myself to understand what went wrong. I will share any solutions I find here.
russoue
Freshman
Freshman
 
Posts: 7
Joined: Fri Nov 16, 2007 6:12 pm

Postby lordhong » Tue Nov 27, 2007 4:06 pm

based on google-developers group response, here's how to build a composite view with MapView (i thought building composite view/widget would be as easy as GWT, but seems not...)

basically you need to declare MapView as class, instead of View (see line #6), (confusing... :roll: )

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.    1.  <?xml version="1.0" encoding="utf-8"?>  
  3.  
  4.    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  5.  
  6.   3. android:orientation="vertical" android:layout_width="fill_parent"  
  7.  
  8.   4. android:layout_height="fill_parent">  
  9.  
  10.    5.  
  11.  
  12.    6.         <view class="com.google.android.maps.MapView"
  13.  
  14.   7.         android:layout_width="fill_parent"  
  15.  
  16.   8.         android:layout_height="fill_parent"  
  17.  
  18.   9.         android:layout_weight="1" />  
  19.  
  20.   10.          
  21.  
  22.   11.         <EditText android:layout_width="fill_parent"  
  23.  
  24.  12. android:layout_height="wrap_content"  
  25.  
  26.  13.     android:text="enter address to search..."  
  27.  
  28.  14.     android:selectAllOnFocus="true"/>  
  29.  
  30.   15. </LinearLayout>  
  31.  
  32.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


a testing java class is as simple as:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.    1.  public class GeoDemo extends MapActivity {  
  3.  
  4.    2.     @Override  
  5.  
  6.    3.     public void onCreate(Bundle icicle) {  
  7.  
  8.    4.         super.onCreate(icicle);  
  9.  
  10.    5.         setContentView(R.layout.main);  
  11.  
  12.    6.     }  
  13.  
  14.    7. }  
  15.  
  16.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


no idea why can't we use <MapView> directly... it's a View extends a View ... :shock:
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby plusminus » Tue Nov 27, 2007 5:11 pm

Hi lordhong,

Thx for the info :!:

Dunno, why we have to do it that way.... :?
Perhaps this will be fixed in later versions.

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 Nov 27, 2007 5:28 pm

until google releases android schema XML, we really don't know at this time... :?
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby russoue » Fri Nov 30, 2007 12:07 am

Thanks lordhong, it solved the issue.
russoue
Freshman
Freshman
 
Posts: 7
Joined: Fri Nov 16, 2007 6:12 pm

Top
Next

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests